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. PyQt not responding with example code

PyQt not responding with example code

Scheduled Pinned Locked Moved Unsolved Qt for Python
12 Posts 6 Posters 5.1k Views 2 Watching
  • 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.
  • S Offline
    S Offline
    Samuel Bachorik
    wrote on last edited by Samuel Bachorik
    #1

    Hello i made simple code like this. While my function is counting my program is not responding.

    Please can somebody show me how to change this code to get it work with that "Threads" thing ?

    THANK YOU !

    import sys
    from PyQt5 import QtWidgets
    from PyQt5.QtWidgets import *
    
    
    
    class Window(QMainWindow):
    
       def __init__(self):
           super().__init__()
    
           self.setFixedSize(300, 300)
           self.move(100,100)
           self.setWindowTitle("Hello")
    
           self.label = QLabel(self)
           self.label.setText("Not done")
           self.label.resize(400,500)
           self.label.move(0,5)
    
    
           self.b1 = QtWidgets.QPushButton(self)
           self.b1.setText("Count")
           self.b1.move(100, 120)
           self.b1.resize(130, 32)
           self.b1.clicked.connect(self.Count)
    
           self.show()
    
       def Count(self):
           self.i = 0
    
           while self.i != 50000000:
                  self.i +=1
    
           self.label.setText("DONE")
    
    
    
    
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        window = Window()
        sys.exit(app.exec())
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Because your counting loop is blocking Qt's event loop.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • S Offline
        S Offline
        Samuel Bachorik
        wrote on last edited by
        #3

        And how can i improve this code to not doing it ? Can you tell me what to add to this code ? Please.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          If you want to run long lasting tasks without blocking your GUI either use QtConcurrent or move it to another thread.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Samuel Bachorik
            wrote on last edited by
            #5

            I just can not find on internet how to properly use this. Where to put this command or how to work with it.

            U 1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              What command are you talking about ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Samuel Bachorik
                wrote on last edited by
                #7

                I mean this QtConcurrent. I dont know how to use it, where to put this thing in this code.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Did you read the documentation I linked ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  1
                  • S Samuel Bachorik

                    I just can not find on internet how to properly use this. Where to put this command or how to work with it.

                    U Offline
                    U Offline
                    unplug
                    wrote on last edited by
                    #9

                    @Samuel-Bachorik This tutorial about multithreading may help you solve your problem.

                    S 1 Reply Last reply
                    1
                    • U unplug

                      @Samuel-Bachorik This tutorial about multithreading may help you solve your problem.

                      S Offline
                      S Offline
                      Samuel Bachorik
                      wrote on last edited by
                      #10

                      @unplug Thank you man !!!!!

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Amarnath Reddy S
                        wrote on last edited by
                        #11

                        Hi Sir,
                        In threading is it possible to run with out sleep ?

                        jsulmJ 1 Reply Last reply
                        0
                        • A Amarnath Reddy S

                          Hi Sir,
                          In threading is it possible to run with out sleep ?

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          @Amarnath-Reddy-S said in PyQt not responding with example code:

                          In threading is it possible to run with out sleep ?

                          Yes, don't call sleep() then.
                          Keep in mind that the thread will consume 100% of one CPU core.

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

                          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