• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 41, 文章 - 8, 評(píng)論 - 8, 引用 - 0
            數(shù)據(jù)加載中……

            [Python][PyQt4]PyQt4 中的 Dialog

            PyQt4 中的 Dialog

            Dialog 窗口或 dialog 是現(xiàn)代 GUI 應(yīng)用必不可少的一部分。一個(gè) dialog 定義為兩人或更多人間的會(huì)話。在計(jì)算機(jī)應(yīng)用中,dialog 就是一個(gè)和應(yīng)用說話的窗口。dialog 可以用于輸入數(shù)據(jù),修改數(shù)據(jù),更改應(yīng)用的設(shè)置等等。對(duì)話框在用戶和計(jì)算機(jī)的通信間是重要的手段。

            QtGui.QInputDialog

            QtGui.QInputDialog 提供了一個(gè)簡(jiǎn)單方便的對(duì)話框,用于獲取用戶輸入的一個(gè)值。輸入值可以是字符串,數(shù)字,或者一個(gè)列表中的一項(xiàng)。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            In this example, we receive data from
            a QtGui.QInputDialog dialog.
            author: Jan Bodnar
            website: zetcode.com
            last edited: October 2011
            """
            import sys
            from PyQt4 import QtGui
            class Example(QtGui.QWidget):
                def __init__(self):
                    super(Example, self).__init__()
                    self.initUI()
                def initUI(self):
                    self.btn = QtGui.QPushButton('Dialog', self)
                    self.btn.move(20, 20)
                    self.btn.clicked.connect(self.showDialog)
                    self.le = QtGui.QLineEdit(self)
                    self.le.move(130, 22)
                    self.setGeometry(300, 300, 290, 150)
                    self.setWindowTitle('Input dialog')
                    self.show()
                def showDialog(self):
                    text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',
                        'Enter your name:')
                    if ok:
                        self.le.setText(str(text))
            def main():
                app = QtGui.QApplication(sys.argv)
                ex = Example()
                sys.exit(app.exec_())
            if __name__ == '__main__':
                main()
            

            這個(gè)例子中用到了一個(gè)按鈕和一個(gè)行編輯組件。按鈕會(huì)顯示一個(gè)輸入對(duì)話框用于得到文本。而輸入的文本將在行編輯組件中顯示。

            text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog',
                'Enter your name:')
            

            這一行顯示了輸入對(duì)話框。第一個(gè)字符串是對(duì)話框的標(biāo)題,第二個(gè)字符串則是對(duì)話框中的消息。對(duì)話框?qū)⒎祷剌斎氲奈谋竞鸵粋€(gè)布爾值。如果點(diǎn)擊了 ok 按鈕,則布爾值為 true ,否則為 false 。

            if ok:
                self.le.setText(str(text))
            

            從對(duì)話框中接收到的文本就被設(shè)置到行編輯組件中。

            QtGui.QColorDialog

            QtGui.QColorDialog 用于選取顏色值。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            In this example, we select a color value
            from the QtGui.QColorDialog and change the background
            color of a QtGui.QFrame widget.
            author: Jan Bodnar
            website: zetcode.com
            last edited: October 2011
            """
            import sys
            from PyQt4 import QtGui
            class Example(QtGui.QWidget):
                def __init__(self):
                    super(Example, self).__init__()
                    self.initUI()
                def initUI(self):
                    col = QtGui.QColor(0, 0, 0)
                    self.btn = QtGui.QPushButton('Dialog', self)
                    self.btn.move(20, 20)
                    self.btn.clicked.connect(self.showDialog)
                    self.frm = QtGui.QFrame(self)
                    self.frm.setStyleSheet("QWidget { background-color: %s }"
                        % col.name())
                    self.frm.setGeometry(130, 22, 100, 100)
                    self.setGeometry(300, 300, 250, 180)
                    self.setWindowTitle('Color dialog')
                    self.show()
                def showDialog(self):
                    col = QtGui.QColorDialog.getColor()
                    if col.isValid():
                        self.frm.setStyleSheet("QWidget { background-color: %s }"
                            % col.name())
            def main():
                app = QtGui.QApplication(sys.argv)
                ex = Example()
                sys.exit(app.exec_())
            if __name__ == '__main__':
                main()
            

            這個(gè)例子顯示一個(gè)按鈕和一個(gè) QtGui.QFrame 。這個(gè)組件的背景被設(shè)為黑色。使用 QtGui.QColorDialog 可以改變其背景。

            col = QtGui.QColor(0, 0, 0)
            

            這個(gè)是 QtGui.QFrame 的初始顏色。

            col = QtGui.QColorDialog.getColor()
            

            這一行將彈出 QtGui.QColorDialog

            if col.isValid():
                self.frm.setStyleSheet("QWidget { background-color: %s }"
                    % col.name())
            

            我們檢查顏色是否合法。如果點(diǎn)擊了取消按鈕,返回的就不是合法值。如果顏色合法,我們就用樣式表更改背景顏色。

            QtGui.QFontDialog

            QtGui.QFontDialog 用于選取字體。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            In this example, we select a font name
            and change the font of a label.
            author: Jan Bodnar
            website: zetcode.com
            last edited: October 2011
            """
            import sys
            from PyQt4 import QtGui
            class Example(QtGui.QWidget):
                def __init__(self):
                    super(Example, self).__init__()
                    self.initUI()
                def initUI(self):
                    vbox = QtGui.QVBoxLayout()
                    btn = QtGui.QPushButton('Dialog', self)
                    btn.setSizePolicy(QtGui.QSizePolicy.Fixed,
                        QtGui.QSizePolicy.Fixed)
                    btn.move(20, 20)
                    vbox.addWidget(btn)
                    btn.clicked.connect(self.showDialog)
                    self.lbl = QtGui.QLabel('Knowledge only matters', self)
                    self.lbl.move(130, 20)
                    vbox.addWidget(self.lbl)
                    self.setLayout(vbox)
                    self.setGeometry(300, 300, 250, 180)
                    self.setWindowTitle('Font dialog')
                    self.show()
                def showDialog(self):
                    font, ok = QtGui.QFontDialog.getFont()
                    if ok:
                        self.lbl.setFont(font)
            def main():
                app = QtGui.QApplication(sys.argv)
                ex = Example()
                sys.exit(app.exec_())
            if __name__ == '__main__':
                main()
            

            在我們的例子中,我們有一個(gè)按鈕和一個(gè)標(biāo)簽。通過 QtGui.QFontDialog 我們可以改變標(biāo)簽的字體。

            font, ok = QtGui.QFontDialog.getFont()
            

            我們彈出一個(gè)字體對(duì)話框。 getFont() 方法將返回字體的名稱和 ok 參數(shù)。如果用戶點(diǎn)擊了 OK 那么就是 True ,否則為 False

            if ok:
                self.label.setFont(font)
            

            如果我們點(diǎn)擊了 ok,標(biāo)簽的字體就可能改變。

            QtGui.QFileDialog

            QtGui.QFileDialog 是允許用戶選擇文件或目錄的對(duì)話框。文件可以用于打開或保存。

            #!/usr/bin/python
            # -*- coding: utf-8 -*-
            """
            ZetCode PyQt4 tutorial
            In this example, we select a file with a
            QtGui.QFileDialog and display its contents
            in a QtGui.QTextEdit.
            author: Jan Bodnar
            website: zetcode.com
            last edited: October 2011
            """
            import sys
            from PyQt4 import QtGui
            class Example(QtGui.QMainWindow):
                def __init__(self):
                    super(Example, self).__init__()
                    self.initUI()
                def initUI(self):
                    self.textEdit = QtGui.QTextEdit()
                    self.setCentralWidget(self.textEdit)
                    self.statusBar()
                    openFile = QtGui.QAction(QtGui.QIcon('open.png'), 'Open', self)
                    openFile.setShortcut('Ctrl+O')
                    openFile.setStatusTip('Open new File')
                    openFile.triggered.connect(self.showDialog)
                    menubar = self.menuBar()
                    fileMenu = menubar.addMenu('&File')
                    fileMenu.addAction(openFile)
                    self.setGeometry(300, 300, 350, 300)
                    self.setWindowTitle('File dialog')
                    self.show()
                def showDialog(self):
                    fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file',
                            '/home')
                    f = open(fname, 'r')
                    with f:
                        data = f.read()
                        self.textEdit.setText(data)
            def main():
                app = QtGui.QApplication(sys.argv)
                ex = Example()
                sys.exit(app.exec_())
            if __name__ == '__main__':
                main()
            

            這個(gè)例子中有菜單欄,文本編輯區(qū)以及狀態(tài)欄。菜單中的選項(xiàng)顯示 QtGui.QFileDialog 用于選擇文件。而文件的內(nèi)容則載入到文本編輯區(qū)。

            class Example(QtGui.QMainWindow):
                def __init__(self):
                    super(Example, self).__init__()
            

            這個(gè)例子是基于 QtGui.QMainWindow 組件,因?yàn)槲覀円谥行脑O(shè)置文本編輯區(qū)。

            fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file',
                '/home')
            

            我們彈出 QtGui.QFileDialog 。在 getOpenFileName() 方法中第一個(gè)字符串是標(biāo)題。第二個(gè)則是指定對(duì)話框的工作目錄。默認(rèn)情況下,文件過濾為所有文件( * )。

            f = open(fname, 'r')
            with f:
                data = f.read()
                self.textEdit.setText(data)
            

            選擇的文件將被讀取,并且其文件內(nèi)容設(shè)置到文本編輯區(qū)。


            這個(gè)部分,我們討論了對(duì)話框。

            posted on 2012-02-12 10:06 mirguest 閱讀(4887) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Python

            色99久久久久高潮综合影院| 亚洲AV无码成人网站久久精品大| 精品无码久久久久久久久久| 亚洲&#228;v永久无码精品天堂久久 | 日产精品久久久久久久性色| 久久精品国产99国产精品澳门| 久久青青草原精品国产不卡| 久久综合亚洲欧美成人| 久久久精品波多野结衣| 久久亚洲春色中文字幕久久久| 精品无码人妻久久久久久| 日本久久久久亚洲中字幕| 久久99精品久久久久久不卡| 无码AV波多野结衣久久| 三级片免费观看久久| 伊人久久综合热线大杳蕉下载| 久久久久久久女国产乱让韩| 久久se这里只有精品| 99国产精品久久| 伊人久久大香线蕉av不变影院| 国产精品美女久久久免费| 1000部精品久久久久久久久| 老男人久久青草av高清| 久久亚洲中文字幕精品一区四| 午夜不卡888久久| 久久99久久99小草精品免视看| 色婷婷综合久久久久中文一区二区 | 久久久亚洲精品蜜桃臀| 丁香五月综合久久激情| 国产精品久久久久AV福利动漫| 亚洲欧洲中文日韩久久AV乱码| 久久久久九九精品影院| 99久久国产主播综合精品| 久久99国产亚洲高清观看首页| 国产午夜福利精品久久2021| 欧美va久久久噜噜噜久久| 亚洲愉拍99热成人精品热久久| 久久精品日日躁夜夜躁欧美| 精品无码久久久久国产动漫3d| 久久婷婷国产剧情内射白浆| 2019久久久高清456|