Switch between two frames in tkinter

I have built my first few scripts with a nice little GUI on them, as the tutorials have shown me, but none of them address what to do for a more complex program.

If you have something with a 'start menu', for your opening screen, and upon user selection you move to a different section of the program and redraw the screen appropriately, what is the elegant way of doing this?

Does one just .destroy() the 'start menu' frame and then create a new one filled with the widgets for another part? And reverse this process when they press the back button?

Answer

One way is to stack the frames on top of each other, then you can simply raise one above the other in the stacking order. The one on top will be the one that is visible. This works best if all the frames are the same size, but with a little work you can get it to work with any sized frames.

Note: for this to work, all of the widgets for a page must have that page (ie: self) or a descendant as a parent (or master, depending on the terminology you prefer).

Here's a bit of a contrived example to show you the general concept:

import tkinter as tk                # python 3

from tkinter import font as tkfont # python 3

#import Tkinter as tk # python 2

#import tkFont as tkfont # python 2

class SampleApp(tk.Tk):

def __init__(self, *args, **kwargs):

tk.Tk.__init__(self, *args, **kwargs)

self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

# the container is where we'll stack a bunch of frames

# on top of each other, then the one we want visible

# will be raised above the others

container = tk.Frame(self)

container.pack(side="top", fill="both", expand=True)

container.grid_rowconfigure(0, weight=1)

container.grid_columnconfigure(0, weight=1)

self.frames = {}

for F in (StartPage, PageOne, PageTwo):

page_name = F.__name__

frame = F(parent=container, controller=self)

self.frames[page_name] = frame

# put all of the pages in the same location;

# the one on the top of the stacking order

# will be the one that is visible.

frame.grid(row=0, column=0, sticky="nsew")

self.show_frame("StartPage")

def show_frame(self, page_name):

'''Show a frame for the given page name'''

frame = self.frames[page_name]

frame.tkraise()

class StartPage(tk.Frame):

def __init__(self, parent, controller):

tk.Frame.__init__(self, parent)

self.controller = controller

label = tk.Label(self, text="This is the start page", font=controller.title_font)

label.pack(side="top", fill="x", pady=10)

button1 = tk.Button(self, text="Go to Page One",

command=lambda: controller.show_frame("PageOne"))

button2 = tk.Button(self, text="Go to Page Two",

command=lambda: controller.show_frame("PageTwo"))

button1.pack()

button2.pack()

class PageOne(tk.Frame):

def __init__(self, parent, controller):

tk.Frame.__init__(self, parent)

self.controller = controller

label = tk.Label(self, text="This is page 1", font=controller.title_font)

label.pack(side="top", fill="x", pady=10)

button = tk.Button(self, text="Go to the start page",

command=lambda: controller.show_frame("StartPage"))

button.pack()

class PageTwo(tk.Frame):

def __init__(self, parent, controller):

tk.Frame.__init__(self, parent)

self.controller = controller

label = tk.Label(self, text="This is page 2", font=controller.title_font)

label.pack(side="top", fill="x", pady=10)

button = tk.Button(self, text="Go to the start page",

command=lambda: controller.show_frame("StartPage"))

button.pack()

if __name__ == "__main__":

app = SampleApp()

app.mainloop()

start pagepage 1page 2

If you find the concept of creating instance in a class confusing, or if different pages need different arguments during construction, you can explicitly call each class separately. The loop serves mainly to illustrate the point that each class is identical.

For example, to create the classes individually you can remove the loop (for F in (StartPage, ...) with this:

self.frames["StartPage"] = StartPage(parent=container, controller=self)

self.frames["PageOne"] = PageOne(parent=container, controller=self)

self.frames["PageTwo"] = PageTwo(parent=container, controller=self)

self.frames["StartPage"].grid(row=0, column=0, sticky="nsew")

self.frames["PageOne"].grid(row=0, column=0, sticky="nsew")

self.frames["PageTwo"].grid(row=0, column=0, sticky="nsew")


Over time people have asked other questions using this code (or an online tutorial that copied this code) as a starting point. You might want to read the answers to these questions:

  • Understanding parent and controller in Tkinter __init__
  • Tkinter! Understanding how to switch frames
  • How to get variable data from a class
  • Calling functions from a Tkinter Frame to an other
  • How to access variables from different classes in tkinter?
  • How would I make a method which is run every time a frame is shown in tkinter
  • Tkinter Frame Resize
  • Tkinter have code for pages in separate files
  • Refresh a tkinter frame on button press

以上是 Switch between two frames in tkinter 的全部内容, 来源链接: utcz.com/a/36786.html

回到顶部