-
Notifications
You must be signed in to change notification settings - Fork 0
/
21-对话框 QFontDialog.py
57 lines (40 loc) · 1.45 KB
/
21-对话框 QFontDialog.py
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
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
'''
In this example, we determine the event sender
object.
author:Administrator
datetime:2018/3/18/018 12:32
software: PyCharm
'''
import sys
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QSizePolicy, QLabel, QFontDialog, QApplication
# 在这个例子中,我们创建了一个按钮和一个标签,通过QFontDialog来改变标签的字体
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):
# 这一行代码弹出字体选择对话框,getFont()方法返回字体名称和ok参数,如果用户点击了ok他就是True,否则就是false
font, ok = QFontDialog.getFont()
# 如果我们点击了ok,标签的字体就会被改变
if ok:
self.lbl.setFont(font)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())