[Tutor] destroying windows

Jim Morcombe jmorcombe at westnet.com.au
Mon Jun 30 14:47:39 CEST 2008


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.

The error is displayed at the bottom of the program listing.

Jim
------------------------------------------------------------------------------------------

from Tkinter import *

class display_Colour_Selector_window():
    def __init__(self):
        window1 = Tk()
        window1.title("Colour Selector")

        menubar = Menu(window1)

        # create pulldown menus
        editmenu = Menu(menubar, tearoff=0)
        editmenu.add_command(label="Colour Selector", 
command=change_to_Colour_Selector)
        editmenu.add_command(label="Colour Picker", 
command=change_to_Colour_Picker)
        menubar.add_cascade(label="Edit", menu=editmenu)

         # display the menu
        window1.config(menu=menubar)



class display_Colour_Picker_window():
    def __init__(self):

        # The second window will be used for the "Colour Picker" which
        # allows the User to pick colours from a colour map.
        window2 = Tk()
        window2.title("Colour Picker")


        menubar = Menu(window2)

           # create pulldown menus
        editmenu = Menu(menubar, tearoff=0)
        editmenu.add_command(label="Colour Selector", 
command=change_to_Colour_Selector)
        editmenu.add_command(label="Colour Picker", 
command=change_to_Colour_Picker)
        menubar.add_cascade(label="Edit", menu=editmenu)

 
        # display the menu
        window2.config(menu=menubar)



       


def change_to_Colour_Selector():
    display_Colour_Picker_window.window2.destroy
    display_Colour_Selector_window()

   
def change_to_Colour_Picker():
    display_Colour_Selector_window.window1.destroy
    display_Colour_Picker_window()

def hello():
    print "hello!"
   
#--------------------------------------------------------------------#
# The program starts here                                            #
#--------------------------------------------------------------------#

display_Colour_Selector_window()

   

# To run this from IDLE, I will just comment out the mainloop()
# because IDLE is written in Tkinter and actually has its own mainloop()
# running, which thoroughly confuses my own program.
# mainloop()

-----------------------------------------------------------------------------------------------
The error:
IDLE 1.2.1      ==== No Subprocess ====
 >>>
 >>> Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python25\lib\lib-tk\Tkinter.py", line 1403, in __call__
    return self.func(*args)
  File "D:/ComputerScienceProgramming/test.py", line 53, in 
change_to_Colour_Picker
    display_Colour_Selector_window.window1.destroy
AttributeError: class display_Colour_Selector_window has no attribute 
'window1'








More information about the Tutor mailing list