[英]pyqt qlabel displays only first char of string
我想在按鈕單擊時在QLabel中顯示一些字符串,但是我的代碼僅顯示字符串的第一個字符。 我為此使用以下代碼
class UITesterWindow(QWidget):
def __init__(self, parent=None):
super(UITesterWindow, self).__init__(parent)
self.test2 = QPushButton("Test1", self)
self.test2.setGeometry(10, 360, 200, 30)
self.test2.setStyleSheet("font: bold 12pt Courier")
self.emailIDIN = QtGui.QLabel(self)
self.emailIDIN.setStyleSheet("font: bold 18pt Courier")
self.emailIDIN.move(420, 170)
class SecondWindow(QMainWindow):
def __init__(self, parent=None):
super(SecondWindow, self).__init__(parent)
self.setGeometry(50, 50, 400, 450)
self.showMaximized()
self.testerEvent()
def testerEvent(self):
self.SecondWindow = UITesterWindow(self)
self.setCentralWidget(self.SecondWindow)
self.SecondWindow.test2.clicked.connect(lambda: self.test3("data"))
self.show()
def test3(self, data):
self.SecondWindow.emailIDIN.setText("hello")
在上面的代碼中,我想將qlable設置為“ hello”,但僅顯示“ h”。 這是什么原因,如何糾正我的代碼?
引起此問題的原因是, QLabel
的大小最初取決於文本的內容,並且由於此空白最初僅占用字母的必要寬度,因此解決方案是調用方法adjustSize()
。
def test3(self, data):
self.SecondWindow.emailIDIN.setText("hello")
self.SecondWindow.emailIDIN.adjustSize()
注意:建議不要將變量命名為類,因為它可能導致錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.