0

I'm trying to create widgets and place it on main window. Calling self.newlabel1() directly from initUI method works fine. Calling self.newlabel2() from timer event handler doesn't work (label 2 doesn't appear). What am I doing wrong?

from PyQt5.QtWidgets import QWidget, QApplication, QLabel
from PyQt5.QtCore import Qt, QBasicTimer

win_width, win_height = 400, 400
win_x, win_y = 200, 200
txt_title = "App"

class MainWindow(QWidget):
    def __init__(self, parent=None, flags=Qt.WindowFlags()):
        super().__init__(parent=parent, flags=flags)
        self.initUI()
        self.set_appear()
        self.show()

    def initUI(self):
        self.q0 = QLabel("Label 0",self)
        self.q0.move(200,100)
        self.newlabel1()
        self.timer = QBasicTimer()
        self.timer.start(200, self)

    def newlabel1(self):
        self.q1 = QLabel("Label 1",self)
        self.q1.move(200,150)

    def newlabel2(self):
        self.q2 = QLabel("Label 2",self)
        self.q2.move(200,200)

    def timerEvent(self, event):
        self.newlabel2()
        self.timer.stop()

    def set_appear(self):
        self.setWindowTitle(txt_title)
        self.resize(win_width, win_height)
        self.move(win_x, win_y)

def main():
    app = QApplication([])
    mw = MainWindow()
    app.exec_()
main()

Screenshot

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

0 Answers0