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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # PyQt5 中的對話框 > 原文: [http://zetcode.com/gui/pyqt5/dialogs/](http://zetcode.com/gui/pyqt5/dialogs/) 對話框窗口或對話框是大多數現代 GUI 應用必不可少的部分。 對話被定義為兩個或更多人之間的對話。 在計算機應用中,對話框是一個窗口,用于與應用“對話”。 對話框用于輸入數據,修改數據,更改應用設置等。 ## `QInputDialog` `QInputDialog`提供了一個簡單的便捷對話框,可從用戶那里獲取單個值。 輸入值可以是字符串,數字或列表中的項目。 `inputdialog.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial In this example, we receive data from a QInputDialog dialog. Aauthor: Jan Bodnar Website: zetcode.com Last edited: August 2017 """ from PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit, QInputDialog, QApplication) import sys class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): self.btn = QPushButton('Dialog', self) self.btn.move(20, 20) self.btn.clicked.connect(self.showDialog) self.le = QLineEdit(self) self.le.move(130, 22) self.setGeometry(300, 300, 290, 150) self.setWindowTitle('Input dialog') self.show() def showDialog(self): text, ok = QInputDialog.getText(self, 'Input Dialog', 'Enter your name:') if ok: self.le.setText(str(text)) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 該示例具有一個按鈕和一個行編輯小部件。 該按鈕顯示用于獲取文本值的輸入對話框。 輸入的文本將顯示在行編輯小部件中。 ```py text, ok = QInputDialog.getText(self, 'Input Dialog', 'Enter your name:') ``` 這行顯示輸入對話框。 第一個字符串是對話框標題,第二個字符串是對話框中的消息。 對話框返回輸入的文本和布爾值。 如果單擊“確定”按鈕,則布爾值為`true`。 ```py if ok: self.le.setText(str(text)) ``` 我們從對話框中收到的文本通過`setText()`設置為行編輯小部件。 ![Input dialog](https://img.kancloud.cn/1d/a6/1da6149d4a1c1f2c0e85b9a4357bb6ce_202x126.jpg) 圖:輸入對話框 ## `QColorDialog` `QColorDialog`提供了一個對話框小部件,用于選擇顏色值。 `colordialog.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial In this example, we select a color value from the QColorDialog and change the background color of a QFrame widget. Author: Jan Bodnar Website: zetcode.com Last edited: August 2017 """ from PyQt5.QtWidgets import (QWidget, QPushButton, QFrame, QColorDialog, QApplication) from PyQt5.QtGui import QColor import sys class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): col = QColor(0, 0, 0) self.btn = QPushButton('Dialog', self) self.btn.move(20, 20) self.btn.clicked.connect(self.showDialog) self.frm = 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 = QColorDialog.getColor() if col.isValid(): self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name()) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 該應用示例顯示了一個按鈕和一個`QFrame`。 窗口小部件背景設置為黑色。 使用`QColorDialog`,我們可以更改其背景。 ```py col = QColor(0, 0, 0) ``` 這是`QFrame`背景的初始顏色。 ```py col = QColorDialog.getColor() ``` 這行彈出`QColorDialog`。 ```py if col.isValid(): self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name()) ``` 我們檢查顏色是否有效。 如果單擊“取消”按鈕,則不會返回有效的顏色。 如果顏色有效,我們將使用樣式表更改背景顏色。 ## `QFontDialog` `QFontDialog`是用于選擇字體的對話框小部件。 `fontdialog.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial In this example, we select a font name and change the font of a label. Author: Jan Bodnar Website: zetcode.com Last edited: August 2017 """ from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton, QSizePolicy, QLabel, QFontDialog, QApplication) import sys class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): vbox = QVBoxLayout() btn = QPushButton('Dialog', self) btn.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed) btn.move(20, 20) vbox.addWidget(btn) btn.clicked.connect(self.showDialog) self.lbl = 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 = QFontDialog.getFont() if ok: self.lbl.setFont(font) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 在我們的示例中,我們有一個按鈕和一個標簽。 使用`QFontDialog`,我們更改標簽的字體。 ```py font, ok = QFontDialog.getFont() ``` 在這里我們彈出字體對話框。 `getFont()`方法返回字體名稱和`ok`參數。 如果用戶單擊“確定”,則它等于`True`; 否則為`False`。 ```py if ok: self.label.setFont(font) ``` 如果單擊“確定”,標簽的字體將更改為`setFont()`。 ## `QFileDialog` `QFileDialog`是允許用戶選擇文件或目錄的對話框。 可以選擇打開和保存文件。 `filedialog.py` ```py #!/usr/bin/python3 # -*- coding: utf-8 -*- """ ZetCode PyQt5 tutorial In this example, we select a file with a QFileDialog and display its contents in a QTextEdit. Author: Jan Bodnar Website: zetcode.com Last edited: August 2017 """ from PyQt5.QtWidgets import (QMainWindow, QTextEdit, QAction, QFileDialog, QApplication) from PyQt5.QtGui import QIcon import sys class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): self.textEdit = QTextEdit() self.setCentralWidget(self.textEdit) self.statusBar() openFile = QAction(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 = QFileDialog.getOpenFileName(self, 'Open file', '/home') if fname[0]: f = open(fname[0], 'r') with f: data = f.read() self.textEdit.setText(data) if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) ``` 該示例顯示了一個菜單欄,集中設置的文本編輯小部件和一個狀態欄。 菜單項顯示用于選擇文件的`QFileDialog`。 文件的內容被加載到文本編輯小部件中。 ```py class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() ``` 該示例基于`QMainWindow`小部件,因為我們集中設置了文本編輯小部件。 ```py fname = QFileDialog.getOpenFileName(self, 'Open file', '/home') ``` 我們彈出`QFileDialog`。 `getOpenFileName()`方法中的第一個字符串是標題。 第二個字符串指定對話框的工作目錄。 默認情況下,文件過濾器設置為`All Files (*)`。 ```py if fname[0]: f = open(fname[0], 'r') with f: data = f.read() self.textEdit.setText(data) ``` 讀取所選文件名,并將文件內容設置為文本編輯小部件。 在 PyQt5 教程的這一部分中,我們使用了對話框。
                  <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>

                              哎呀哎呀视频在线观看