forked from muziing/PyQt_practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02-QFontDialog-弹出方式、选项控制.py
62 lines (47 loc) · 1.98 KB
/
02-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
58
59
60
61
62
import sys
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("QFontDialog-功能作用")
self.resize(500, 500)
self.move(400, 250)
self.setup_ui()
def setup_ui(self):
btn = QPushButton("测试按钮", self)
btn.move(100, 100)
label = QLabel(self)
label.move(100, 200)
label.setText("https://muzing.top")
fd = QFontDialog(self)
def cfc(font):
label.setFont(font)
label.adjustSize()
fd.currentFontChanged.connect(cfc)
def font_sel():
print("字体已经被选择", fd.selectedFont().family())
# ------------选项控制-----------------
"""
QFontDialog.FontDialogOption:
QFontDialog.NoButtons 不显示“确定”和”取消“按钮。(对“实时对话框”有用)
QFontDialog.DontUseNativeDialog 在Mac上使用Qt的标准字体对话框而不是Apple的原生字体对话框
QFontDialog.ScalableFonts 显示可缩放字体
QFontDialog.NonScalableFonts 显示不可缩放的字体
QFontDialog.MonospacedFonts 显示等宽字体
QFontDialog.ProportionalFonts 显示比例字体
"""
# fd.setOption(QFontDialog.MonospacedFonts, on=True)
fd.setOptions(QFontDialog.NoButtons | QFontDialog.MonospacedFonts)
print(fd.testOption(QFontDialog.NoButtons))
print(fd.testOption(QFontDialog.DontUseNativeDialog))
# -------------弹出方式------------------
btn.clicked.connect(
lambda: fd.open(font_sel)
) # 在open方法中传入一个槽函数,则打开FontDialog后自动连接
# if fd.exec(): # 用户点击确定键则返回1,取消键和关闭窗口返回0
# print(fd.selectedFont().family())
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())