對話框
對話框是一個現代GUI應用不可或缺的一部分。對話是兩個人之間的交流,對話框就是人與電腦之間的對話。對話框用來輸入數據,修改數據,修改應用設置等等。
輸入文字
QInputDialog提供了一個簡單方便的對話框,可以輸入字符串,數字或列表。
#!/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_())
這個示例有一個按鈕和一個輸入框,點擊按鈕顯示對話框,輸入的文本會顯示在輸入框里。
text, ok = QInputDialog.getText(self, 'Input Dialog',
'Enter your name:')
這是顯示一個輸入框的代碼。第一個參數是輸入框的標題,第二個參數是輸入框的占位符。對話框返回輸入內容和一個布爾值,如果點擊的是OK按鈕,布爾值就返回True。
if ok:
self.le.setText(str(text))
把得到的字符串放到輸入框里。
程序展示:

選取顏色
QColorDialog提供顏色的選擇。
#!/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改變背景顏色。
col = QColor(0, 0, 0)
初始化QtGui.QFrame的背景顏色。
col = QColorDialog.getColor()
彈出一個QColorDialog對話框。
if col.isValid():
self.frm.setStyleSheet("QWidget { background-color: %s }"
% col.name())
我們可以預覽顏色,如果點擊取消按鈕,沒有顏色值返回,如果顏色是我們想要的,就從取色框里選擇這個顏色。
程序展示:

選擇字體
QFontDialog能做字體的選擇。
#!/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_())
我們創(chuàng)建了一個有一個按鈕和一個標簽的QFontDialog的對話框,我們可以使用這個功能修改字體樣式。
font, ok = QFontDialog.getFont()
彈出一個字體選擇對話框。getFont()方法返回一個字體名稱和狀態(tài)信息。狀態(tài)信息有OK和其他兩種。
if ok:
self.label.setFont(font)
如果點擊OK,標簽的字體就會隨之更改。
程序展示:

選擇文件
QFileDialog給用戶提供文件或者文件夾選擇的功能。能打開和保存文件。
#!/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_())
本例中有一個菜單欄,一個置中的文本編輯框,一個狀態(tài)欄。點擊菜單欄選項會彈出一個QtGui.QFileDialog對話框,在這個對話框里,你能選擇文件,然后文件的內容就會顯示在文本編輯框里。
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
這里設置了一個文本編輯框,文本編輯框是基于QMainWindow組件的。
fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
彈出QFileDialog窗口。getOpenFileName()方法的第一個參數是說明文字,第二個參數是默認打開的文件夾路徑。默認情況下顯示所有類型的文件。
if fname[0]:
f = open(fname[0], 'r')
with f:
data = f.read()
self.textEdit.setText(data)
讀取選中的文件,并顯示在文本編輯框內(但是打開HTML文件時,是渲染后的結果,汗)。
程序展示:
