简体   繁体   English

tkinter 在按下一个按钮的同时按下另一个按钮

[英]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所以当我按下一个按钮时,它应该运行一个 while 循环一段时间,当 function 正在运行时我想按下另一个按钮来激活另一个 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.通常,在 tkinter 程序中,您不希望有 while 循环。 In your case you could use the after() method:在您的情况下,您可以使用 after() 方法:

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.这将在单独的线程上运行 function 并允许应用程序同时继续动态运行。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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