Dialogs in PyQt4
::-- zhuyj [2008-06-16 09:48:48]
1. Dialogs in PyQt4
PyQt4中的对话框
Dialog windows or dialogs are an indispensable part of most modern GUI applications. A dialog is defined as a conversation between two or more persons. In a computer application a dialog is a window which is used to "talk" to the application. A dialog is used to input data, modify data, change the application settings etc. Dialogs are important means of communication between a user and a computer program.
- 对话窗口和对话框是现代GUI应用程序中不可缺少的部分。对话的定义是两个或多个人之间的会话,在计算机程序中对话是一个与应用程序对话的窗口。对话可以用来完成输入数据,修改数据,改变应用程序设置等工作。对话是重要的人机通讯方法。
There are essentially two types of dialogs. Predefined dialogs and custom dialogs.
- 本质上存在两种对话,预定义对话和自定义对话。
1.1. Predefined Dialogs
预定义对话
QInputDialog The QInputDialog provides a simple convenience dialog to get a single value from the user. The input value can be a string, a number or an item from a list.
- QInputDialog提供了一个简单方便的从用户处获取单一结果的对话。输入的结果可以是字符串,数字或者列表中的一项。
1 #!/usr/bin/python
2 # inputdialog.py
3 import sys
4 from PyQt4 import QtGui
5 from PyQt4 import QtCore
6 class InputDialog(QtGui.QWidget):
7 def __init__(self, parent=None):
8 QtGui.QWidget.__init__(self, parent)
9 self.setGeometry(300, 300, 350, 80)
10 self.setWindowTitle('InputDialog')
11 self.button = QtGui.QPushButton('Dialog', self)
12 self.button.setFocusPolicy(QtCore.Qt.NoFocus)
13 self.button.move(20, 20)
14 self.connect(self.button, QtCore.SIGNAL('clicked()'), self.showDialog)
15 self.setFocus()
16 self.label = QtGui.QLineEdit(self)
17 self.label.move(130, 22)
18 def showDialog(self):
19 text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
20 if ok:
21 self.label.setText(unicode(text))
22 app = QtGui.QApplication(sys.argv)
23 icon = InputDialog()
24 icon.show()
25 app.exec_()
The example has a button and a line edit widget. The button shows the input dialog for getting text values. The entered text will be displayed in the line edit widget.
- 这个例子有一个按钮和一个行编辑组件。点击按钮会生成获取文本结果的输入对话框。输入的文本将显示在行编辑组件中。
text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 'Enter your name:')
This line displays the input dialog. The first string is a dialog title, the second one is a message within the dialog. The dialog returns the entered text and a boolean value. If we clicked ok button, the boolean value is true, otherwise false.
- 这一行显示输入对话框,第一个字符串是对话框标题,第二个是对话框中的提示信息。对话框返回输入的文本和一个布尔值。如果我们点击OK按钮,布尔值为真,否则为假。
Input Dialog
Figure: Input Dialog
1.2. QColorDialog
The QColorDialog provides a dialog widget for specifying colors.
- QColorDialog提供了一个指定颜色的对话框组件。
1 #!/usr/bin/python
2 # colordialog.py
3 import sys
4 from PyQt4 import QtGui
5 from PyQt4 import QtCore
6 class ColorDialog(QtGui.QWidget):
7 def __init__(self, parent=None):
8 QtGui.QWidget.__init__(self, parent)
9 color = QtGui.QColor(0, 0, 0)
10 self.setGeometry(300, 300, 250, 180)
11 self.setWindowTitle('ColorDialog')
12 self.button = QtGui.QPushButton('Dialog', self)
13 self.button.setFocusPolicy(QtCore.Qt.NoFocus)
14 self.button.move(20, 20)
15 self.connect(self.button, QtCore.SIGNAL('clicked()'), self.showDialog)
16 self.setFocus()
17 self.widget = QtGui.QWidget(self)
18 self.widget.setStyleSheet("QWidget { background-color: %s }"
19 % color.name())
20 self.widget.setGeometry(130, 22, 100, 100)
21 def showDialog(self):
22 color = QtGui.QColorDialog.getColor()
23 self.widget.setStyleSheet("QWidget { background-color: %s }"
24 % color.name())
25 app = QtGui.QApplication(sys.argv)
26 cd = ColorDialog()
27 cd.show()
28 app.exec_()
The application example shows a push button and a QWidget. The widget background is set to black color. Using the QColorDialog, we can change it's background.
- 这个例子包含一个按钮和一个QWidget。插件的背景颜色被设置成黑色,利用QColorDialog我们可以改变它的背景颜色。
color = QtGui.QColorDialog.getColor()
This line will pop up the QColorDialog.
- 这一行弹出QColorDialog。
self.widget.setStyleSheet("QWidget { background-color: %s }" % color.name())
We change the background color using stylesheets.
- 我们用样式表改变背景颜色。
Color dialog
Figure: Color dialog
1.3. QFontDialog
The QFontDialog is a dialog widget for selecting font.
- QFontDialog用来选择字体。
1 #!/usr/bin/python
2 # fontdialog.py
3 import sys
4 from PyQt4 import QtGui
5 from PyQt4 import QtCore
6 class FontDialog(QtGui.QWidget):
7 def __init__(self, parent=None):
8 QtGui.QWidget.__init__(self, parent)
9 hbox = QtGui.QHBoxLayout()
10 self.setGeometry(300, 300, 250, 110)
11 self.setWindowTitle('FontDialog')
12 button = QtGui.QPushButton('Dialog', self)
13 button.setFocusPolicy(QtCore.Qt.NoFocus)
14 button.move(20, 20)
15 hbox.addWidget(button)
16 self.connect(button, QtCore.SIGNAL('clicked()'), self.showDialog)
17 self.label = QtGui.QLabel('Knowledge only matters', self)
18 self.label.move(130, 20)
19 hbox.addWidget(self.label, 1)
20 self.setLayout(hbox)
21 def showDialog(self):
22 font, ok = QtGui.QFontDialog.getFont()
23 if ok:
24 self.label.setFont(font)
25 app = QtGui.QApplication(sys.argv)
26 cd = FontDialog()
27 cd.show()
28 app.exec_()
In our example, we have a button and a label. With QFontDialog, we change the font of the label.
- 这个例子里,我们放志一个按钮和一个标签。利用QFontDialog我们可以修改标签的字体。
hbox.addWidget(self.label, 1)
We make the label resizable. It is necessary, because when we select a different font, the text may become larger. Otherwise the label might not be fully visible.
- 我们使得标签可以改变大小,这是必须的,因为如果我们选择一个不同的字体,文本可能会变大,标签就有可能无法完全显示所以的东西。
font, ok = QtGui.QFontDialog.getFont()
Here we pop up the font dialog.
- 这里我们弹出字体对话框。
if ok: self.label.setFont(font)
If we clicked ok, the font of the label was changed.
- 如果我们点击ok,标签的字体将会变化。
Font dialog
Figure: Font dialog
1.4. QFileDialog
The QFileDialog is a dialog that allows users to select files or directories. The files can be selected for both opening or saving.
- QFileDialog是允许用户选择文件和目录的对话框。可以为了打开和保存来选择文件。
1 #!/usr/bin/python
2 # openfiledialog.py
3 import sys
4 from PyQt4 import QtGui
5 from PyQt4 import QtCore
6 class OpenFile(QtGui.QMainWindow):
7 def __init__(self, parent=None):
8 QtGui.QMainWindow.__init__(self, parent)
9 self.setGeometry(300, 300, 350, 300)
10 self.setWindowTitle('OpenFile')
11 self.textEdit = QtGui.QTextEdit()
12 self.setCentralWidget(self.textEdit)
13 self.statusBar()
14 self.setFocus()
15 exit = QtGui.QAction(QtGui.QIcon('open.png'), 'Open', self)
16 exit.setShortcut('Ctrl+O')
17 exit.setStatusTip('Open new File')
18 self.connect(exit, QtCore.SIGNAL('triggered()'), self.showDialog)
19 menubar = self.menuBar()
20 file = menubar.addMenu('&File')
21 file.addAction(exit)
22 def showDialog(self):
23 filename = QtGui.QFileDialog.getOpenFileName(self, 'Open file',
24 '/home')
25 file=open(filename)
26 data = file.read()
27 self.textEdit.setText(data)
28 app = QtGui.QApplication(sys.argv)
29 cd = OpenFile()
30 cd.show()
31 app.exec_()
The example shows a menubar, centrally set text edit widget and a statusbar. The statusbar is shown only for desing purposes. The file menu item shows the QFileDialog which is used to select a file. The contents of the file are loaded into the text edit widget.
- 这个例子包括一个菜单栏,中间的文本编辑控件和一个状态栏。状态栏仅仅为了设计目的。file菜单弹出QFileDialog对话框用于选择一个文件,选中的文件将加载到文本编辑控件中。
class OpenFile(QtGui.QMainWindow): ... self.textEdit = QtGui.QTextEdit() self.setCentralWidget(self.textEdit)
The example is based on the QMainWindow widget, because we centrally set the text edit widget. This is easily done with the QMainWindow widget, without resorting to layouts.
- 这个例子基于QMainWindow控件,我们居中设置了文本编辑组件,无须利用布局,使用QMainWindow可以很简单的完成。
filename = QtGui.QFileDialog.getOpenFileName(self, 'Open file','/home')
We pop up the QFileDialog. The first string in the getOpenFileName method is the caption. The second string specifies the dialog working directory. By default, the file filter is set to All files (*).
- 我们弹出QFileDialog,getOpenFileName方法的第一个字串是标题,第二个字串指明了对话框的工作目录,缺省的文件过滤被设置为所有文件(*)。
file=open(filename) data = file.read() self.textEdit.setText(data)
The selected file name is read and the contents of the file are set to the text edit widget.
- 被选中的文件被读取并且显示在文本编辑组件里。
File Dialog
Figure: File dialog