gpt4 book ai didi

python - 插入短 HTML 文本时 QLabel 字体不同

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:09 25 4
gpt4 key购买 nike

我注意到 PyQt GUI 中 QLabel() 的字体大小不是很一致。让我们看看下面的例子。我已经编写了一个完整的 python 文件用于快速测试。它将弹出一个带有两个标签的 Qt 窗口:

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
import os

'''---------------------------------------------------------------------'''
''' '''
''' T E S T I N G '''
''' '''
'''---------------------------------------------------------------------'''


class TestWindow(QMainWindow):
def __init__(self):
super(TestWindow, self).__init__()

# 1. Set basic geometry and color.
# --------------------------------
self.setGeometry(100, 100, 800, 600)
self.setWindowTitle('Hello World')
palette = QPalette()
palette.setColor(QPalette.Window, QColor(200,200,200))
self.setPalette(palette)
self.show()

# 2. Create the central frame.
# ----------------------------
self.centralFrame = QFrame(self)
self.centralFrame.setFrameShape(QFrame.NoFrame)
self.centralLayout = QVBoxLayout()
self.centralFrame.setLayout(self.centralLayout)
self.centralLayout.setSpacing(5)
self.centralLayout.setContentsMargins(20,20,20,20)
self.setCentralWidget(self.centralFrame)

# 3. Do the test.
# ----------------
# TEST CASE 1
# The label with html
self.infoLbl = QLabel()
self.infoLbl.setTextFormat(Qt.RichText)
self.infoLbl.setFrameShape(QFrame.StyledPanel)
self.infoTxt = \
'<html> \
<head> \
</head> \
<body> \
<font size="10"> \
<p style="margin-left:8px;">My html text, font = 10pt</p> \
</font> \
</body> \
</html> '
self.infoLbl.setText(self.infoTxt)
self.infoLbl.setMaximumHeight(50)
self.infoLbl.setMaximumWidth(500)

# TEST CASE 2
# The label with a normal string
self.normalLbl = QLabel()
self.normalLbl.setFrameShape(QFrame.StyledPanel)
self.normalLbl.setText('My normal text, font = 10pt')
font = QFont()
font.setFamily("Arial")
font.setPointSize(10)
self.normalLbl.setFont(font)
self.normalLbl.setMaximumHeight(50)
self.normalLbl.setMaximumWidth(500)

# 4. Add both labels to the central layout.
# ------------------------------------------
self.centralLayout.addWidget(self.infoLbl) # <- The label with html snippet
self.centralLayout.addWidget(self.normalLbl) # <- The normal label
self.centralLayout.addWidget(QLabel()) # <- Just a spacer


if __name__== '__main__':
app = QApplication(sys.argv)
QApplication.setStyle(QStyleFactory.create('Fusion'))
testWindow = TestWindow()
app.exec_()
app = None

如果你运行这段代码,你会得到:

Font size issue in QLabel

为什么两个标签中的字体大小不相等?

为了完整起见,这是我的系统:

  • 操作系统:Windows 10,64 位
  • Python 版本:v3(anaconda 包)
  • Qt版本:PyQt5

最佳答案

Qt4 和 Qt5 仍然完全支持 font 标签。它已被当前的 HTML 标准淘汰的事实无关紧要,因为 Qt 只支持 a limited subset of HTML4。 .

该示例的真正问题是由于对 size 属性的误解。这没有指定 大小。相反,它指定 (a) 1-7 范围内的固定值,或 (b) 相对于 basefont 大小的加/减值。所以它所能做的就是使字体“变小”或“变大”,具体结果将完全取决于渲染引擎。

要设置精确 点大小,请使用 css font-size 属性:

<p style="font-size:10pt">My html text, font = 10pt</p>

注意:请参阅 CSS Properties Table获取 Qt 的富文本引擎支持的所有 css 属性的列表。

关于python - 插入短 HTML 文本时 QLabel 字体不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39335955/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com