[Tutor] destroying windows
Cédric Lucantis
omer at no-log.org
Mon Jun 30 17:56:08 CEST 2008
Le Monday 30 June 2008 14:47:39 Jim Morcombe, vous avez écrit :
> I want to have a program that uses Tkinter to display a window.
> If the user selects an option, then I want to destroy that window and
> then display a second window.
> In turn, the user can select an option to change back to the first
> window and I want to destroy that window and then display the first again.
>
> I have pasted my code below. The window is successfully created.
> However, I can't figure out the correct way to destroy it.
> In my first attempt, "window1" is not defined when I go to destroy it
> and I am not sure how to make "window1" global or what.
>
Using global variables may help for simple scripts, but is not a good practice
in general. Anyway you could it like this:
# this in the global scope
window1 = None
class display_Colour_Selector_window():
def __init__(self):
global window1
window1 = Tk()
...
def change_to_Colour_Picker():
window1.destroy
(note that the global statement is only required when you set the variable,
not when you only read it)
But a better design would be to use instance attributes and methods for your
callbacks instead, so the whole stuff is 'packed' inside your class:
class display_Colour_Selector_window():
def __init__(self):
self.window1 = Tk()
self.window1.title("Colour Selector")
menubar = Menu(self.window1)
# create pulldown menus
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label="Colour Picker",
command=self.change_to_Colour_Picker)
menubar.add_cascade(label="Edit", menu=editmenu)
# display the menu
self.window1.config(menu=menubar)
def change_to_Colour_Picker (self) :
self.window1.destroy()
display_Colour_Picker_window()
--
Cédric Lucantis
More information about the Tutor
mailing list