import sys
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.setup_ui()
def setup_ui(self):
self.setWindowTitle("Qt桌面应用程序")
self.resize(500, 300)
self.font = QFont()
self.font.setFamily("黑体")
self.font.setPointSize(20)
self.label = QLabel(self)
self.label.setText("选择字体")
self.label.resize(500, 200)
self.label.setStyleSheet("background-color:cyan")
self.push_button = QPushButton(self)
self.push_button.setText("show按钮")
self.push_button.move(30, 250)
self.push_button_2 = QPushButton(self)
self.push_button_2.setText("getFont按钮")
self.push_button_2.move(130, 250)
def push_button_clicked():
self.dialogue_func()
# self.font_dialogue.open()
# self.font_dialogue.exec()
self.font_dialogue.show()
self.dialogue_activities()
self.push_button.clicked.connect(push_button_clicked)
def push_button_2_clicked():
self.font_dialogue = QFontDialog(self)
self.font_result = self.font_dialogue.getFont(self.font, self, "字体选择", QFontDialog.MonospacedFonts)
self.label.setText(f"最终字体:{self.font_result[0].family()} 确定:{self.font_result[1]}")
self.push_button_2.clicked.connect(push_button_2_clicked)
def dialogue_func(self):
self.font_dialogue = QFontDialog(self.font, self)
self.font_dialogue.setWindowTitle("字体选择")
self.font_dialogue.resize(400, 200)
# self.dialogue.setModal(True)
self.font_dialogue.setWindowModality(Qt.WindowModal)
self.font_dialogue.setSizeGripEnabled(True)
# self.font_dialogue.setCurrentFont(font)
# self.font_dialogue.setOptions(QFontDialog.NoButtons | QFontDialog.MonospacedFonts)
def dialogue_activities(self):
self.font_dialogue.accepted.connect(lambda: self.label.setText(
f"接受 结果:{self.font_dialogue.result()} 最终字体:{self.font_dialogue.selectedFont().family()}"))
self.font_dialogue.rejected.connect(lambda: self.label.setText(
f"拒绝 结果:{self.font_dialogue.result()} 当前字体:{self.font_dialogue.currentFont().family()}"))
def current_fontChanged(font):
self.label.setFont(font)
self.font_dialogue.currentFontChanged.connect(current_fontChanged)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())