繁体   English   中英

当我使用wait_window()时,Tkinter冻结

[英]Tkinter freezes when I use wait_window()

我正在尝试使用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

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提供主服务器也很重要。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM