<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # PyQt4 中的對話框 > 原文: [http://zetcode.com/gui/pyqt4/dialogs/](http://zetcode.com/gui/pyqt4/dialogs/) 對話框窗口或對話框是大多數現代 GUI 應用必不可少的部分。 對話被定義為兩個或更多人之間的對話。 在計算機應用中,對話框是一個窗口,用于與應用“對話”。 對話框用于輸入數據,修改數據,更改應用設置等。 ## `QtGui.QInputDialog` `QtGui.QInputDialog`提供了一個簡單的便捷對話框,可從用戶那里獲取單個值。 輸入值可以是字符串,數字或列表中的項目。 ```py #!/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() ``` 該示例具有一個按鈕和一個行編輯小部件。 該按鈕顯示用于獲取文本值的輸入對話框。 輸入的文本將顯示在行編輯小部件中。 ```py text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:') ``` 這行顯示輸入對話框。 第一個字符串是對話框標題,第二個字符串是對話框中的消息。 對話框返回輸入的文本和布爾值。 如果單擊“確定”按鈕,則布爾值為`true`。 ```py if ok: self.le.setText(str(text)) ``` 我們從對話框中收到的文本設置為行編輯小部件。 ![Input Dialog](https://img.kancloud.cn/1d/a6/1da6149d4a1c1f2c0e85b9a4357bb6ce_202x126.jpg) 圖:輸入對話框 ## `QtGui.QColorDialog` `QtGui.QColorDialog`提供了一個對話框小部件,用于選擇顏色值。 ```py #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we select a colour value from the QtGui.QColorDialog and change the background colour 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`,我們可以更改其背景。 ```py col = QtGui.QColor(0, 0, 0) ``` 這是`QtGui.QFrame`背景的初始顏色。 ```py col = QtGui.QColorDialog.getColor() ``` 這行會彈出`QtGui.QColorDialog`。 ```py if col.isValid(): self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name()) ``` 我們檢查顏色是否有效。 如果單擊“取消”按鈕,則不會返回有效的顏色。 如果顏色有效,我們將使用樣式表更改背景顏色。 ![Color dialog](https://img.kancloud.cn/e6/50/e65071affaddd4be9fe5dc6e4448ebc4_400x303.jpg) 圖:顏色對話框 ## `QtGui.QFontDialog` `QtGui.QFontDialog`是用于選擇字體的對話框小部件。 ```py #!/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`,我們更改標簽的字體。 ```py font, ok = QtGui.QFontDialog.getFont() ``` 在這里我們彈出字體對話框。 `getFont()`方法返回字體名稱和`ok`參數。 如果用戶單擊“確定”,則等于`True`。 否則為`False`。 ```py if ok: self.label.setFont(font) ``` 如果單擊確定,則標簽的字體將被更改。 ## `QtGui.QFileDialog` `QtGui.QFileDialog`是允許用戶選擇文件或目錄的對話框。 可以選擇打開和保存文件。 ```py #!/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`。 文件的內容被加載到文本編輯小部件中。 ```py class Example(QtGui.QMainWindow): def __init__(self): super(Example, self).__init__() ``` 該示例基于`QtGui.QMainWindow`小部件,因為我們集中設置了文本編輯小部件。 ```py fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home') ``` 我們彈出`QtGui.QFileDialog`。 `getOpenFileName()`方法中的第一個字符串是標題。 第二個字符串指定對話框的工作目錄。 默認情況下,文件過濾器設置為`All Files (*)`。 ```py f = open(fname, 'r') with f: data = f.read() self.textEdit.setText(data) ``` 讀取所選文件名,并將文件內容設置為文本編輯小部件。 ![File Dialog](https://img.kancloud.cn/64/ce/64cecae4bbba1a41e757ba0465476cc6_400x182.jpg) 圖:文件對話框 在 PyQt4 教程的這一部分中,我們使用了對話框。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看