(本系列中所有代码在windows7 64位[]/Python 3.4.3 32bit/PyQt GPL v5.5 for Python v3.4 (x32)/eric6-6.0.8下测试通过.)
原本地址:http://zetcode.com/gui/pyqt5/
================================================================================
对话框窗口或对话框是现在GUI程序的一个必不可少的部分.一个对话框是作为两个可多个人之间的交流而定义的.在一个计算机程序中,一个对话框是一个用于和程序"对话"的窗口.
一个对话框用于输入数据、修改数据和修改程序设置等等.
QInputDialog提供了一个简单方便的对话框,它可以从用户那里得到一个的变量.输入的值可以是字符串、数字或列表中的一项.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
ZetCode PyQt5 tutorial
In this example, we receive data from
a QInputDialog dialog.
author: Jan Bodnar
website: zetcode.com
last edited: January 2015
"""
import sys
from 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:')
这一行代码用于显示输入对话框.第一个字符串是对话框的标题,第二个是对话框的信息.对话框返回输入的文本和一个布尔型的值.如果我们点击Okbutton,那么布尔型的值为真.
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: January 2015
"""
import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, QFrame,
QColorDialog, QApplication)
from PyQt5.QtGui import QColor
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_())
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: January 2015
"""
import sys
from 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参数.如果用户点击了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: January 2015
"""
import sys
from PyQt5.QtWidgets import (QMainWindow, QTextEdit,
QAction, QFileDialog, QApplication)
from PyQt5.QtGui import QIcon
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')
f = open(fname, 'r')
with f:
data = f.read()
self.textEdit.setText(data)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
class Example(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
例子是基于QMainWindow的,这是因为我们要在中间设置一个文件编辑器部件.
fname = QFileDialog.getOpenFileName(self, 'Open file', '/home')
我们弹出一个QFileDialog.在getOpenFileName()方法的第一个字符串是标题.第二个字符串指定对话框的工作目录.默认情况下,文件过滤器被设置为允许所有文件(*).
f = open(fname, 'r')
with f:
data = f.read()
self.textEdit.setText(data)
被选择的文件是读的,文件内容被加载到文本编辑器部件中.
图片:文件对话框