1

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()
texpiet
  • 13
  • 3
  • Just remove the `command=root.destroy` parameter. Why is it there in the first place? – quamrana Feb 09 '22 at 21:21
  • I'm no tkinter expert but I know for a fact that `command=root.destroy` is causing the window to close; you're explicitly closing it. What do you expect to happen? And what do you want to happen instead? – Random Davis Feb 09 '22 at 21:22
  • Wow that was stupid - did not see that command - I just copy and pasted from here and there. Thanks! – texpiet Feb 10 '22 at 00:46
  • @quamrana - 'cause you were first...would you write your suggestion as an answer - so I can give U credit. Thanks again - I really did not see that. – texpiet Feb 10 '22 at 19:40

1 Answers1

0

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")
quamrana
  • 37,849
  • 12
  • 53
  • 71