对话框是大多数GUI应用中不可分割的一部分。一个对话框是二者或多者的会话。在GUI内,对话框是应用向人说话的方式。一个对话框能够用来输入数据,修改数据,改变应用设置等等。python
QtGui.QInputDialog
给用户提供了一个简单方便的对话框来获取值。输入的值能够使字符串,一个数字,或者是一个列表中的元素。web
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we receive data from a QtGui.QInputDialog dialog. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.btn = QtGui.QPushButton('Dialog', self) self.btn.move(20, 20) self.btn.clicked.connect(self.showDialog) self.le = QtGui.QLineEdit(self) self.le.move(130, 22) self.setGeometry(300, 300, 290, 150) self.setWindowTitle('Input dialog') self.show() def showDialog(self): text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:') if ok: self.le.setText(str(text)) def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main()
例子有一个按钮和一行编辑控件。点击按钮显示处获得文本值的对话框。输入的值将会在编辑控件里显示出来。app
text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
这行代码显示了输入对话框。第一个字符串是对话框的标题(title),第二个字符串是对话框里的信息。对话框返回输入的文本值和一个boolean值。若是咱们点击OK按钮,布尔值将会为真。字体
if ok: self.le.setText(str(text))
这行代码使应用将对话框收到的文本值放到编辑控件上。
ui
QtGui.QColorDialog
提供了一个选择颜色的对话框this
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we select a colour value from the QtGui.QColorDialog and change the background colour of a QtGui.QFrame widget. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): col = QtGui.QColor(0, 0, 0) self.btn = QtGui.QPushButton('Dialog', self) self.btn.move(20, 20) self.btn.clicked.connect(self.showDialog) self.frm = QtGui.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 = QtGui.QColorDialog.getColor() if col.isValid(): self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name()) def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main()
应用的例子显示了一个按钮和一个QtGui.QFrame
。控件背景被设定成了黑色。使用QtGui.QColorDialog
,咱们能够改变背景颜色。code
col = QtGui.QColor(0, 0, 0)
这是QtGui.QFrame的初始背景颜色。blog
col = QtGui.QColorDialog.getColor()
这一行将会弹出选择颜色的界面。
ip
if col.isValid(): self.frm.setStyleSheet("QWidget { background-color: %s }" % col.name())
咱们检查颜色是否合法,若是咱们点击取消键,不会返回合法的颜色。若是颜色是合法的,咱们改变背景颜色。
utf-8
QtGui.QFontDialog
是一个用来改变字体的对话框控件。
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we select a font name and change the font of a label. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): vbox = QtGui.QVBoxLayout() btn = QtGui.QPushButton('Dialog', self) btn.setSizePolicy(QtGui.QSizePolicy.Fixed, QtGui.QSizePolicy.Fixed) btn.move(20, 20) vbox.addWidget(btn) btn.clicked.connect(self.showDialog) self.lbl = QtGui.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 = QtGui.QFontDialog.getFont() if ok: self.lbl.setFont(font) def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main()
在咱们的例子里,咱们有一个按钮和一个标签,咱们能够改变标签的字体。
font, ok = QtGui.QFontDialog.getFont()
在这里咱们弹出了字体对话框。getFont()
方法会返回字体的名字和ok参数。当点击ok时返回True,不然就是返回False。
if ok: self.label.setFont(font)
若是咱们点击ok,标签的字体会被改变。
QtGui.QFileDialog
是容许用户选择文件或者文件夹的对话框。选择的文件能够用来打开或者保存。
#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, we select a file with a QtGui.QFileDialog and display its contents in a QtGui.QTextEdit. author: Jan Bodnar website: zetcode.com last edited: October 2011 """ import sys from PyQt4 import QtGui class Example(QtGui.QMainWindow): def __init__(self): super(Example, self).__init__() self.initUI() def initUI(self): self.textEdit = QtGui.QTextEdit() self.setCentralWidget(self.textEdit) self.statusBar() openFile = QtGui.QAction(QtGui.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 = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home') f = open(fname, 'r') with f: data = f.read() self.textEdit.setText(data) def main(): app = QtGui.QApplication(sys.argv) ex = Example() sys.exit(app.exec_()) if __name__ == '__main__': main()
这个例子显示了一个菜单栏,在中心有文本编辑控件和一个状态栏。菜单用QtGui.QFileDialog
来选择文件。文件的内容将会被载入到编辑控件上。
class Example(QtGui.QMainWindow): def __init__(self): super(Example, self).__init__()
由于咱们将文本编辑控件放在了中心,因此例子是基于QtGui.QMainWindow
控件。
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home')
咱们弹出了QtGui.QFileDialog
,第一个字符串在getOpenFileName()方法内是字幕。第二个字符串是指定对话框的工做目录。默认的,文件格式包括全部文件(.*)
f = open(fname, 'r') with f: data = f.read() self.textEdit.setText(data)
上面这段代码读文件,而且将文件内容载入到文本编辑控件中。