0

我正在关注这个答案: Switch between two frames in tkinter

在下面提到的代码中,我正在用两页制作表格,起始页和第一页,问题是为什么第一页的子框架的标签“lorem”显示在起始页上,因为它是子的一部分第一页的框架?

import tkinter as tk                # python 3
from tkinter import font as tkfont  # python 3

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")

        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):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame

            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()

        frame1 = tk.Frame(self).pack()
        tk.Label(frame1, text='lorem', font=controller.title_font).pack()


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

4

2 回答 2

0

通过在下一行中打包“frame1”来解决问题,例如:

        frame1 = tk.Frame(self)
        frame1.pack()

现在“frame1”的内容仅显示在“frame1”中,而不显示在其他框架上。

于 2020-11-25T06:12:41.163 回答
-1

'frame1' 似乎不受 PageOne 的本地范围的限制。我已经看了一段时间,并认为这是由于该for F in...循环只是parent=container为所有帧设置,container根窗口在哪里。

如果您指定 PageOne'sself作为标签的父级,而不是frame1,则显示 OK。

#! /usr/bin/env python3

import tkinter as tk
from tkinter import font as tkfont  ## python 3

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' )

        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
            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
        tk .Label( self,  text='This is the start page',  font=controller .title_font ) .pack( side='top',  fill='x',  pady=10 )
        tk .Button( self,  text='Go to Page One',  command=lambda: controller .show_frame('PageOne') ) .pack()
        tk .Button( self,  text='Go to Page Two',  command=lambda: controller .show_frame('PageTwo') ) .pack()


class PageOne( tk .Frame ):
    def __init__( self,  parent,  controller ):
        tk .Frame .__init__( self,  parent )
        self .controller  = controller
        tk .Label( self,  text='This is page 1',  font=controller .title_font ) .pack( side='top',  fill='x', pady=10 )
        tk .Button( self,  text='Go to the start page',  command=lambda: controller .show_frame('StartPage') ) .pack()
        tk .Label( self,  text='lorem',  font=controller .title_font ) .pack()


class PageTwo( tk .Frame ):
    def __init__( self,  parent,  controller ):
        tk .Frame .__init__( self,  parent )
        self .controller = controller
        tk .Label( self,  text='This is page 2',  font=controller .title_font ) .pack( side='top',  fill='x',  pady=10 )
        tk .Button( self,  text='Go to the start page',  command=lambda: controller .show_frame('StartPage') ) .pack()
        tk .Label( self,  text='ipsum',  font=controller .title_font ) .pack()


if __name__ == '__main__':
    app  = SampleApp()
    app .mainloop()
于 2020-11-24T21:45:25.313 回答