简体   繁体   中英

tkinter press another button while one is pressed

So when i press a button, it should run a while loop for some time, and while that function is running i wanna press another button to active another function

import threading
from tkinter import *

root = Tk()

def running_function(): #running forever
    while True:
        pass


def print_something(): #i want to run this while the other function is running
    pass

button1 = Button(root, text='PRESS1', command=running_function)
button1.pack()

button2 = Button(root, text='PRESS2', command=print_something) # while "running_function" is active i want to be able to press this button 
button2.pack()

root.mainloop()

In general, in tkinter programs, you don't want to have while loops. In your case you could use the after() method:

def running_function(): #running forever
    # contents of function elided
    root.after(1, running_function)

I know this is a couple years old but maybe this will be helpful to someone. I get around this problem by using threading. This will run the function on a separate thread and allow the application to continue running dynamically at the same time.

thread1 = threading.Thread(target=running_function, args = (your_arg,))
thread1.start()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM