简体   繁体   English

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

[英]Tkinter freezes when I use wait_window()

I'm trying to get some values using Tkinter GUI, for then use them to make a url request. 我正在尝试使用Tkinter GUI获取一些值,然后使用它们进行url请求。 But I got stuck apparently when a "wait_window()" command is executed, the window freezes, and if I don't use the wait_window() command, it does not return the values that i want, instead returns zeros. 但是当执行“ wait_window()”命令时,我显然卡住了,窗口冻结了,如果我不使用wait_window()命令,它不会返回我想要的值,而是返回零。 Please tell me where I'm wrong, it's really frustrating. 请告诉我哪里错了,这真令人沮丧。 And my apologies if is an obvious mistake i'm new in tkinter. 我很抱歉,如果这是一个明显的错误,我是tkinter的新手。 This is my code so far, using Python 2.7.xx Ps. 到目前为止,这是我的代码,使用Python 2.7.xx Ps。 when I run this code isolated it works perfectly. 当我隔离运行此代码时,它可以完美运行。 the problem comes when I try to mix it with the other part of my code. 当我尝试将其与代码的其他部分混合时,就会出现问题。

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() is something else. wait_window()是另外一回事。 You want mainloop() . 您需要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

Also, if you plan on calling this more than once, it's very important that you provide the IntVar with a master. 另外,如果您计划多次调用此程序,则向IntVar提供主服务器也很重要。

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

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