import string
import sys
import traceback
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class Main(QDialog):
    """定时器Demo"""
    def __init__(self, parent=None):
        try:
            super(Main, self).__init__(parent)
            self.init_ui()
            self.init_slot()
        except:
            traceback.print_exc()

    def init_ui(self):
        try:
            self.resize(480, 240)
            self.setWindowTitle("字体选择demo")
            self.setFixedSize(self.size())

            # 文本输入框
            self.label = QLabel(self)
            self.label.setText("请点击字体选择按钮")
            self.label.setAlignment(Qt.AlignCenter)

            # 选择按钮
            self.pushButton = QPushButton(self)
            self.pushButton.setText("选择")

            # 布局
            layout = QHBoxLayout()
            layout.addWidget(self.label)
            layout.addWidget(self.pushButton)
            layout.setStretch(0, 1)
            layout.setStretch(1, 1)

            self.setLayout(layout)
        except:
            traceback.print_exc()

    def init_slot(self):
        try:
            self.pushButton.clicked.connect(self.select_font)
        except:
            traceback.print_exc()

    def select_font(self):
        try:
            font, ok = QFontDialog.getFont()
            if ok:
                self.label.setText("字形: {}, 大小: {}".format(font.family(), font.pointSize()))
                self.label.setFont(font)
        except:
            traceback.print_exc()


if __name__ == '__main__':
    authorization = None
    app = QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())
