PyQt GUI内のQLabel()
のフォントサイズがそれほど一貫していないことに気付きました。以下の例を見てみましょう。私は簡単なテストのための完全なpythonファイルを書いた。このコードを実行した場合、これはあなたが得るものです短いHTMLテキストを挿入するとQLabelフォントが異なる
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
:それは2つのラベルでQtのウィンドウをポップアップ表示されます
はなぜ両方のラベルのフォントサイズを等しくありませんか?完全のために
、これが私のシステムである:
- オペレーティングシステム:Windows 10、64ビット
- Pythonのバージョン:v3の(アナコンダパッケージ)
- Qtのバージョン:PyQt5
ありがとうございました:-) –
私は、他の答えに適切な編集を加えました。このように、 'size'属性の誤解が明らかになりました。問題の根本が本当に何であるかを理解するのを手伝ってくれてありがとう。 –