So I want to have the toplevel frame keep focus after it has been withdrawn.
from Tkinter import *
class VanishingFrame():
def __init__(self,parent,Event=None):
self.parent=parent
self.visible=True
self.Frame=Toplevel()
self.Frame.title('Magical disapearing frame')
self.Frame.transient(self.parent)
self.Frame.grab_set()
self.Frame.focus_set()
self.Frame.geometry('300x300+150+150')
Label(self.Frame,text="Press any key").place(relx=.5,rely=0.5,anchor=CENTER)
self.Frame.bind('<Key>',self.AnyKey)
self.AnyKey()
def AnyKey(self,Event=None):
print "key hit"
self.visible= not self.visible
if self.visible:
self.Frame.withdraw()
else:
self.Frame.deiconify()
self.parent.update()
self.Frame.update()
self.Frame.grab_set()
self.Frame.focus_set()
self.Frame.focus_force()
self.Frame.update()
root=Tk()
root.geometry('500x500')
root.title("Bring out your dead")
Magic=VanishingFrame(root);
root.mainloop()
The first key hit works as expected and the VanishingFrame dissapears. However Focus appears to be on the root frame now. Clicking anywhere in the root frame will cause focus to switch back to the vanished frame and then the next key hit will cause it to reappear. Interestingly when the vanishingframe is withdrawn and the root frame appears to have focus, you can not close the root frame. You must click somewhere in the root frame and get the vanished frame to show up and then close it. That would tell me that self.Frame.transient is working.
So the actual question is how do I keep the focus on the withdrawn frame without having to click on the root frame first?