Skip to content Skip to sidebar Skip to footer

With Pyqt5, Implement Two Windows Looping Forever Automatically

Using PyQt5, I want to implement a two windows displaying one after another automatically, without the user interacting with any window. Something like this: While True: Show W

Solution 1:

If you are going to work with Qt then you should forget about sequential logic but you have to implement the logic using events. For example, in your case you want one window to be shown every time T and another to be hidden, so that can be implemented with a QTimer and a flag:

import sys
from PyQt5.QtCore import QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5 import uic


classWin1(QMainWindow):
    def__init__(self):
        super(Win1, self).__init__()
        uic.loadUi('win1.ui', self)
        self.show()
        

classWin2(QMainWindow):
    def__init__(self):
        super(Win2, self).__init__()
        uic.loadUi('win2.ui', self)
        self.show()


if __name__ == "__main__":

    app = QApplication(sys.argv)

    timer = QTimer()
    timer.setProperty("flag", True)

    win1 = Win1()
    win2 = Win2()

    defon_timeout():
        flag = timer.property("flag")
        if flag:
            win1.show()
            win2.close()
        else:
            win2.show()
            win1.close()
        timer.setProperty("flag", not flag)

    timer.timeout.connect(on_timeout)
    timer.start(1 * 1000)
    on_timeout()

    app.exec_()

You should not use while loop or time.sleep since they block the eventloop in which the GUI lives, that is: they freeze the windows

Post a Comment for "With Pyqt5, Implement Two Windows Looping Forever Automatically"