【发布时间】:2021-05-12 07:55:30
【问题描述】:
我有这个程序,当你按下是或否时输出一些东西,但它不想开始我的循环,直到我关闭 tkinter 窗口。 tkinter 窗口的目标是在我按下 no 时停止循环,停止按钮并不是真正必要的,我只是把它放在那里关闭窗口。
代码: 将 tkinter 导入为 tk
choice = False
def ja():
global choice
choice == True
print("1 works") # To check if this works
def nee():
global choice
choice == False
print("2 works") # Also to check if works
def prog():
while True:
if choice:
print("3 works")
# Program here
if not choice:
print("4 works")
# Program here
root = tk.Tk()
root.title("Bot")
label1 = tk.Label(root, text="continue?", width=75, height=25)
Yes = tk.Button(root, text="Yes", width=25, height=5, command=ja)
No = tk.Button(root, text="No", width=25, height=5, command=nee)
Stop= tk.Button(root, text="Stop", width=25, height=5, command=root.quit)
label1.pack()
Yes.pack(fill=tk.Y, side=tk.LEFT)
No.pack(fill=tk.Y, side=tk.LEFT)
Stop.pack(fill=tk.Y, side=tk.LEFT)
root.mainloop()
prog()
当我按下是和否 2 次然后停止时的结果(注意:当你按下停止时,循环只会播放直到 ide 崩溃。我知道这一点以及我的项目需要它,所以这不是错误。):
1 works
1 works
2 works
2 works
2 works
4 works
4 works
4 works
4 works
....
【问题讨论】: