How to run an infinite loop on Menu?

huey.y.jiang at huey.y.jiang at
Mon Dec 3 02:58:55 EST 2018

Hi Folks,

I need to run an infinite loop on a Menu button, like this:

from Tkinter import *

def run_job():
    i = 0
    while 1:
        i = i + 1
        if i > 100:

root = Tk()
menu = Menu(root)
filemenu = Menu(menu)
menu.add_cascade(label="Jobs", menu=filemenu)
filemenu.add_command(label="Run", command=run_job)

In fact, the def run_job has more things to do. But, it is an infinite loop in essence. It works. However, when the def run_job is running, the menu will be hang there. I have no way to stop it. Even if I raise it with Key-Interupt, the thing was the same: the infinite loop cannot be stopped nicely. The only way was to wait until the Python interpreter notice the problem, and popped out an error message, then I can kill the running program.

Can anybody tell me how to handle this? I wish when variable i reached 100, the run_job will be end, and the Menu "Run" will be free.

Thanks in advance. 

More information about the Python-list mailing list