0

An answer to this question might be either about tkinter or parallel loops. I am not sure which solution will be the best. I need your advice.

I have a while True loop, which goes infinitely. Each loop takes around 5 seconds or more, and I wanted to create a GUI button (skip and stop_skip buttons). If the skip button is clicked by a user, it will skip the current while loop and move on to the next while loop until 'stop_skip' button is clicked. So, the skip button will be working as 'continue' in a loop.

Since each loop takes around 5 seconds, I want it to check the variable frequently in the loop. I found that tk.after() may work in every x milliseconds, and some people mentioned tk.update_idletasks() and tk.update(). I tried them, but they just show the box when the loop begins and freeze for the rest 5 seconds. If nothing can work like this (performing a function repetitively with intervals in a while loop), I will probably put if conditions multiple times during the while loop.

The script I want may look like..

show_tk_gui()
while True:
    check_tk_if_skip_button_clicked(interval==1000) #if clicked, continue to the next while loop until 'stop_skip' button is clicked. 
    performing_5_sec_calculation()

Is there any way that can make this possible? Not using GUI option is also acceptable.

SSS
  • 621
  • 2
  • 7
  • 25
  • You _must_ call `mainloop()` in order use tkinter — it's what processes user events and drives the GUI. See the accepted answer to the question [Tkinter — executing functions over time](https://stackoverflow.com/questions/9342757/tkinter-executing-functions-over-time). – martineau Oct 06 '19 at 22:19
  • https://robotic-controls.com/book/export/html/61 – SSS Oct 06 '19 at 22:50
  • This one says root.update() can be used in my own loop. That is why I asked.. – SSS Oct 06 '19 at 22:50
  • 1
    Although the text of the link claims "Alternatively, you could write your own infinite loop, and call `root.update()` yourself occasionally", the code shown isn't doing that…and I've never seen it demostrated anywhere. The canonical way to do such things is via the universal `after()` method as shown in the accompanying code. Also see [Freezing/Hanging tkinter Gui in waiting for the thread to complete](https://stackoverflow.com/questions/53696888/freezing-hanging-tkinter-gui-in-waiting-for-the-thread-to-complete) for another example of running a asynchronous task with a tkinter-based GUI. – martineau Oct 06 '19 at 23:26
  • use `tk.after()` instead of `while True`. It should run function which do two things: (1) check button and perform calculation, (2) run again `tk.after()` to run the same function and then `mainloop()` will have time to update window so it will not freeze. – furas Oct 07 '19 at 00:29
  • Hmm then I guess your answers mean it is not possible to keep my 'while true' with performing_5_sec_calculation() in the loop in order to run tk gui at the same time. Did I understand correctly? – SSS Oct 07 '19 at 02:09
  • ***"performing_5_sec_calculation()"**: You have a long running task, therefore using `.after` is not the propper way to go. You have to [use threads to preventing main event loop from “freezing”](https://stackoverflow.com/a/16747734/7414759) – stovfl Oct 07 '19 at 08:12

0 Answers0