標籤:
PyQt5中的對話方塊
對話方塊視窗或對話方塊是大多數主流GUI應用不可缺少的部分。對話是兩個或更多人之間的會話。在電腦應用中,對話方塊是一個用來和應用對話的視窗。對話方塊可以用來輸入資料,修改資料,改變應用設定等等。
輸入對話方塊
QInputDialog提供了一個簡單便利的對話方塊用於從使用者那兒獲得只一個值。輸入值可以是字串,數字,或者一個列表中的清單項目。
#!/usr/bin/python3# -*- coding: utf-8 -*-"""ZetCode PyQt5 tutorial In this example, we receive data froma QInputDialog dialog. author: Jan Bodnarwebsite: zetcode.com last edited: January 2015"""import sysfrom PyQt5.QtWidgets import (QWidget, QPushButton, QLineEdit, QInputDialog, QApplication)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_())
例子中有一個按鈕和一個單行編輯框組件。按下按鈕會顯示輸入對話方塊用於獲得一個字串值。在對話方塊中輸入的值會在單行編輯框組件中顯示。
text, ok = QInputDialog.getText(self, ‘Input Dialog‘, ‘Enter your name:‘)
這一行會顯示一個輸入對話方塊。第一個字串參數是對話方塊的標題,第二個字串參數是對話方塊內的訊息文本。對話方塊返回輸入的常值內容和一個布爾值。如果我們點擊了Ok按鈕,布爾值就是true,反之布爾值是false(譯者註:也只有按下Ok按鈕時,返回的常值內容才會有值)。
if ok: self.le.setText(str(text))
把我們從對話方塊接收到的文本設定到單行編輯框組件上顯示。
Figure: Input dialog
顏色選擇對話方塊
QColorDialog類提供了一個用於選擇顏色的對話方塊組件。
#!/usr/bin/python3# -*- coding: utf-8 -*-"""ZetCode PyQt5 tutorial In this example, we select a color valuefrom the QColorDialog and change the backgroundcolor of a QFrame widget. author: Jan Bodnarwebsite: zetcode.com last edited: January 2015"""import sysfrom PyQt5.QtWidgets import (QWidget, QPushButton, QFrame, QColorDialog, QApplication)from PyQt5.QtGui import QColorclass 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。將QFrame組件的背景設定為黑色。使用顏色選擇框類,我們可以改變它的顏色。
col = QColor(0, 0, 0)
初始化QtGuiQFrame組件的背景顏色。
col = QColorDialog.getColor()
這一行彈出顏色選擇框。
if col.isValid(): self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name())
如果我們選中一個顏色並且點了ok按鈕,會返回一個有效顏色值。如果我們點擊了Cancel按鈕,不會返回選中的顏色值。我們使用樣式表來定義背景顏色。
字型選擇框
QFontDialog是一個用於選擇字型的對話方塊組件。
#!/usr/bin/python3# -*- coding: utf-8 -*-"""ZetCode PyQt5 tutorial In this example, we select a font nameand change the font of a label. author: Jan Bodnarwebsite: zetcode.com last edited: January 2015"""import sysfrom PyQt5.QtWidgets import (QWidget, QVBoxLayout, QPushButton, QSizePolicy, QLabel, QFontDialog, QApplication)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_())
在我們的例子中,我們有一個按鈕和一個表情。通過字型選擇對話方塊,我們可以改變標籤的字型。
font, ok = QFontDialog.getFont()
在這兒我們彈出一個字型對話方塊。getFont()方法返回字型名字和布爾值。如果使用者點擊了OK,布爾值為True;否則為False。
if ok: self.label.setFont(font)
如果我們點擊了Ok按鈕,標籤字型會被改變。
Figure: Font dialog
檔案對話方塊
檔案對話方塊是用於讓使用者選擇檔案或目錄的對話方塊。可以選擇檔案的開啟和儲存。
#!/usr/bin/python3# -*- coding: utf-8 -*-"""ZetCode PyQt5 tutorial In this example, we select a file with aQFileDialog and display its contentsin a QTextEdit.author: Jan Bodnarwebsite: zetcode.com last edited: January 2015"""import sysfrom PyQt5.QtWidgets import (QMainWindow, QTextEdit, QAction, QFileDialog, QApplication)from PyQt5.QtGui import QIconclass 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_())
樣本中顯示了一個功能表列,中間設定了一個文本編輯框組件,和一個狀態列。點擊功能表項目會顯示QtGui.QFileDialog(檔案選擇框)對話方塊,用於選擇一個檔案。檔案的內容會被讀取並在文本編輯框組件中顯示。
class Example(QMainWindow): def __init__(self): super().__init__() self.initUI()
樣本基於QMainWindow組件,因為我們中心需要設定一個文本編輯框組件。
fname = QFileDialog.getOpenFileName(self, ‘Open file‘, ‘/home‘)
彈出檔案選擇框。第一個字串參數是getOpenFileName()方法的標題。第二個字串參數指定了對話方塊的工作目錄。預設的,檔案過濾器設定成All files (*)。
if fname[0]: f = open(fname[0], ‘r‘) with f: data = f.read() self.textEdit.setText(data)
選中檔案後,讀出檔案的內容,並設定成文本編輯框組件的顯示文本、
Figure: File dialog
這部分的PyQt5教程中,我們學習了幾種對話方塊的使用。
PyQt5教程——對話方塊(6)