Reputation: 5199
problem
I have written code for a stopwatch. In this there is a function to reset the stopwatch. And for this: I first destroy the present window and then create a new window. In the process though, the window loses keyboard focus. How can I force the new window to have keyboard focus?
code
def reset(self,event=None):
self.quitwin() ##Closing the window that is open now
self.__init__() ##Creating a new window
def quitwin(self,event=None):
self.window.destroy()
Specs Python 2.7
I would also be grateful if someone could point me toward better implementation (like clearing the present window and again writing the things).
Upvotes: 0
Views: 139
Reputation: 5199
I found the solution. Simply destroy the present frame and just create the frame again without affecting the window.
By doing this the window does not lose keyboard focus and the things are recreated as they were before.
code
def reset(self,event=None):
self.frame.destroy()
self.frame = Frame(self.window,width=300,height=200) ##The frame instance
self.frame.pack_propagate(0) ##Making sure that the window does not shrink
self.frame.pack(fill=None)
Upvotes: 0
Reputation: 590
Try module tkMessageBox and tkCommonDialog, In them you can find the answers.
See their source code, they are included with Python
folder Python\Lib\lib-tk\
And is it required to remove the window, maybe just change the content?
For a button, you can do so:
>>> import tkinter
>>> r=Tkinter.Tk()
>>> b=Tkinter.Button(r,text='aaa')
>>> b.pack()
>>> b['text']='bbb'
For a text widget, you can do so
>>> t=Tkinter.Text(r)
>>> t.pack()
>>> t.insert('1.0','aaa')
>>> t.delete('1.0','end') # clear text widget
>>> t.insert('1.0','bbb')
Upvotes: 1