By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,272 Members | 1,733 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,272 IT Pros & Developers. It's quick & easy.

How to create different application windows in Tkinter?

P: 26
Hey all

I am trying to make GUI windows using Tkinter and have got stucked. Its exactly like an installation setup where you move to new windows . I have been unable to understand how to create multiple application windows in Tkinter.
I want that according to user input I should be directed to new screen belonging to a new class like follows :-

Expand|Select|Wrap|Line Numbers
  1.  
  2. class c1 :
  3.     def __init__:
  4.  
  5. class c2:
  6.     def __init__:
  7.          if user :
  8.             root.destroy()
  9.             root1 = Tk()   
  10.             a2 = c1(root1)    #new application starts
  11.  
  12. root = Tk()
  13. a = c2(root)
  14. root.mainloop()
  15.  
The error that I get is that c1 does not have Tk attribute. How can I go to new screens ?

Please help !
Nov 13 '10 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 622
Start with a class that will actually run. Class C2 will not (start here for a simple example) . Then you can pass "root" to C1.
Nov 13 '10 #2

P: 26
Hey my problem is that c2 is working fine on its own . But when I add these statements to work c1 after c2 the Tk window gets formed with the error that c1 has no Tk attribute.

What is difference between this and a2 = Toplevel(root)
Nov 13 '10 #3

Expert 100+
P: 622
"C2" never receives "root" and "C1" never receives "root1"-->although root will work just as well. Take a look at the link posted above and see how root is passed to myApp and how the class receives and then uses root/myParent.

"root = Tk()" just creates a class instance of Tk and you can pass that instance around, and use everything in the Tk class instance, in as many different objects as you like.

Below is an example of a self-contained class. And some Tkinter links
http://www.ferg.org/thinking_in_tkinter/index.html
http://effbot.org/tkinterbook/
http://infohost.nmt.edu/tcc/help/pub...ter/index.html
Expand|Select|Wrap|Line Numbers
  1. import Tkinter
  2.  
  3. class EntryTest:
  4.    """ shows using the same StringVar in the second list box
  5.        and in the entry box
  6.    """
  7.    def __init__(self):
  8.       self.top = Tkinter.Tk()
  9.       self.top.title("Test of Entry")
  10.       self.top.geometry("200x150+10+10")
  11.  
  12.       self.str_1 = Tkinter.StringVar()
  13.       label_lit = Tkinter.StringVar()
  14.       self.int_lit = Tkinter.IntVar()
  15.       self.int_ctr = 0
  16.  
  17.       label_1 = Tkinter.Label(self.top, textvariable = label_lit )
  18.       label_1.pack()
  19.       label_lit.set( "Test of Label")
  20.  
  21.       label_2 = Tkinter.Label(self.top, textvariable = self.str_1 )
  22.       label_2.pack()
  23.  
  24.       label_3 = Tkinter.Label(self.top, textvariable = self.int_lit )
  25.       label_3.pack()
  26.       self.int_lit.set(self.int_ctr)
  27.  
  28.       entry_1 = Tkinter.Entry(self.top, textvariable=self.str_1)
  29.       entry_1.pack()
  30.       self.str_1.set( "Entry Initial Value" )
  31.  
  32.       ##---  Delete the contents of an entry widget
  33.       ## entry.delete(0,END)
  34.  
  35.       print_button = Tkinter.Button(self.top, text='PRINT CONTENTS',
  36.                      command=self.getit, bg='blue', fg='white' )
  37.       print_button.pack(fill=Tkinter.X, expand=1)
  38.  
  39.       exit_button= Tkinter.Button(self.top, text='EXIT',
  40.                    command=self.top.quit, bg='red', fg='white' )
  41.       exit_button.pack(fill=Tkinter.X, expand=1)
  42.  
  43.       entry_1.focus_set()
  44.  
  45.       self.top.mainloop()
  46.  
  47.    ##-----------------------------------------------------------------
  48.    def getit(self) :
  49.       print "getit: variable passed =", self.str_1.get()
  50.       self.int_ctr += 1
  51.       self.int_lit.set(self.int_ctr)
  52.  
  53.  
  54. ##===============================================================
  55. if "__main__" == __name__  :
  56.    ET=EntryTest()
  57.    print "under __main__ =", ET.str_1.get() 
Nov 13 '10 #4

Post your reply

Sign in to post your reply or Sign up for a free account.