How to create different application windows in Tkinter?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nk28
    New Member
    • Jan 2010
    • 26

    How to create different application windows in Tkinter?

    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 :-

    Code:
    class c1 :
        def __init__:
    
    class c2:
        def __init__:
             if user :
                root.destroy()
                root1 = Tk()   
                a2 = c1(root1)    #new application starts
    
    root = Tk()
    a = c2(root)
    root.mainloop()
    The error that I get is that c1 does not have Tk attribute. How can I go to new screens ?

    Please help !
  • dwblas
    Recognized Expert Contributor
    • May 2008
    • 626

    #2
    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.

    Comment

    • nk28
      New Member
      • Jan 2010
      • 26

      #3
      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)

      Comment

      • dwblas
        Recognized Expert Contributor
        • May 2008
        • 626

        #4
        "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://infohost.nmt.edu/tcc/help/pub...ter/index.html
        Code:
        import Tkinter
        
        class EntryTest:
           """ shows using the same StringVar in the second list box
               and in the entry box
           """
           def __init__(self):
              self.top = Tkinter.Tk()
              self.top.title("Test of Entry")
              self.top.geometry("200x150+10+10")
        
              self.str_1 = Tkinter.StringVar()
              label_lit = Tkinter.StringVar()
              self.int_lit = Tkinter.IntVar()
              self.int_ctr = 0
        
              label_1 = Tkinter.Label(self.top, textvariable = label_lit )
              label_1.pack()
              label_lit.set( "Test of Label")
        
              label_2 = Tkinter.Label(self.top, textvariable = self.str_1 )
              label_2.pack()
        
              label_3 = Tkinter.Label(self.top, textvariable = self.int_lit )
              label_3.pack()
              self.int_lit.set(self.int_ctr)
        
              entry_1 = Tkinter.Entry(self.top, textvariable=self.str_1)
              entry_1.pack()
              self.str_1.set( "Entry Initial Value" )
        
              ##---  Delete the contents of an entry widget
              ## entry.delete(0,END)
        
              print_button = Tkinter.Button(self.top, text='PRINT CONTENTS',
                             command=self.getit, bg='blue', fg='white' )
              print_button.pack(fill=Tkinter.X, expand=1)
        
              exit_button= Tkinter.Button(self.top, text='EXIT',
                           command=self.top.quit, bg='red', fg='white' )
              exit_button.pack(fill=Tkinter.X, expand=1)
        
              entry_1.focus_set()
        
              self.top.mainloop()
        
           ##-----------------------------------------------------------------
           def getit(self) :
              print "getit: variable passed =", self.str_1.get()
              self.int_ctr += 1
              self.int_lit.set(self.int_ctr)
        
        
        ##===============================================================
        if "__main__" == __name__  :
           ET=EntryTest()
           print "under __main__ =", ET.str_1.get()

        Comment

        Working...