PyQt5: Child window is not displayed until while loop is executed












1














I have a main class that displays a window with a push button. On clicking the push button calls a child class (that contains while loop) that displays "Yay" in a separate window. I am not sure why the child window is displayed after executing the while loop. Attached is the code for your reference. Any help is much appreciated.



class test1(QtWidgets.QMainWindow):

def __init__(self):
super (test1, self).__init__()
test1.setGeometry(self, 20, 20, 400, 250)

btn = QtWidgets.QPushButton("Add", self)
btn.clicked.connect(self.Check)
btn.resize(btn.sizeHint())
btn.move(10, 170)
self.show()

def Check(self):
self.test2=test2()



class test2(QtWidgets.QMainWindow):

def __init__(self):
super (test2, self).__init__()

test2.setGeometry(self,20, 20, 450, 100)
self.label = QtWidgets.QLabel("Yay",self)
self.show()
a=50
b=60
x=True
time.sleep(5)
while (x):
if a!=b:
a+=1
else:
x=False
def run():
app = QtWidgets.QApplication(sys.argv)
GUI = test1()
GUI.show()
sys.exit(app.exec_())

run()









share|improve this question



























    1














    I have a main class that displays a window with a push button. On clicking the push button calls a child class (that contains while loop) that displays "Yay" in a separate window. I am not sure why the child window is displayed after executing the while loop. Attached is the code for your reference. Any help is much appreciated.



    class test1(QtWidgets.QMainWindow):

    def __init__(self):
    super (test1, self).__init__()
    test1.setGeometry(self, 20, 20, 400, 250)

    btn = QtWidgets.QPushButton("Add", self)
    btn.clicked.connect(self.Check)
    btn.resize(btn.sizeHint())
    btn.move(10, 170)
    self.show()

    def Check(self):
    self.test2=test2()



    class test2(QtWidgets.QMainWindow):

    def __init__(self):
    super (test2, self).__init__()

    test2.setGeometry(self,20, 20, 450, 100)
    self.label = QtWidgets.QLabel("Yay",self)
    self.show()
    a=50
    b=60
    x=True
    time.sleep(5)
    while (x):
    if a!=b:
    a+=1
    else:
    x=False
    def run():
    app = QtWidgets.QApplication(sys.argv)
    GUI = test1()
    GUI.show()
    sys.exit(app.exec_())

    run()









    share|improve this question

























      1












      1








      1







      I have a main class that displays a window with a push button. On clicking the push button calls a child class (that contains while loop) that displays "Yay" in a separate window. I am not sure why the child window is displayed after executing the while loop. Attached is the code for your reference. Any help is much appreciated.



      class test1(QtWidgets.QMainWindow):

      def __init__(self):
      super (test1, self).__init__()
      test1.setGeometry(self, 20, 20, 400, 250)

      btn = QtWidgets.QPushButton("Add", self)
      btn.clicked.connect(self.Check)
      btn.resize(btn.sizeHint())
      btn.move(10, 170)
      self.show()

      def Check(self):
      self.test2=test2()



      class test2(QtWidgets.QMainWindow):

      def __init__(self):
      super (test2, self).__init__()

      test2.setGeometry(self,20, 20, 450, 100)
      self.label = QtWidgets.QLabel("Yay",self)
      self.show()
      a=50
      b=60
      x=True
      time.sleep(5)
      while (x):
      if a!=b:
      a+=1
      else:
      x=False
      def run():
      app = QtWidgets.QApplication(sys.argv)
      GUI = test1()
      GUI.show()
      sys.exit(app.exec_())

      run()









      share|improve this question













      I have a main class that displays a window with a push button. On clicking the push button calls a child class (that contains while loop) that displays "Yay" in a separate window. I am not sure why the child window is displayed after executing the while loop. Attached is the code for your reference. Any help is much appreciated.



      class test1(QtWidgets.QMainWindow):

      def __init__(self):
      super (test1, self).__init__()
      test1.setGeometry(self, 20, 20, 400, 250)

      btn = QtWidgets.QPushButton("Add", self)
      btn.clicked.connect(self.Check)
      btn.resize(btn.sizeHint())
      btn.move(10, 170)
      self.show()

      def Check(self):
      self.test2=test2()



      class test2(QtWidgets.QMainWindow):

      def __init__(self):
      super (test2, self).__init__()

      test2.setGeometry(self,20, 20, 450, 100)
      self.label = QtWidgets.QLabel("Yay",self)
      self.show()
      a=50
      b=60
      x=True
      time.sleep(5)
      while (x):
      if a!=b:
      a+=1
      else:
      x=False
      def run():
      app = QtWidgets.QApplication(sys.argv)
      GUI = test1()
      GUI.show()
      sys.exit(app.exec_())

      run()






      python pyqt5






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 21 at 4:25









      Krish

      82




      82
























          1 Answer
          1






          active

          oldest

          votes


















          0














          The GUI lives in an event-loop, this a loop that allows to obtain data of diverse elements like the keyboard, the mouse, the logic, etc and to update the status of the GUI, so if the GUI is frozen it will not allow to do its I work correctly. And in this case the one that blocks the event-loop is the sleep, the while does not intervene since it is executed in less than 1 ms.



          So the tasks that take up a lot of time must be executed in a different thread of the GUI avoiding that it is blocked, but in your case the sleep can be replaced by a QTimer.



          In conclusion, the sleep is freezing the GUI. I see that the code has an educational purpose so I will show you several options:



          threading.Thread():



          import threading

          # ...

          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()

          threading.Thread(target=self.foo, daemon=True).start()

          def foo(self):
          time.sleep(5)
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          QTimer + QEventLoop:



          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()
          loop = QtCore.QEventLoop()
          QtCore.QTimer.singleShot(5*1000, loop.quit)
          loop.exec_()
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          QTimer:



          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()
          QtCore.QTimer.singleShot(5*1000, self.foo)

          def foo(self):
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          As a general conclusion, the tasks executed in the thread of the GUI should consume a time less than 30ms, if it is greater than that it is recommended to execute it in another thread. In the case that each task within the loop fulfills the above but there are many iterations, a QTimer can be used to perform the tasks of the loop periodically.






          share|improve this answer





















          • it worked. Thank you for the detailed explanation.
            – Krish
            Nov 21 at 5:18











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405246%2fpyqt5-child-window-is-not-displayed-until-while-loop-is-executed%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          The GUI lives in an event-loop, this a loop that allows to obtain data of diverse elements like the keyboard, the mouse, the logic, etc and to update the status of the GUI, so if the GUI is frozen it will not allow to do its I work correctly. And in this case the one that blocks the event-loop is the sleep, the while does not intervene since it is executed in less than 1 ms.



          So the tasks that take up a lot of time must be executed in a different thread of the GUI avoiding that it is blocked, but in your case the sleep can be replaced by a QTimer.



          In conclusion, the sleep is freezing the GUI. I see that the code has an educational purpose so I will show you several options:



          threading.Thread():



          import threading

          # ...

          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()

          threading.Thread(target=self.foo, daemon=True).start()

          def foo(self):
          time.sleep(5)
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          QTimer + QEventLoop:



          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()
          loop = QtCore.QEventLoop()
          QtCore.QTimer.singleShot(5*1000, loop.quit)
          loop.exec_()
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          QTimer:



          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()
          QtCore.QTimer.singleShot(5*1000, self.foo)

          def foo(self):
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          As a general conclusion, the tasks executed in the thread of the GUI should consume a time less than 30ms, if it is greater than that it is recommended to execute it in another thread. In the case that each task within the loop fulfills the above but there are many iterations, a QTimer can be used to perform the tasks of the loop periodically.






          share|improve this answer





















          • it worked. Thank you for the detailed explanation.
            – Krish
            Nov 21 at 5:18
















          0














          The GUI lives in an event-loop, this a loop that allows to obtain data of diverse elements like the keyboard, the mouse, the logic, etc and to update the status of the GUI, so if the GUI is frozen it will not allow to do its I work correctly. And in this case the one that blocks the event-loop is the sleep, the while does not intervene since it is executed in less than 1 ms.



          So the tasks that take up a lot of time must be executed in a different thread of the GUI avoiding that it is blocked, but in your case the sleep can be replaced by a QTimer.



          In conclusion, the sleep is freezing the GUI. I see that the code has an educational purpose so I will show you several options:



          threading.Thread():



          import threading

          # ...

          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()

          threading.Thread(target=self.foo, daemon=True).start()

          def foo(self):
          time.sleep(5)
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          QTimer + QEventLoop:



          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()
          loop = QtCore.QEventLoop()
          QtCore.QTimer.singleShot(5*1000, loop.quit)
          loop.exec_()
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          QTimer:



          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()
          QtCore.QTimer.singleShot(5*1000, self.foo)

          def foo(self):
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          As a general conclusion, the tasks executed in the thread of the GUI should consume a time less than 30ms, if it is greater than that it is recommended to execute it in another thread. In the case that each task within the loop fulfills the above but there are many iterations, a QTimer can be used to perform the tasks of the loop periodically.






          share|improve this answer





















          • it worked. Thank you for the detailed explanation.
            – Krish
            Nov 21 at 5:18














          0












          0








          0






          The GUI lives in an event-loop, this a loop that allows to obtain data of diverse elements like the keyboard, the mouse, the logic, etc and to update the status of the GUI, so if the GUI is frozen it will not allow to do its I work correctly. And in this case the one that blocks the event-loop is the sleep, the while does not intervene since it is executed in less than 1 ms.



          So the tasks that take up a lot of time must be executed in a different thread of the GUI avoiding that it is blocked, but in your case the sleep can be replaced by a QTimer.



          In conclusion, the sleep is freezing the GUI. I see that the code has an educational purpose so I will show you several options:



          threading.Thread():



          import threading

          # ...

          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()

          threading.Thread(target=self.foo, daemon=True).start()

          def foo(self):
          time.sleep(5)
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          QTimer + QEventLoop:



          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()
          loop = QtCore.QEventLoop()
          QtCore.QTimer.singleShot(5*1000, loop.quit)
          loop.exec_()
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          QTimer:



          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()
          QtCore.QTimer.singleShot(5*1000, self.foo)

          def foo(self):
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          As a general conclusion, the tasks executed in the thread of the GUI should consume a time less than 30ms, if it is greater than that it is recommended to execute it in another thread. In the case that each task within the loop fulfills the above but there are many iterations, a QTimer can be used to perform the tasks of the loop periodically.






          share|improve this answer












          The GUI lives in an event-loop, this a loop that allows to obtain data of diverse elements like the keyboard, the mouse, the logic, etc and to update the status of the GUI, so if the GUI is frozen it will not allow to do its I work correctly. And in this case the one that blocks the event-loop is the sleep, the while does not intervene since it is executed in less than 1 ms.



          So the tasks that take up a lot of time must be executed in a different thread of the GUI avoiding that it is blocked, but in your case the sleep can be replaced by a QTimer.



          In conclusion, the sleep is freezing the GUI. I see that the code has an educational purpose so I will show you several options:



          threading.Thread():



          import threading

          # ...

          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()

          threading.Thread(target=self.foo, daemon=True).start()

          def foo(self):
          time.sleep(5)
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          QTimer + QEventLoop:



          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()
          loop = QtCore.QEventLoop()
          QtCore.QTimer.singleShot(5*1000, loop.quit)
          loop.exec_()
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          QTimer:



          class test2(QtWidgets.QMainWindow):
          def __init__(self):
          super (test2, self).__init__()
          test2.setGeometry(self,20, 20, 450, 100)
          self.label = QtWidgets.QLabel("Yay",self)
          self.show()
          QtCore.QTimer.singleShot(5*1000, self.foo)

          def foo(self):
          a=50
          b=60
          x=True
          while x:
          if a != b:
          a += 1
          print(a)
          else:
          x = False


          As a general conclusion, the tasks executed in the thread of the GUI should consume a time less than 30ms, if it is greater than that it is recommended to execute it in another thread. In the case that each task within the loop fulfills the above but there are many iterations, a QTimer can be used to perform the tasks of the loop periodically.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 4:42









          eyllanesc

          73.4k103056




          73.4k103056












          • it worked. Thank you for the detailed explanation.
            – Krish
            Nov 21 at 5:18


















          • it worked. Thank you for the detailed explanation.
            – Krish
            Nov 21 at 5:18
















          it worked. Thank you for the detailed explanation.
          – Krish
          Nov 21 at 5:18




          it worked. Thank you for the detailed explanation.
          – Krish
          Nov 21 at 5:18


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53405246%2fpyqt5-child-window-is-not-displayed-until-while-loop-is-executed%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Tonle Sap (See)

          I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

          Guatemaltekische Davis-Cup-Mannschaft