本文代码来自zetcode.com
Dialogs in PyQt5
对话窗口和对话是密不可分的,在一个计算机应用中,对话是用来和应用程序交流的窗口。
对话被用来输入数据,修改数据,改变应用程序设置等。
QInputDialog
QInputDialog提供一种简单方便的对话,输入值可以是字符串、数字或者一个列表。
#!/usr/bin/python3
"""
In this example, we receive data from a QInputDialog dialog.
"""
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QLineEdit, QInputDialog
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.le.move(130, 22)
self.setGeometry(300, 300, 300, 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(text)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
上述代码中,点击Dialog按钮弹出编辑框以供输入。
如果我们点击OK按钮,布尔值则为True
QColorDialog
QColorDialog提供对话框以选择色彩值
#!/usr/bin/python3
"""
In this example, we select a color value from the QColorDialog and
change the backgroud color of a QFrame widget.
"""
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QFrame, QColorDialog
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 { backgroud-color : %s } " % col.name())
self.frm.setGeometry(130, 22, 100, 100)
self.setGeometry(300, 300, 250, 100)
self.setWindowTitle('Color dialog')
self.show()
def showDialog(self):
col = QColorDialog.getColor()
if col.isValid():
self.frm.setStyleSheet("QWidget { background-color: %s }" %col.name()) #弹出QColorDialog.
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
控件的颜色可以通过Dialog按钮选择
QFontDialog
QFontDialog用来选择字体
#!/usr/bin/python3
"""
In this example, we select a font name
and change the font of a label.
"""
import sys
from PyQt5.QtWidgets import (QWidget, QApplication, QPushButton, QVBoxLayout,
QSizePolicy, QFontDialog, QLabel)
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, 100)
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_())
通过dialog设置字体
QFileDialog
QFileDialog允许用户选择文件或者目录以便打开或者保存
#!/usr/bin/python3
"""
In this example, we select a file with a
QFileDialog and display its contents in a QTextEdit.
"""
import sys
from PyQt5.QtWidgets import QApplication, QTextEdit, QMainWindow, QAction, QFileDialog
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+Q')
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_())
点击File按钮选择Open,打开文档并将文档内容输入在文本框中
fname = QFileDialog.getopenFileName(self, 'Open file', '/home')
我们弹出QFileDialog,getOpenFileName()方法中第一个字符串用来获得文本标题,第二个字符串指示dialog的工作目录,默认文件过滤器设置为全部文件(*)。
读取文件内容