Search code examples
pythonpyqtpyqt5signalssignals-slots

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 Window1    
    wait 2 seconds
    Close Window1
    Show Window2
    wait 2 seconds
    Close Window2

The problem I am having is that the main UI thread is stuck in app.exec_() function, so it cannot implement the opening and closing logic.

import sys
from PyQt5.QtWidgets import *
from PyQt5 import uic


class Win1(QMainWindow):
    def __init__(self):
        super(Win1, self).__init__()
        uic.loadUi('win1.ui', self)
        self.show()
        
class Win2(QMainWindow):
    def __init__(self):
        super(Win2, self).__init__()
        uic.loadUi('win2.ui', self)
        self.show()

if __name__ == '__main__':

    app = QApplication(sys.argv)

    while True:
        win = Win1()
        time.sleep(1) 
        win.close()
        
        win = Win2()
        time.sleep(1)
        win.close()
        
        app.exec_() # <--------- Program blocks here

I would appreciate if someone can share a minimal example for this working without blocking. Or please point to the mechanism that should be used.


Solution

  • 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
    
    
    class Win1(QMainWindow):
        def __init__(self):
            super(Win1, self).__init__()
            uic.loadUi('win1.ui', self)
            self.show()
            
    
    class Win2(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()
    
        def on_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