• <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 閱讀(4900) 評論(0)  編輯 收藏 引用 所屬分類: Python

            久久亚洲天堂| 国产激情久久久久影院老熟女免费| 久久久久免费视频| 性做久久久久久免费观看| 亚洲成色WWW久久网站| 久久综合丝袜日本网| 国产69精品久久久久APP下载| 久久人爽人人爽人人片AV| 国产午夜电影久久| 新狼窝色AV性久久久久久| 999久久久免费国产精品播放| 久久热这里只有精品在线观看| 久久99精品国产一区二区三区 | 色综合久久综合网观看| 久久久久亚洲国产| 国产精品成人精品久久久| 伊人久久大香线蕉av一区| 久久久91人妻无码精品蜜桃HD | 亚洲精品成人久久久| 久久精品成人国产午夜| 精品久久久无码人妻中文字幕| 久久97久久97精品免视看秋霞 | 国产产无码乱码精品久久鸭 | 88久久精品无码一区二区毛片| 久久亚洲精品人成综合网| 国内精品九九久久精品| 亚洲精品无码久久毛片| 久久国产香蕉一区精品| 久久精品国产亚洲5555| 国产精品伊人久久伊人电影| 热re99久久精品国产99热| 7国产欧美日韩综合天堂中文久久久久 | 岛国搬运www久久| 91久久婷婷国产综合精品青草| 色综合久久综合中文综合网| 久久人人爽人人爽人人片AV不| 亚洲精品无码专区久久同性男| 色青青草原桃花久久综合| 久久夜色精品国产噜噜亚洲a| 7777精品伊人久久久大香线蕉| 97视频久久久|