开发者

press more than one Tkinter button

开发者 https://www.devze.com 2023-03-07 08:48 出处:网络
Hi i was wondering if you could make it so you could have 开发者_运维百科more that one button pressed at a time?

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消