#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11

    Question How to Clear A Tkinter Screen.


    What I need to know is how to clear a Tkinter Window. I need the last screen to clear after the user clicks next then have new questions appear on that cleared screen.

    Thanks,
    Nick125
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    I would set up your various screens in separate frames, then call the grid() and grid_forget() methods to show/hide the frames as you need. grid_forget() does basically the opposite of grid(), removing the frame from the screen but not destroying it (ie it can be shown again with another grid() call on the widget).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    How would you do that? I am new to Python and programming. If you can, provide a small example.

    Thanks,
    Nick125
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    ok, here's a quick example. you just need to note that I've stored all the pages of the application in a list to iterate over, and that all the work is done 'next' method of the Application class. if you have any queries on the window components used or anything else Tkinter related, this site will come in handy.

    here's the code:

    Code:
    from Tkinter import *
    
    class Page(Frame):
        """Page is the Frame that will be added/removed at will"""
        def __init__(self, root, id):
            Frame.__init__(self, root)
            Label(self, text="Frame %d" % id).pack()
    
    class Application(Frame):
        """Main application where everything is done"""
        def __init__(self, root):
            Frame.__init__(self, root)
            self.root = root
            self.page = 0
            self.pages = [Page(self, x) for x in range(5)]#creates list of 5 pages
            self.pages[self.page].pack(side=TOP)
            Button(self, text="Next", command=self.next).pack(side=BOTTOM)
    
        def next(self):
            """changes the current page. I've only done next here, but you could
            do backwards, skip pages, etc"""
            self.pages[self.page].pack_forget() #remove the current page
            self.page += 1
            if self.page >= 5: #checking haven't gone past the end of self.page
                self.page = 0
            self.pages[self.page].pack(side=TOP) #add the next one
            
    if __name__ == "__main__":
        root = Tk()
        app = Application(root)
        app.pack()
        root.mainloop()
    any other questions/queries/etc, don't hesitate to ask.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    Ok what I need is a menu system that goes to another page then another with other information. How would you create something like that. What I really need to know is how to define the pages that are used in the program.

    Thanks,
    Nick125
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    as I said in my original post, just use a Frame object to represent each page. my code above does almost exactly what you want (from what I can tell), except it just puts a simple Label on each page. You'll have to create a more advanced interface obviously, with more text/widgets/etc. with my code above, you could do that by modifying the Page class to contain whatever you need. if you keep the same page layout for all pages, just with a different blurb of information on each, you could keep the Page class as a generic framework and keep the different pieces of text in a list. not sure if I'm making sense, so here's another example:

    Code:
    from Tkinter import *
    
    class Page(Frame):
        def __init__(self, root, id):
            Frame.__init__(self, root)
            pages = ["This is the info on page 0",
                     "This is the info on page 1",
                     "This is the info on page 2",
                     "This is the info on page 3",
                     "This is the info on page 4"]
            Label(self, text=pages[id]).pack(fill=BOTH)
    
    class Application(Frame):
        """Main application where everything is done"""
        def __init__(self, root):
            Frame.__init__(self, root)
            self.root = root
            self.page = 0
            self.pages = [Page(self, x) for x in range(5)]#creates list of 5 pages
            self.pages[self.page].grid(row=0, column=0, columnspan=2)
            Button(self, text="Next", command=self.next).grid(row=1, column=1)
            Button(self, text="Back", command=self.back).grid(row=1, column=0)
    
        def next(self):
            """changes the current page. I've only done next here, but you could
            do backwards, skip pages, etc"""
            self.pages[self.page].grid_forget() #remove the current page
            self.page += 1
            if self.page >= 5: #checking haven't gone past the end of self.page
                self.page = 4
            self.pages[self.page].grid(row=0, column=0, columnspan=2)
            
        def back(self):
            self.pages[self.page].grid_forget()
            self.page -= 1
            if self.page < 0:
                self.page = 0
            self.pages[self.page].grid(row=0, column=0, columnspan=2)
            
    if __name__ == "__main__":
        root = Tk()
        app = Application(root)
        app.pack()
        root.mainloop()
    from there you should be able to work with it easily. just put the real text in, some buttons to finish/close and whatever else, help etc.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    How would I create the different windows with radio buttons, text, fields that are totally different in the pages list. I am new to this and still don't understand some of Tkinter.

    Thanks,
    Nick125
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    doing that would mean you couldn't use the very generic approach to the task as I did above. you would need to create different Page classes for each separate page, or atleast each one that is unique in terms of widgets/layout.

IMN logo majestic logo threadwatch logo seochat tools logo