如何在标准Python中获得一个阻塞模态输入对话框?
我需要用户在代码继续执行之前输入一个值。
以下是一些不起作用的测试代码,但这个想法是我应该能够从脚本的任何地方调用MyDialog
,所以这只是一个简化的例子。
import tkinter
class MyDialog:
def __init__(self, prompt):
self.top = tkinter.Toplevel()
tkinter.Label(self.top, text=prompt).pack()
self.e = tkinter.Entry(self.top)
self.e.pack(padx=5)
tkinter.Button(self.top, text="OK", command=self.ok).pack(pady=5)
def ok(self):
self.top.destroy()
return self.e.get()
root = tkinter.Tk()
userName = MyDialog('Enter your name')
tkinter.Label(root, text="Hello {}".format(userName)).pack()
root.mainloop()
对话框不仅应该禁用主窗口,还应该阻止调用它的任何代码。并且它应该能够将值传递回调用代码。
tkinter.Label(root...)
,此时您将在userName
中获得用户提供的值。 - mcuuserName
吗?但这仍然无法阻止代码。 - mcu