如何在 tkinter 中使用 "place" 切换框架?

How do I switch frame using "place" in tkinter?

我是 Tkinter 和 python 的新手。我正在关注 Switch between two frames in tkinter 以查看切换帧并且它有效。接下来,我尝试在 Page One 中编写用于切换帧的代码,但我不知道该怎么做。

代码如下:

from tkinter import *
import tkinter as tk
from tkinter import font as tkfont

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)

        width = 1350  # 1280
        height = 720
        screen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()
        x = (screen_width / 2) - (width / 2)
        y = (screen_height / 2) - (height / 2)
        self.geometry(f'{width}x{height}+{int(x)}+{int(y)}')
        self.resizable(False,False)

        self.frames = {}
        for F in (StartPage, PageOne):
            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"))         
        button1.pack()
        


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        LeftFrame=tk.Frame(self,relief=RIDGE, bd=2)
        LeftFrame.place(x=0,y=0,width=160,height=720)

        button = tk.Button(LeftFrame, text="Go to the start page",font=("bold"),command=lambda: controller.show_frame("StartPage")) 
        button.grid(row=0)

        buttonBlue = tk.Button(LeftFrame, text="Go to the blue page",bg="blue",fg="white",command=self.blue,activebackground="blue",activeforeground="white")                   
        buttonBlue.grid(row=1)

        buttonRed = tk.Button(LeftFrame, text="Go to the red page",bg="red",fg="white",command=self.red,activebackground="red",activeforeground="white")                  
        buttonRed.grid(row=2)

        buttonYellow = tk.Button(LeftFrame, text="Go to the yellow page",bg="yellow",fg="black",command=self.yellow,activebackground="yellow",activeforeground="black")
        buttonYellow.grid(row=3)

    def blue(self):
        # self.hide_all_frames()
        blueFrame=tk.Frame(self,relief=RIDGE,bd=1 ,bg="blue")
        blueFrame.place(x=160,y=0,width=1190,height=720)

    def red(self):  # Do I need to put self here? It still worked without putting self here
        # self.hide_all_frames()
        redFrame=tk.Frame(self,relief=RIDGE,bd=1 ,bg="red")
        redFrame.place(x=200,y=0,width=1150,height=720)

    def yellow(self):
        # self.hide_all_frames()
        yellowFrame=tk.Frame(self,relief=RIDGE,bd=1 ,bg="yellow")
        yellowFrame.place(x=240,y=0,width=1110,height=720)

    def hide_all_frames(self):
        self.blue.withdraw()


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

当我切换到第一页时,我创建了几个按钮来切换颜色框架。每次我在颜色之间切换时,它们都会重叠,如下图所示。

我正在寻找一种在不相互重叠的情况下切换颜色框的方法。而且,当我返回起始页时,所有颜色框都应该是 destroyed/hidden。请帮我。非常感谢。

对于PageOne中的帧切换,您需要在显示请求的帧之前隐藏当前帧。另外最好在__init__()中创建三个色框并显示在相应的函数中:

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        ...

        # create the three color frames with initially hidden
        self.blueFrame = tk.Frame(self, relief=RIDGE, bd=1, bg="blue")
        self.redFrame = tk.Frame(self, relief=RIDGE, bd=1, bg="red")
        self.yellowFrame = tk.Frame(self, relief=RIDGE, bd=1, bg="yellow")

    def blue(self):
        self.hide_all_frames()
        self.blueFrame.place(x=160, y=0, width=1190, height=720)

    def red(self):
        self.hide_all_frames()
        self.redFrame.place(x=200, y=0, width=1150, height=720)

    def yellow(self):
        self.hide_all_frames()
        self.yellowFrame.place(x=240, y=0, width=1110, height=720)

    def hide_all_frames(self, event=None):
        self.redFrame.place_forget()
        self.blueFrame.place_forget()
        self.yellowFrame.place_forget()

如果想在切换帧后隐藏所有颜色帧,即PageOne -> MainPage -> PageOne,可以使用虚拟事件通知PageOne当它被提出时。然后PageOne在接收到这样的虚拟事件时隐藏所有颜色框:

class SampleApp(tk.Tk):
    ...

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()
        # notify the raised frame via virtual event
        frame.event_generate('<<Raised>>') 

...

class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        ...

        self.blueFrame = tk.Frame(self, relief=RIDGE, bd=1, bg="blue")
        self.redFrame = tk.Frame(self, relief=RIDGE, bd=1, bg="red")
        self.yellowFrame = tk.Frame(self, relief=RIDGE, bd=1, bg="yellow")

        self.bind('<<Raised>>', self.hide_all_frames)