Home > OS >  simple Tkinter program - window vanishes after clicking button - maintop problem?
simple Tkinter program - window vanishes after clicking button - maintop problem?

Time:02-12

Coders, I guess I have a newbie question: my windows disappears when I click on a button. If I put in root.mainloop() as last line in the buttonClicked-function, then the program is fine - but it looks not right...what is wrong here?

import tkinter as tk

def buttonClicked(event):
    print(tf1.get())
    tf1Content.set("button clicked")
    # root.mainloop() ... would work

root = tk.Tk()

frame = tk.Frame(root, relief="ridge", borderwidth=2)
frame.pack(fill="both",expand=1)
label = tk.Label(frame, text="Input:")
label.pack(expand=1)
tf1Content = tk.StringVar()
tf1 = tk.Entry(frame, text="input here", textvariable=tf1Content)
tf1.pack(expand=1)
bOk = tk.Button(frame,text="OK",command=root.destroy)
bOk.bind("<Button-1>", buttonClicked)
bOk.widget = "bOK"
bOk.pack(side="bottom")

tf1.focus()

root.mainloop()

CodePudding user response:

It turns out that you just copied this line:

bOk = tk.Button(frame,text="OK",command=root.destroy)

which binds a call to root.destroy() to the button press.

The fix is to just remove the command parameter:

bOk = tk.Button(frame,text="OK")
  • Related