简体   繁体   English

pyqt qlabel仅显示字符串的第一个字符

[英]pyqt qlabel displays only first char of string

I want to display some string in QLabel on button click but my code only shows first char of the string. 我想在按钮单击时在QLabel中显示一些字符串,但是我的代码仅显示字符串的第一个字符。 I am using following code for this 我为此使用以下代码

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")

In above code I want to set qlable as "hello" but only 'h' is displayed. 在上面的代码中,我想将qlable设置为“ hello”,但仅显示“ h”。 What is the reason for this and how can I correct my code? 这是什么原因,如何纠正我的代码?

The problem is caused because the size of the QLabel initially depends on the content of the text, and since this empty initially only takes the necessary width for a letter, the solution is to call the method adjustSize() . 引起此问题的原因是, QLabel的大小最初取决于文本的内容,并且由于此空白最初仅占用字母的必要宽度,因此解决方案是调用方法adjustSize()

def test3(self, data):
    self.SecondWindow.emailIDIN.setText("hello")
    self.SecondWindow.emailIDIN.adjustSize()

Note: It is not advisable to have a variable that is named as a class since it can cause errors. 注意:建议不要将变量命名为类,因为它可能导致错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM