当前位置: 首页 > 热点 > > 内容页

世界快资讯:气轻PyQt5 31 字体对话框(QFontDialog)

发布时间:2023-06-02 11:11:50 来源:哔哩哔哩

from PyQt5.QtWidgets import *


(相关资料图)

from PyQt5.QtGui import QFont

from PyQt5.QtCore import Qt

import sys

class PyQt531(QWidget):

def __init__(self):

super().__init__()

self.font = QFont()

self.font.setFamily('宋体')

self.font.setPointSize(24)

self.initUI()

def initUI(self):

self.setWindowTitle('气轻PyQt6')                # 设置窗口标题

self.resize(320, 200)                           # 设置窗口大小

self.setStyleSheet('background-color:#FFBBFF')

self.tag = QLabel(self)                         # 设置label信息

self.tag.setGeometry(10, 50, 200, 100)         # 设置位置和大小

self.tag.setText('字体')

self.tag.setAlignment(Qt.AlignmentFlag.AlignCenter) # 横竖居中设置

self.tag.setFont(self.font)

self.btn = QPushButton('字体设置', self)

self.btn.setGeometry(210, 70, 100, 60)

self.btn.setStyleSheet('background-color:#66CDAA;color : #8B8682; \

font: bold large /"SimSun/";font-size:16px')

self.btn.clicked.connect(self.buttonClicked)

self.show()

def buttonClicked(self):

self.font, flag = QFontDialog.getFont(self.font,self,'选择字体')

if flag:

self.tag.setFont(self.font)

if __name__ == '__main__':

app = QApplication(sys.argv)

window = PyQt531()

sys.exit(app.exec_())

执行结果

参见【气轻PyQt6 31

推荐阅读