Search code examples
pythonuser-interfaceloopstkinterttk

Jump out in Tkinter loop


I am working on a Tkinter doesn't jump out problem.

There is what I am running:

from Tkinter import *
import ttk

def plus(*args):
    value = float(a.get())
    value1 = float(b.get())
    result.set(value + value1)
    print "the result is " + str(result.get())

root = Tk()
root.title("Plus them")

mainframe = ttk.Frame(root, padding="10 10 10 10")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

a = StringVar()
b = StringVar()
result = StringVar()

feet_entry = ttk.Entry(mainframe, width=5, textvariable=a)
feet_entry.grid(column=2, row=1, sticky=(W, E))

feet_entry1 = ttk.Entry(mainframe, width=5, textvariable=b)
feet_entry1.grid(column=5, row=1, sticky=(W, E))

ttk.Label(mainframe, text="the result is").grid(column=3, row=2, sticky=W)
ttk.Label(mainframe, textvariable = result).grid(column=5, row=2, sticky=(W, E))

ttk.Button(mainframe, text="Plus", command=plus).grid(column=3, row=3, sticky=W)

for child in mainframe.winfo_children():
    child.grid_configure(padx=5, pady=5)
    feet_entry.focus()
    root.bind('<Return>', plus)
    root.mainloop()

enter image description here

When it runs, it seems fine. But it doesn’t “jump out” no matter how many times I click the “Plus”, until I input new values for calculation and it still waiting for new entry.

How can I adjust to have it calculate only for once? Thanks.


Solution

  • To make your window execute only once and have plus button closing the window do as follow:

    from Tkinter import *
    import ttk
    
    
    
    
    def plus(*args):
        value = float(a.get())
        value1 = float(b.get())
        result.set(value + value1)
        print "the result is " + str(result.get())
        root.destroy()             ####### look here !!!#######
    
    root = Tk()
    
    root.title("Plus them")
    
    mainframe = ttk.Frame(root, padding="10 10 10 10")
    mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
    mainframe.columnconfigure(0, weight=1)
    mainframe.rowconfigure(0, weight=1)
    
    a = StringVar()
    b = StringVar()
    result = StringVar()
    
    feet_entry = ttk.Entry(mainframe, width=5, textvariable=a)
    feet_entry.grid(column=2, row=1, sticky=(W, E))
    
    feet_entry1 = ttk.Entry(mainframe, width=5, textvariable=b)
    feet_entry1.grid(column=5, row=1, sticky=(W, E))
    
    ttk.Label(mainframe, text="the result is").grid(column=3, row=2, sticky=W)
    ttk.Label(mainframe, textvariable = result).grid(column=5, row=2, sticky=(W, E))
    
    ttk.Button(mainframe, text="Plus", command=plus).grid(column=3, row=3, sticky=W)
    
    
    # for child in mainframe.winfo_children():
    #     child.grid_configure(padx=5, pady=5)
    
    feet_entry.focus()
    root.bind('<Return>', plus)
    
    root.mainloop()
    

    So here, in your plus callbeck you need to call root.destroy(). Also this loop for child in mainframe.winfo_children() does not make sense in my option, and its not needed. So I removed it in the example.