user11794063
user11794063

Reputation:

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()

Upvotes: 0

Views: 386

Answers (2)

J_J
J_J

Reputation: 63

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()

Upvotes: 0

quamrana
quamrana

Reputation: 39354

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)

Upvotes: 1

Related Questions