Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. show() and exec_() behavior difference
QtWS25 Last Chance

show() and exec_() behavior difference

Scheduled Pinned Locked Moved Unsolved Qt for Python
7 Posts 4 Posters 4.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Junhi
    wrote on last edited by
    #1

    Hello.
    I have spent many time for solving the problem.

    I made code that display QDialog window when a condition meet.

    Here is part of code

        def display_message(self):
            global result_display
    
            self.Dialog = QtWidgets.QDialog()
            self.dm = Warning_Window(result_display)
            self.dm.setupUi(self.Dialog)
            self.Dialog.exec_()
    

    When I use self.Dialog.exec_(), QDialog window displays
    but in case of self.Dialog.show(), my program does not response

    Below is code
    main.py

    class Form(QMainWindow, Ui_MainWindow):
        def __init__(self, parent=None):
            super().__init__()
            self.setupUi(self)
            self.show()
    
        @pyqtSlot()
        def display_message(self):
            global result_display
    
            self.Dialog = QtWidgets.QDialog()
            self.dm = Warning_Window(result_display)
            self.dm.setupUi(self.Dialog)
            self.Dialog.exec_()
    
    
        def discover_sleeping_cell(self):
    
            global order
            global result_display
    
    
                if (len(result_display) != 0):
    
                    p1 = Thread(target=self.display_message)
                    p1.start()
    
    
            cursor.close()
            conn.close()
            fp_sleeping_cell.close()
    
            order = order + 1
            if order > 32:
                pass
            else :
                self.wait_10min()
    
    
        def wait_10min(self):
            global order
    
            t = threading.Timer(1.0,self.discover_sleeping_cell)
            t.start()
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        global w
        w = Form()
    
        file_list = []
        order = 0
        w.wait_10min()
    
        sys.exit(app.exec_())
    

    warning_window.py

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'C:\Users\think\Documents\monitor\warning_window.ui'
    #
    # Created by: PyQt5 UI code generator 5.13.0
    #
    # WARNING! All changes made in this file will be lost!
    
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    
    class Warning_Window(object):
    
        def __init__(self,msg):
            global list_of_sleeping_cell
    
            list_of_sleeping_cell = msg
            if list_of_sleeping_cell == 'none':
                pass
            print(list_of_sleeping_cell)
    
        def setupUi(self, dialog):
    
            dialog.setObjectName("Dialog")
            dialog.resize(500, 400)
            self.horizontalLayout = QtWidgets.QHBoxLayout(dialog)
            self.horizontalLayout.setObjectName("horizontalLayout")
    
            self.warn_sleeping = QtWidgets.QTextBrowser(dialog)
    
            self.warn_sleeping.setFrameShape(QtWidgets.QFrame.StyledPanel)
            self.warn_sleeping.setFrameShadow(QtWidgets.QFrame.Plain)
            self.warn_sleeping.setLineWidth(3)
            self.warn_sleeping.setMidLineWidth(1)
    
            self.warn_sleeping.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
            self.warn_sleeping.setObjectName("warn_sleeping")
            self.horizontalLayout.addWidget(self.warn_sleeping)
    
            self.retranslateUi(dialog)
            QtCore.QMetaObject.connectSlotsByName(dialog)
    
            ################################################
            lenth_sleeping_cell_list = len(list_of_sleeping_cell)
    
            self.warn_sleeping.setText("Message")
    
    
            ################################################
    
        def retranslateUi(self, dialog):
            _translate = QtCore.QCoreApplication.translate
            dialog.setWindowTitle(_translate("Dialog", "Dialog"))
            self.warn_sleeping.setText(_translate("Dialog", "TextLabel"))
    
    
    if __name__ == "__main__":
        import sys
    
        app = QtWidgets.QApplication(sys.argv)
        Dialog = QtWidgets.QDialog()
        ui = Warning_Window()
        ui.setupUi(Dialog)
        Dialog.show()
        sys.exit(app.exec_())
    
    
    JonBJ 1 Reply Last reply
    0
    • J Junhi

      Hello.
      I have spent many time for solving the problem.

      I made code that display QDialog window when a condition meet.

      Here is part of code

          def display_message(self):
              global result_display
      
              self.Dialog = QtWidgets.QDialog()
              self.dm = Warning_Window(result_display)
              self.dm.setupUi(self.Dialog)
              self.Dialog.exec_()
      

      When I use self.Dialog.exec_(), QDialog window displays
      but in case of self.Dialog.show(), my program does not response

      Below is code
      main.py

      class Form(QMainWindow, Ui_MainWindow):
          def __init__(self, parent=None):
              super().__init__()
              self.setupUi(self)
              self.show()
      
          @pyqtSlot()
          def display_message(self):
              global result_display
      
              self.Dialog = QtWidgets.QDialog()
              self.dm = Warning_Window(result_display)
              self.dm.setupUi(self.Dialog)
              self.Dialog.exec_()
      
      
          def discover_sleeping_cell(self):
      
              global order
              global result_display
      
      
                  if (len(result_display) != 0):
      
                      p1 = Thread(target=self.display_message)
                      p1.start()
      
      
              cursor.close()
              conn.close()
              fp_sleeping_cell.close()
      
              order = order + 1
              if order > 32:
                  pass
              else :
                  self.wait_10min()
      
      
          def wait_10min(self):
              global order
      
              t = threading.Timer(1.0,self.discover_sleeping_cell)
              t.start()
      
      
      if __name__ == '__main__':
          app = QApplication(sys.argv)
          global w
          w = Form()
      
          file_list = []
          order = 0
          w.wait_10min()
      
          sys.exit(app.exec_())
      

      warning_window.py

      # -*- coding: utf-8 -*-
      
      # Form implementation generated from reading ui file 'C:\Users\think\Documents\monitor\warning_window.ui'
      #
      # Created by: PyQt5 UI code generator 5.13.0
      #
      # WARNING! All changes made in this file will be lost!
      
      
      from PyQt5 import QtCore, QtGui, QtWidgets
      
      
      class Warning_Window(object):
      
          def __init__(self,msg):
              global list_of_sleeping_cell
      
              list_of_sleeping_cell = msg
              if list_of_sleeping_cell == 'none':
                  pass
              print(list_of_sleeping_cell)
      
          def setupUi(self, dialog):
      
              dialog.setObjectName("Dialog")
              dialog.resize(500, 400)
              self.horizontalLayout = QtWidgets.QHBoxLayout(dialog)
              self.horizontalLayout.setObjectName("horizontalLayout")
      
              self.warn_sleeping = QtWidgets.QTextBrowser(dialog)
      
              self.warn_sleeping.setFrameShape(QtWidgets.QFrame.StyledPanel)
              self.warn_sleeping.setFrameShadow(QtWidgets.QFrame.Plain)
              self.warn_sleeping.setLineWidth(3)
              self.warn_sleeping.setMidLineWidth(1)
      
              self.warn_sleeping.setAlignment(QtCore.Qt.AlignLeading|QtCore.Qt.AlignLeft|QtCore.Qt.AlignTop)
              self.warn_sleeping.setObjectName("warn_sleeping")
              self.horizontalLayout.addWidget(self.warn_sleeping)
      
              self.retranslateUi(dialog)
              QtCore.QMetaObject.connectSlotsByName(dialog)
      
              ################################################
              lenth_sleeping_cell_list = len(list_of_sleeping_cell)
      
              self.warn_sleeping.setText("Message")
      
      
              ################################################
      
          def retranslateUi(self, dialog):
              _translate = QtCore.QCoreApplication.translate
              dialog.setWindowTitle(_translate("Dialog", "Dialog"))
              self.warn_sleeping.setText(_translate("Dialog", "TextLabel"))
      
      
      if __name__ == "__main__":
          import sys
      
          app = QtWidgets.QApplication(sys.argv)
          Dialog = QtWidgets.QDialog()
          ui = Warning_Window()
          ui.setupUi(Dialog)
          Dialog.show()
          sys.exit(app.exec_())
      
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @Junhi said in show() and exec_() behavior difference:

      but in case of self.Dialog.show(), my program does not response

      Don't know what that means.

      show() and exec() work as documented, whether in C++ or Python.

      p1 = Thread(target=self.display_message): what are you doing trying to use threads here, especially with Python? Further, you are trying to make the thread execute a Qt UI behaviour, which is absolutely forbidden in Qt (UI stuff can only be executed in the main GUI thread), and could easily be your issue.

      Why does your warning_window.py have all the if __name__ == "__main__": code, it's just confusing?

      So start by sorting out your code, in particular remove any thread stuff, and see where you are then.

      J 1 Reply Last reply
      1
      • JonBJ JonB

        @Junhi said in show() and exec_() behavior difference:

        but in case of self.Dialog.show(), my program does not response

        Don't know what that means.

        show() and exec() work as documented, whether in C++ or Python.

        p1 = Thread(target=self.display_message): what are you doing trying to use threads here, especially with Python? Further, you are trying to make the thread execute a Qt UI behaviour, which is absolutely forbidden in Qt (UI stuff can only be executed in the main GUI thread), and could easily be your issue.

        Why does your warning_window.py have all the if __name__ == "__main__": code, it's just confusing?

        So start by sorting out your code, in particular remove any thread stuff, and see where you are then.

        J Offline
        J Offline
        Junhi
        wrote on last edited by
        #3

        @JonB Thank you for your feedback

        1. "but in case of self.Dialog.show(), my program does not response" means
          When I use self.Dialog.exec_() at 5th line in the display_message() method,
          I can see QDialog window pop-up.
          But when I use self.Dialog.show() instead of self.Dialog.exec_(), QDialog window does not pop-up and main program got hang(no response).

        2. p1 = Thread(target=self.display_message): could easily be your issue.
          I have changed the code like below, but same result

                   if (len(result_display) != 0):
        
        #                p1 = Thread(target=self.display_message)
        #                p1.start()
                        self.display_message()
        
        1. ave all the if name == "main": code, it's just confusing?
          Yes, it is just confusing. This is my first GUI code.
          So difficult than non GUI programming. -_-;;
          What is your recommendation?
        jsulmJ RokeJulianLockhartR 2 Replies Last reply
        0
        • J Junhi

          @JonB Thank you for your feedback

          1. "but in case of self.Dialog.show(), my program does not response" means
            When I use self.Dialog.exec_() at 5th line in the display_message() method,
            I can see QDialog window pop-up.
            But when I use self.Dialog.show() instead of self.Dialog.exec_(), QDialog window does not pop-up and main program got hang(no response).

          2. p1 = Thread(target=self.display_message): could easily be your issue.
            I have changed the code like below, but same result

                     if (len(result_display) != 0):
          
          #                p1 = Thread(target=self.display_message)
          #                p1.start()
                          self.display_message()
          
          1. ave all the if name == "main": code, it's just confusing?
            Yes, it is just confusing. This is my first GUI code.
            So difficult than non GUI programming. -_-;;
            What is your recommendation?
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Junhi Why do you have

          if __name__ == "__main__":
              import sys
          
              app = QtWidgets.QApplication(sys.argv)
              Dialog = QtWidgets.QDialog()
              ui = Warning_Window()
              ui.setupUi(Dialog)
              Dialog.show()
              sys.exit(app.exec_())
          

          in warning_window.py ?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          J 1 Reply Last reply
          0
          • jsulmJ jsulm

            @Junhi Why do you have

            if __name__ == "__main__":
                import sys
            
                app = QtWidgets.QApplication(sys.argv)
                Dialog = QtWidgets.QDialog()
                ui = Warning_Window()
                ui.setupUi(Dialog)
                Dialog.show()
                sys.exit(app.exec_())
            

            in warning_window.py ?

            J Offline
            J Offline
            Junhi
            wrote on last edited by
            #5

            @jsulm Hmmm.. I don't know why.
            Cause warning_window.py is from QT Designer.
            I just design by using QT Design and convert .ui to .py

            J 1 Reply Last reply
            0
            • J Junhi

              @jsulm Hmmm.. I don't know why.
              Cause warning_window.py is from QT Designer.
              I just design by using QT Design and convert .ui to .py

              J Offline
              J Offline
              Junhi
              wrote on last edited by
              #6

              @Junhi said in show() and exec_() behavior difference:

              @jsulm Hmmm.. I don't know why.
              Cause warning_window.py is from QT Designer.
              I just design by using QT Design and convert .ui to .py

              I had just removed main and had test again. but Same result

              #if __name__ == "__main__":
              #    import sys
              
              #    app = QtWidgets.QApplication(sys.argv)
              #    Dialog = QtWidgets.QDialog()
              #    ui = Warning_Window()
              #    ui.setupUi(Dialog)
              #    Dialog.show()
              #    sys.exit(app.exec_())
              
              
              1 Reply Last reply
              0
              • J Junhi

                @JonB Thank you for your feedback

                1. "but in case of self.Dialog.show(), my program does not response" means
                  When I use self.Dialog.exec_() at 5th line in the display_message() method,
                  I can see QDialog window pop-up.
                  But when I use self.Dialog.show() instead of self.Dialog.exec_(), QDialog window does not pop-up and main program got hang(no response).

                2. p1 = Thread(target=self.display_message): could easily be your issue.
                  I have changed the code like below, but same result

                           if (len(result_display) != 0):
                
                #                p1 = Thread(target=self.display_message)
                #                p1.start()
                                self.display_message()
                
                1. ave all the if name == "main": code, it's just confusing?
                  Yes, it is just confusing. This is my first GUI code.
                  So difficult than non GUI programming. -_-;;
                  What is your recommendation?
                RokeJulianLockhartR Offline
                RokeJulianLockhartR Offline
                RokeJulianLockhart
                wrote on last edited by
                #7

                @Junhi,

                if __name__ == "__main__":
                

                prevents invocation as module, instead only as script.

                When using a forum, remember to tag the person you are responding to, in case they are not subscribed to the thread.

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved