简体   繁体   中英

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. 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. Please tell me where I'm wrong, it's really frustrating. And my apologies if is an obvious mistake i'm new in tkinter. This is my code so far, using 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. You want 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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