Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can i stop the progressbar while loop with the pushbutton? (Pyqt5 Gui)

How can i stop the progressbar while loop with the pushbutton? (Pyqt5 Gui)

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 1.9k 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.
  • C Offline
    C Offline
    caca91
    wrote on last edited by
    #1

    This is a basic progress bar.

    i just tried this code. the progress bar is loading but not stopping when i push the stop button it just contiues.

    and whenever i push the stop button the application is locking until progress bar finish.

    import sys
    import time
    
    from PyQt5.QtWidgets import (QApplication, QDialog,
                                 QProgressBar, QPushButton)
    
    TIME_LIMIT = 100
    
    class Actions(QDialog):
        """
        Simple dialog that consists of a Progress Bar and a Button.
        Clicking on the button results in the start of a timer and
        updates the progress bar.
        """
        def __init__(self):
            super().__init__()
            self.initUI()
    
        def initUI(self):
            self.setWindowTitle('Progress Bar')
            self.progress = QProgressBar(self)
            self.progress.setGeometry(0, 0, 300, 100)
            self.progress.setMaximum(100)
            self.button = QPushButton('Start', self)
            self.button.move(0, 30)
            self.button2 = QPushButton('Stop', self)
            self.button2.move(30, 60)
            self.show()
            self.show()
    
            self.button.clicked.connect(self.onButtonClick)
            self.button2.clicked.connect(self.onButtonClick2)
    
        def onButtonClick(self):
            count = 0
            while count < TIME_LIMIT:
                count += 1
                time.sleep(1)
                self.progress.setValue(count)
                print(count)
                stop = 0
                if stop == 1:
                    break
    
        def onButtonClick2(self):
                stop = 1
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = Actions()
        sys.exit(app.exec_())
    
    jsulmJ JonBJ 2 Replies Last reply
    0
    • C caca91

      This is a basic progress bar.

      i just tried this code. the progress bar is loading but not stopping when i push the stop button it just contiues.

      and whenever i push the stop button the application is locking until progress bar finish.

      import sys
      import time
      
      from PyQt5.QtWidgets import (QApplication, QDialog,
                                   QProgressBar, QPushButton)
      
      TIME_LIMIT = 100
      
      class Actions(QDialog):
          """
          Simple dialog that consists of a Progress Bar and a Button.
          Clicking on the button results in the start of a timer and
          updates the progress bar.
          """
          def __init__(self):
              super().__init__()
              self.initUI()
      
          def initUI(self):
              self.setWindowTitle('Progress Bar')
              self.progress = QProgressBar(self)
              self.progress.setGeometry(0, 0, 300, 100)
              self.progress.setMaximum(100)
              self.button = QPushButton('Start', self)
              self.button.move(0, 30)
              self.button2 = QPushButton('Stop', self)
              self.button2.move(30, 60)
              self.show()
              self.show()
      
              self.button.clicked.connect(self.onButtonClick)
              self.button2.clicked.connect(self.onButtonClick2)
      
          def onButtonClick(self):
              count = 0
              while count < TIME_LIMIT:
                  count += 1
                  time.sleep(1)
                  self.progress.setValue(count)
                  print(count)
                  stop = 0
                  if stop == 1:
                      break
      
          def onButtonClick2(self):
                  stop = 1
      
      if __name__ == "__main__":
          app = QApplication(sys.argv)
          window = Actions()
          sys.exit(app.exec_())
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @caca91 said in How can i stop the progressbar while loop with the pushbutton? (Pyqt5 Gui):

      while count < TIME_LIMIT:
      count += 1
      time.sleep(1)

      Such code should never be in the main thread of an event driven application! This is a no go.
      It blocks the main thread, so the event loop is blocked until the loop terminates.
      Remove the while loop and use a QTimer to update the progress dialog.

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

      1 Reply Last reply
      3
      • C caca91

        This is a basic progress bar.

        i just tried this code. the progress bar is loading but not stopping when i push the stop button it just contiues.

        and whenever i push the stop button the application is locking until progress bar finish.

        import sys
        import time
        
        from PyQt5.QtWidgets import (QApplication, QDialog,
                                     QProgressBar, QPushButton)
        
        TIME_LIMIT = 100
        
        class Actions(QDialog):
            """
            Simple dialog that consists of a Progress Bar and a Button.
            Clicking on the button results in the start of a timer and
            updates the progress bar.
            """
            def __init__(self):
                super().__init__()
                self.initUI()
        
            def initUI(self):
                self.setWindowTitle('Progress Bar')
                self.progress = QProgressBar(self)
                self.progress.setGeometry(0, 0, 300, 100)
                self.progress.setMaximum(100)
                self.button = QPushButton('Start', self)
                self.button.move(0, 30)
                self.button2 = QPushButton('Stop', self)
                self.button2.move(30, 60)
                self.show()
                self.show()
        
                self.button.clicked.connect(self.onButtonClick)
                self.button2.clicked.connect(self.onButtonClick2)
        
            def onButtonClick(self):
                count = 0
                while count < TIME_LIMIT:
                    count += 1
                    time.sleep(1)
                    self.progress.setValue(count)
                    print(count)
                    stop = 0
                    if stop == 1:
                        break
        
            def onButtonClick2(self):
                    stop = 1
        
        if __name__ == "__main__":
            app = QApplication(sys.argv)
            window = Actions()
            sys.exit(app.exec_())
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @caca91
        As @jsulm has written, your main issue is that you must not use sleep() and you must start using Qt/PySide2 signals/slots, like a QTimer.

        However, additionally:

                    stop = 0
                    if stop == 1:
                        break
        
            def onButtonClick2(self):
                    stop = 1
        

        How do you expect stop to ever == 1 so as to break out of your loop? Conceptually, onButtonClick2() would have to get called immediately after stop = 0 and before if stop == 1, which ain't going to happen. Your stop = 0 needs to be moved outside the loop which tests its value.

        Plus, in Python those two occurrences of a stop variable in two separate functions (onButtonClick & onButtonClick2) are two separate local variables, and one does not refer to the other. You would need to make them self.stop for it to begin to work. Just saying....

        1 Reply Last reply
        3

        • Login

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