簡體   English   中英

tkinter 在按下一個按鈕的同時按下另一個按鈕

[英]tkinter press another button while one is pressed

所以當我按下一個按鈕時,它應該運行一個 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()

通常,在 tkinter 程序中,您不希望有 while 循環。 在您的情況下,您可以使用 after() 方法:

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

我知道這已經有幾年了,但也許這會對某人有所幫助。 我通過使用線程解決了這個問題。 這將在單獨的線程上運行 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