I have a top level widget that is created when a button is pressed. How do I make it so when that same button is pressed again, while the top level widget is still open it simply moves the top level widget into focus?
+1
A:
Imagine you have the following method in a class. This method is called when you press the button. You will also have an instance attribute defined in the __init__
method: self.toplevel = None
.
def button_press(self):
if self.toplevel is None:
self.toplevel = ... # another method to create toplevel widget
else:
# set focus to self.toplevel
# you can use self.toplevel.deiconify() if self.toplevel is minimised
# also look at self.toplevel.lift() to bring the window to the top
You will also need to reset self.toplevel
to None
when the toplevel widget is destroyed.
Also look at the focus_set
method of widgets. You may have to set the take_focus
attribute to True
for a toplevel widget. But maybe you want to set the focus to a particular widget (eg a Textbox) on the toplevel widget.
PreludeAndFugue
2010-07-05 21:57:15
I would remove the "else". Simply create it if it doesn't exist then always raise it and give it focus.
Bryan Oakley
2010-07-06 11:53:41
@Bryan: yes, that is a better idea. Thanks.
PreludeAndFugue
2010-07-06 12:05:53