user2120692
user2120692

Reputation: 13

tkinter give window focus

I have a tkinter python 2.x program. I have several windows appearing on the screen. I use buttons to navigate from one to another, but I'm struggling to close a window and refocus on a currently opened window. I can open new windows just fine!

#code for main window
def frmMain():
  app = Tk()
  app.title("TWS: XML Options")
  app.geometry("200x100")
  bn1 = Button(app,text="Add", command=frmAdd)
  bn1.grid(row = 2,column = 2, stick = W)
  bn2 = Button(app,text="Edit", command=frmEdit)
  bn2.grid(row = 2,column = 3, stick = W)
  bn3 = Button(app,text="Delete", command=frmDelete)
  bn3.grid(row = 2,column = 4, stick = W)
  bn4 = Button(app,text="Back",command=frmMenu)
  bn4.grid(row = 3,column = 2, stick = W)
  app.mainloop()

#code for button on sub window
....
bn1 = Button(app,text="Back", command=back)
...

def back():
   #Code to close current window and reopen frmMain

Upvotes: 1

Views: 2579

Answers (2)

Honest Abe
Honest Abe

Reputation: 8759

A TopLevel Window can be activated using its deiconify() method.

Use the basic Widget method focus_set() to set keyboard focus to a specific widget.

Upvotes: 0

A. Rodas
A. Rodas

Reputation: 20689

Just call destroy() on widget's parent.

bn1 = Button(app, text="Back", command=app.destroy)

I suppose you are not destroying the parent window, so once the Toplevel is destroyed, the focus is automatically returned to the previous opened window.

Upvotes: 1

Related Questions