我正在尝试使用Tkinter GUI获取一些值,然后使用它们来发出url请求。但是当执行“wait_window()”命令时,我显然卡住了,窗口冻结,如果我不使用wait_window()命令,它就不会返回我想要的值,而是返回零。 请告诉我我哪里错了,真的很令人沮丧。我很抱歉,如果这是一个明显的错误我是tkinter的新手。 到目前为止,这是我的代码,使用的是Python 2.7.xx PS。当我运行此代码时,它完美无缺。当我尝试将它与我的代码的其他部分混合时,问题出现了。
import Tkinter as tk
def getTime(root):
tk.Label(root,font = ('arial',12,'bold'),
text="Input the requested values",
fg="Steel Blue",bd=8, anchor='center').grid(row=0, columnspan=4)
#
tk.Label(root, text="Time (hh)"
).grid(row=2, column=1)
tk.Label(root, text="Minutes (mm)"
).grid(row=2, column=2)
tk.Label(root, text="(max. 20 minutes)"
).grid(row=6, column=0, columnspan=3)
tk.Label(root, text="Lapse (min)"
).grid(row=4, sticky='w')
#
f1 = tk.IntVar()
f2 = tk.IntVar()
f3 = tk.IntVar()
e1 = tk.Entry(root, textvariable=f1, width=12,
bg='light blue')
e2 = tk.Entry(root, textvariable=f2, width=12,
bg='light blue')
e3 = tk.Entry(root, textvariable=f3, width=12,
bg='light blue')
#
e1.grid(row=3, column=1)
e2.grid(row=3, column=2)
e3.grid(row=4, column=1)
#
tk.Button(root, text="Done", font=('arial',10,'bold'),
command=root.quit).grid(row=10, column=3, sticky='w')
##
root.wait_window() ## here's where it stops
##
return ["%02i" %f1.get(), "%02i"%f2.get(), "%02i" %f3.get()]
#
if __name__ == "__main__":
## Some code to get other values
##
root = tk.Tk()
values = getTime(root)
root.mainloop() ## here stops
print time ## never print this
#
time = "%s:%s" %(values[0],values[1])
lapse = values[2]
print time, lapse
##
## some code that will use those values
答案 0 :(得分:0)
wait_window()
是另一回事。你想要mainloop()
。
import Tkinter as tk
def getTime():
root = tk.Tk()
tk.Label(root,font = ('arial',12,'bold'),
text="Input the requested values",
fg="Steel Blue",bd=8, anchor='center').grid(row=0, columnspan=4)
#
tk.Label(root, text="Time (hh)"
).grid(row=2, column=1)
tk.Label(root, text="Minutes (mm)"
).grid(row=2, column=2)
tk.Label(root, text="(max. 20 minutes)"
).grid(row=6, column=0, columnspan=3)
tk.Label(root, text="Lapse (min)"
).grid(row=4, sticky='w')
#
f1 = tk.IntVar(root)
f2 = tk.IntVar(root)
f3 = tk.IntVar(root)
e1 = tk.Entry(root, textvariable=f1, width=12,
bg='light blue')
e2 = tk.Entry(root, textvariable=f2, width=12,
bg='light blue')
e3 = tk.Entry(root, textvariable=f3, width=12,
bg='light blue')
#
e1.grid(row=3, column=1)
e2.grid(row=3, column=2)
e3.grid(row=4, column=1)
#
tk.Button(root, text="Done", font=('arial',10,'bold'),
command=root.quit).grid(row=10, column=3, sticky='w')
##
root.mainloop() ## here's where it stops
##
return ["%02i" %f1.get(), "%02i"%f2.get(), "%02i" %f3.get()]
#
if __name__ == "__main__":
## Some code to get other values
##
values = getTime()
time = "%s:%s" %(values[0],values[1])
lapse = values[2]
print time, lapse
另外,如果您打算不止一次调用此功能,那么为IntVar提供主服务器非常重要。