【发布时间】:2023-01-11 00:06:01
【问题描述】:
请帮忙
def change_flag(top_frame, bottom_frame, button1, button2, button3, button4, controller):
global counter, canvas, my_image, chosen, flag, directory
canvas.delete('all')
button5['state'] = DISABLED
counter += 1
chosen, options_text = function_options()
right_answer_flag = get_right_answer_flag(chosen, options_text)
#pdb.set_trace()
try:
location = directory + chosen + format_image
except:
controller.show_frame(PlayAgainExit)
my_image = PhotoImage(file=location)
canvas.create_image(160, 100, anchor=CENTER, image=my_image)
button1["text"] = options_text[0]
button2["text"] = options_text[1]
button3["text"] = options_text[2]
button4["text"] = options_text[3]
button1['state'] = NORMAL
button2['state'] = NORMAL
button3['state'] = NORMAL
button4['state'] = NORMAL
##############
button5 = Button(
next_frame,
width=20,
text="next",
fg="black",
#command=lambda: change_flag(top_frame,bottom_frame,button1,button2,button3,button4,controller))
command=Thread(target=change_flag, args =(top_frame,bottom_frame,button1,button2,button3,button4,controller)).start)
button5.pack(side=RIGHT, padx=5, pady=5)
你好,
我不希望 GUI 冻结,所以我为 button5 使用了线程,但它给了我运行时错误
“你只能启动线程一次”是正确的。但是我应该如何解决这个问题呢?
谢谢你的帮助, 阿拜
【问题讨论】:
-
在
command选项上使用lambda。 -
我看到了您之前的评论并也添加了 lambda,它没有冻结但不会更改下一帧。你想让我发布整个代码吗? button5 = Button( next_frame, width=20, text="next", fg="black", command=lambda: change_flag(top_frame,bottom_frame,button1,button2,button3,button4,controller)) #command=lambda: 线程( target=change_flag, args =(top_frame,bottom_frame,button1,button2,button3,button4,controller)).start)
标签: python-3.x multithreading tkinter