Reputation: 2996
Hi i was wondering if you could make it so you could have more that one button pressed at a time?
Like:
from Tkinter import *
tkwin = Tk()
def delayedDoSomethings():
for i in range(1,10000000):
print 'hi',i
def delayedDoSomething():
for i in range(1,10000000):
print i
a = Button(tkwin, text="Go", command=delayedDoSomething)
a.pack()
b = Button(tkwin, text="Go hi", command=delayedDoSomethings)
b.pack()
tkwin.mainloop()
and the i would be able to click "go" and then "go hi" but i cant because the window freezes until it is done. does any one know how to make it so that you can press more that one button at a time?
Upvotes: 2
Views: 996
Reputation: 5967
What you want here is to use threads. Threads allow you to have multiple pieces of code executing at the same time (or they will at least appear to be executing simultaneously)
Inside of delayedDoSomethings()
, you'll want to spawn a new thread that does the actual work, so that you can return control to Tkinter in the main thread.
You would do the same thing in delayedDoSomething()
.
Here's some actual code that you could use in delayedDoSomethings()
def delayedDoSomethings():
def work():
for i in rance(1, 10000000):
print 'hi',i
import thread
thread.start_new_thread(separateThread, ()) #run the work function in a separate thread.
Here is the documentation for Python's built-in thread module, which will be useful.
Upvotes: 2