How do I make it so a two top level widgets can't be open simultaneously?
I have a top level widget that is created when a b开发者_Go百科utton 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?
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.
精彩评论