PyQt: 练习 QFontDialogPyQt: 练习 QFontDialogPyQt: 练习 QFontDialogPyQt: 练习 QFontDialog
  • 首页
  • 博客
  • 书签
  • 文件
  • 分析
  • 登录

PyQt: 练习 QFontDialog

发表 admin at 2022年4月15日
类别
  • Practice
标签
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_())

发表回复 取消回复

要发表评论,您必须先登录。

类别

  • Cat
  • Python
  • Django
  • Database
  • Html/CSS
  • JavaScript
  • Vue
  • RegExp
  • Maths/AI
  • PHP/Wordpress
  • Practice
  • Linux
  • Windows
  • Android
  • NAS
  • Software
  • Hardware
  • Network
  • SEO
  • English
  • Games
  • Recipes
  • General
  • Memorandum
  • Essays
  • 未分类

归档

©2015-2023 艾丽卡 Blog support@alaica.com
      ajax-loader