我有一个应用程序,它会在按下按钮时尝试生成文本。大多数情况下,文本生成速度很快,但有一个函数需要大约 20 秒才能执行(取决于文本量)。在此过程中,GUI 经常卡住,因此我将该函数转移到一个单独的线程上,并且该字段上的一切都很好。
现在我遇到了按钮问题。当需要一些时间执行的函数正在运行时,用户仍然可以单击该按钮,并且该函数将在第一次调用仍在处理时执行多次。 我想通过在该函数运行时禁用所有按钮来防止这种情况发生,但我无法让线程正常工作。
这是我的代码:
def generate_text():
choice = dropdown_choice.get()
if context_obj.context_text.get() == '':
if choice == 'OpenAI':
context = 'Some random context text'
else:
context = ' '
else:
context = context_obj.context_text.get()
if choice == 'OpenAI':
progress.start(50)
progress_bar_text = Label(text='Please wait while the text is being generated',
background='#E5F2FF',
font=("Helvetica", 12))
progress_bar_text.place(relx=.2,
rely=.66,
anchor="c")
# multithreading for the OpenAI text generation
q = queue.Queue()
thread1 = Thread(target=openAI_generator.sample, args=[text_amount.get(), temperature.get(), context, q])
thread1.start()
def display_AI_text(q):
openAI_text = q.get()
generated_text.configure(state='normal')
generated_text.delete(1.0,END)
generated_text.insert(tk.END, openAI_text)
generated_text.configure(state='disabled')
progress.stop()
progress_bar_text.place_forget()
thread2 = Thread(target=display_AI_text, args=[q])
thread2.start()
在此代码中,thread1
正在执行该函数,thread2
正在从该函数获取输入并显示它。
我想做的是,在执行 thread2
时,禁用所有按钮,当线程完成时,按钮将再次启用。
我尝试添加:
thread2 = Thread(target=display_AI_text, args=[q])
generate_button.config(state="disabled")
thread2.start()
然后:
thread2.join()
generate_button.config(state="normal")
但是这段代码会卡住应用程序。我假设主线程正在等待 thread2
完成,这就是它没有响应的原因。
有谁知道解决这个问题的方法吗?
最佳答案
在许多 GUI 中,您无法在线程中更改 GUI - 您必须在主进程中执行此操作。
您可以使用队列将信息发送到将更新 GUI 的主进程。
在 Tkinter 中您可以使用
root.after(time_in_milliseconds, function_name)
定期运行函数,可以检查此队列中的消息。
或者可以定期检查
thread2.is_alive()
而不是使用thread2.join()
,因为is_alive()
不会阻塞代码。
import tkinter as tk
from threading import Thread
import time
def long_running_function():
print('start sleep')
time.sleep(3)
print('end sleep')
def start_thread():
global t
global counter
b['state'] = 'disable'
counter = 0
t = Thread(target=long_running_function)
t.start()
check_thread()
# or check after 100ms
# root.after(100, check_thread)
def check_thread():
global counter
if not t.is_alive():
b['state'] = 'normal'
l['text'] = ''
else:
l['text'] = str(counter)
counter += 0.1
# check again after 100ms
root.after(100, check_thread)
#-----------------------------------------------------
# counter displayed when thread is running
counter = 0
root = tk.Tk()
l = tk.Label(root)
l.pack()
b = tk.Button(root, text="Start", command=start_thread)
b.pack()
root.mainloop()
关于python - TkInter 在线程运行时禁用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56951383/