[Tutor] Tkinter - How to get multilple windows

Michael Lange klappnase at freenet.de
Thu Jan 1 17:19:09 EST 2004


On Fri, 2 Jan 2004 00:12:36 +0530 (IST)
Uppala Babu <ubabu at cse.iitk.ac.in> wrote:

> >> I have one window which has some buttons and menus. 
> >> On clicking the button, how can i get a new window 
> >> which has some other buttons and menus.
> 
> >Instantiate a new window object which inherits from 
> >TopLevel rather than a simple Frame.
> 
> 
> 
> from Tkinter import *  # Interface to TK Widgets
> 
> 
> #class which has the test button only
> #oncliking the test button i need the One more window but here it is 
> coming befo
> class App(Frame):
>         global quitButton , master
>         def __init__(self, master=None):
>                 Frame.__init__(self,master)
>                 self.grid()
>                 self.createWidgets()
> 
>         def createWidgets(self):
>                 self.quitButton = 
> Button(self,text="Test",command=self.openNew()
>                 self.quitButton.grid()
> 
> 
>         def openNew(self):
>                 app1 = App1(self)
>                 app1.mainloop()
> 
> 
> #quit class
> class App1(Toplevel):
>         def __init__(self,master=None):
>                 Toplevel.__init__(self,master)
>                 self.grid()
>                 self.createWidgets()
> 
>         def createWidgets(self):
>                 textButton = Button(self,text="Quit",command=self.quit)
>                 textButton.grid()
> app = App()     #instance of the Application
> app.master.title("Sample program")
> app.mainloop()          #wait for the events
> 
> 
> 
> 
> 1. I am getting two windows
> 2. I want only the object of APP1 to quit not the entire program.
> 
> Can any please help me?
> 
> 
> 

1. In your createWidgets method you defined the button with the "command=self.openNew()" option;
the brackets after "openNew" were the mistake, if the command option is specified this way,
the command will be executed the moment the button is created. Use "command=self.openNew" instead.

2. The "quit" command exits the mainloop and "quits" the application this way; if you just want to
"destroy" one widget (like a Toplevel window) use its "destroy" method instead:
	textButton = Button(self,text="Quit",command=self.destroy)
(BTW , you do not need an own mainloop() for your Toplevel window as you defined in your App.openNew() method.)

I hope this helps

Good luck

Michael



More information about the Tutor mailing list