0

I am currently coding a program that will do something (e.g count numbers constantly) until something is inputted into a dialog box displayed.

However, whenever I try this, the program freezes when waiting for an input and so does not make any progress in the counting process I am trying to run in the background.

Is there any way to have a timer that continuously runs in the background so that in say 5 minutes, the counter instantly stops and the dialog box disappears? This is a basic skeleton of my code. I used the tkinter dialog box for input and tried to create a timer that will run in the background.

from time import *
from tkinter import *

from tkinter import messagebox

from tkinter import simpledialog

while timer<300:
    sleep(1)
    timer += 1

    ROOT = Tk()
    ROOT.withdraw()
    USER_INP = simpledialog.askstring(title="Code Required",
                                      prompt="What's the Code?:")

Preferably without external modules but if not that is fine. Thanks in advance :)

  • This is the code requested

      from tkinter import *
      from tkinter import simpledialog
    
      root = Tk()
      root.withdraw()
    
      def ask():
          simpledialog.askstring(title="Code Required",
                                            prompt="What's the Code?:")
      ##    root.after(5000, root.destroy()) #added in the root.after() to try        and terminate it after set time
    
      root.after(3000,ask) #triggers ask() after 3000 ms(3 seconds)
      root.after(100000, root.destroy()) # tried to wait 10 seconds before it breaks but this doesn't show the dialog box any more
      root.mainloop()
    
Max Davies
  • 148
  • 10
  • 1
    Does this answer your question? [Tkinter: How to use threads to preventing main event loop from "freezing"](https://stackoverflow.com/questions/16745507/tkinter-how-to-use-threads-to-preventing-main-event-loop-from-freezing) – jordanm Sep 09 '20 at 20:50
  • @jordanm I think this is referring to an oscillation problem instead of an input freezing the program and so stopping the background operations. Thanks though and this is just what I have gathered from a brief reading. Correct me if I am wrong – Max Davies Sep 09 '20 at 21:05

1 Answers1

1

Here is a basic code with tkinter that makes the dialogbox pop up after 5 seconds.

from tkinter import *
from tkinter import simpledialog

root = Tk()
root.withdraw()

def ask():
    simpledialog.askstring(title="Code Required",
                                    prompt="What's the Code?:")
    root.after(5000, root.destroy) #added in the root.after() to try and terminate it after set time

root.after(3000,ask) #triggers ask() after 3000 ms(3 seconds)
#root.after(10000, root.destroy) # tried to wait 10 seconds before it breaks but this doesn't show the dialog box any more
root.mainloop()

Here after() triggers a function after the given time, i.e, 3000 ms(3 sec), so you can adjust the timer, out there too. This is just an example and you can edit this more as you like.

Why use after() and not while and a timer?

This is because a while loop interferes a tkinter mainloop() causing the window to be unresponsive, so it is not recommended to use while or time.sleep(). Instead you could use the built-in after() method by tkinter or threading too.

Here is a bit more on after():

  • It takes two positional arguments,mainly, ms and func
  • ms - It is the time(in milliseconds) after which the specified function will be triggered.
  • func - It is the function to be triggered after the specified ms finises.

WARNING: Keep in mind that the root window is not destroyed, its just hidden, so as long as the root window is not destroyed, the program keeps on running in the background, so you will have to bring back the window and close it for the task to end. For this reason, ive added root.destroy() there.

Take a look here for a bit more understanding on after()

Hope it cleared your doubts, do let me know if any errors.

Cheers

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46