对话框窗口或对话框是大多数主流GUI应用不可缺少的部分。对话是两个或更多人之间的会话。在计算机应用中,对话框是一个用来和应用对话的窗口。对话框可以用来输入数据,修改数据,改变应用设置等等。
输入对话框
QInputDialog提供了一个简单便利的对话框用于从用户那儿获得只一个值。输入值可以是字符串,数字,或者一个列表中的列表项。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
#!/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_())
|
例子中有一个按钮和一个单行编辑框组件。按下按钮会显示输入对话框用于获得一个字符串值。在对话框中输入的值会在单行编辑框组件中显示。
1
2
|
text, ok
=
QInputDialog.getText(
self
,
'Input Dialog'
,
'Enter your name:'
)
|
这一行会显示一个输入对话框。第一个字符串参数是对话框的标题,第二个字符串参数是对话框内的消息文本。对话框返回输入的文本内容和一个布尔值。如果我们点击了Ok按钮,布尔值就是true,反之布尔值是false(译者注:也只有按下Ok按钮时,返回的文本内容才会有值)。
1
2
|
if
ok:
self
.le.setText(
str
(text))
|
把我们从对话框接收到的文本设置到单行编辑框组件上显示。
Figure: Input dialog
颜色选择对话框
QColorDialog类提供了一个用于选择颜色的对话框组件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
#!/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_())
|
例子中显示了一个按钮和一个QFrame。将QFrame组件的背景设置为黑色。使用颜色选择框类,我们可以改变它的颜色。
1
|
col
=
QColor(
0
,
0
,
0
)
|
初始化QtGuiQFrame组件的背景颜色。
1
|
col
=
QColorDialog.getColor()
|
这一行弹出颜色选择框。
1
2
3
|
if
col.isValid():
self
.frm.setStyleSheet(
"QWidget { background-color: %s }"
%
col.name())
|
如果我们选中一个颜色并且点了ok按钮,会返回一个有效的颜色值。如果我们点击了Cancel按钮,不会返回选中的颜色值。我们使用样式表来定义背景颜色。
字体选择框
QFontDialog是一个用于选择字体的对话框组件。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
#!/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_())
|
在我们的例子中,我们有一个按钮和一个表情。通过字体选择对话框,我们可以改变标签的字体。
1
|
font, ok
=
QFontDialog.getFont()
|
在这儿我们弹出一个字体对话框。getFont()方法返回字体名字和布尔值。如果用户点击了OK,布尔值为True;否则为False。
1
2
|
if
ok:
self
.label.setFont(font)
|
如果我们点击了Ok按钮,标签字体会被改变。
Figure: Font dialog
文件对话框
文件对话框是用于让用户选择文件或目录的对话框。可以选择文件的打开和保存。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#!/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'
)
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_())
|
示例中显示了一个菜单栏,中间设置了一个文本编辑框组件,和一个状态栏。点击菜单项会显示QtGui.QFileDialog(文件选择框)对话框,用于选择一个文件。文件的内容会被读取并在文本编辑框组件中显示。
1
2
3
4
5
6
|
class
Example(QMainWindow):
def
__init__(
self
):
super
().__init__()
self
.initUI()
|
示例基于QMainWindow组件,因为我们中心需要设置一个文本编辑框组件。
1
|
fname
=
QFileDialog.getOpenFileName(
self
,
'Open file'
,
'/home'
)
|
弹出文件选择框。第一个字符串参数是getOpenFileName()方法的标题。第二个字符串参数指定了对话框的工作目录。默认的,文件过滤器设置成All files (*)。
1
2
3
4
5
6
|
if
fname[
0
]:
f
=
open
(fname[
0
],
'r'
)
with f:
data
=
f.read()
self
.textEdit.setText(data)
|
选中文件后,读出文件的内容,并设置成文本编辑框组件的显示文本、
Figure: File dialog
这部分的PyQt5教程中,我们学习了几种对话框的使用。