我正在尝试制作一个程序,当满足条件时,返回开始并等待。但不是等待用户按下按钮,而是继续执行代码。
我正在使用 python 3.7.4 和 Windows 10。我认为出现此问题是因为 tkinter 在这种情况下不等待用户输入,而是继续执行代码。
我的代码:
from tkinter import *
from tkinter.ttk import *
def start():
print("Start")
# Removes all widgets without destroying root
for widget in root.winfo_children():
widget.destroy()
button_1 = Button(root, text="Begin", command=begin).pack()
button_2 = Button(root, text="Do something else", command=something).pack()
# I want the program to wait here for the user to click a button
def begin():
print("\nDoing stuff")
if True:
start()
print("This should not be printed")
def something():
pass
root = Tk()
root.geometry("300x300")
btn1 = Button(root, text = "Start", command = start)
btn1.pack()
root.mainloop()
这输出:
Start
Doing stuff
Start
This should not be printed
我想要这个输出:
Start
Doing stuff
Start
然后等待用户选择一个按钮。