• <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, 評論 - 8, 引用 - 0
            數據加載中……

            [Python][PyQt4]PyQt4 中的 Dialog

            PyQt4 中的 Dialog

            Dialog 窗口或 dialog 是現代 GUI 應用必不可少的一部分。一個 dialog 定義為兩人或更多人間的會話。在計算機應用中,dialog 就是一個和應用說話的窗口。dialog 可以用于輸入數據,修改數據,更改應用的設置等等。對話框在用戶和計算機的通信間是重要的手段。

            QtGui.QInputDialog

            QtGui.QInputDialog 提供了一個簡單方便的對話框,用于獲取用戶輸入的一個值。輸入值可以是字符串,數字,或者一個列表中的一項。

            #!/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()
            

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

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

            這一行顯示了輸入對話框。第一個字符串是對話框的標題,第二個字符串則是對話框中的消息。對話框將返回輸入的文本和一個布爾值。如果點擊了 ok 按鈕,則布爾值為 true ,否則為 false

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

            從對話框中接收到的文本就被設置到行編輯組件中。

            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()
            

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

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

            這個是 QtGui.QFrame 的初始顏色。

            col = QtGui.QColorDialog.getColor()
            

            這一行將彈出 QtGui.QColorDialog 。

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

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

            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()
            

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

            font, ok = QtGui.QFontDialog.getFont()
            

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

            if ok:
                self.label.setFont(font)
            

            如果我們點擊了 ok,標簽的字體就可能改變。

            QtGui.QFileDialog

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

            #!/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()
            

            這個例子中有菜單欄,文本編輯區以及狀態欄。菜單中的選項顯示 QtGui.QFileDialog 用于選擇文件。而文件的內容則載入到文本編輯區。

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

            這個例子是基于 QtGui.QMainWindow 組件,因為我們要在中心設置文本編輯區。

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

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

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

            選擇的文件將被讀取,并且其文件內容設置到文本編輯區。


            這個部分,我們討論了對話框。

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

            国产福利电影一区二区三区久久老子无码午夜伦不 | 国产精品热久久无码av| 国产A级毛片久久久精品毛片| 亚洲精品高清久久| 婷婷久久综合| 色偷偷偷久久伊人大杳蕉| 久久久精品人妻一区二区三区蜜桃| 久久久久中文字幕| 久久亚洲sm情趣捆绑调教| 亚洲精品无码久久久影院相关影片 | 久久久久99精品成人片牛牛影视| 一级做a爰片久久毛片免费陪| 91精品国产91久久综合| 狠狠人妻久久久久久综合| 久久综合精品国产二区无码| 欧美777精品久久久久网| 色综合久久夜色精品国产| 精品久久久久久| 亚洲中文字幕久久精品无码喷水| 国产精品久久久久一区二区三区| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 亚洲国产另类久久久精品| 国内精品久久久久影院网站| 亚洲香蕉网久久综合影视| 久久人人爽人人爽人人片AV麻豆 | 亚洲&#228;v永久无码精品天堂久久| 人妻无码精品久久亚瑟影视 | 天天爽天天狠久久久综合麻豆| 国产福利电影一区二区三区久久久久成人精品综合 | 色婷婷久久综合中文久久蜜桃av | 99久久国语露脸精品国产| 亚洲中文字幕久久精品无码APP| 欧美亚洲国产精品久久久久| 久久久久久久亚洲精品| 国产成人AV综合久久| .精品久久久麻豆国产精品| 亚洲中文字幕无码久久精品1| 久久久久久久久久久久久久| 97香蕉久久夜色精品国产| 久久久久无码精品| 日韩一区二区三区视频久久|