PyQt GUI order of operations

后端 未结 1 1415
失恋的感觉
失恋的感觉 2020-12-01 23:08

I have a following problem:

upon pressing a button in PyQt made GUI I have to do two actions:

  1. Immediately update a QTextBrowser
  2. Run a method t
相关标签:
1条回答
  • 2020-12-01 23:50

    The GUI is not updated/drawn until control is returned to the Qt event loop. The event loop runs in the main thread, and is what handles interactions with the GUI and coordinates the signals/slot system.

    When you call a Qt function in a slot like clickStop1(), the Qt call runs, but the GUI is not redrawn immediately. In this case, control doesn't return to the event loop until clickStop() has finished running (aka all the slots for the clicked signal are processed.

    The main problem with your code is that you have a time.sleep(5) in the main thread, which is blocking GUI interaction for the user as well as the redrawing. You should keep the execution time of slots short to maintain a responsive GUI.

    I would thus suggest you modify clicked() so that it fires a singleshot QTimer after your specified timeout. QTimers do not block the main thread, and so responsiveness will be maintained. However, be aware that the user may interact with the GUI in the mean time! Make sure they can't compromise the state of your program with some user interaction while you wait for the QTimer to execute.

    0 讨论(0)
提交回复
热议问题