【问题标题】:Understanding root and tk.Frame in tkinter了解 tkinter 中的 root 和 tk.Frame
【发布时间】:2020-10-15 18:25:30
【问题描述】:

下面的示例之前已用于描述 Tkinter 中的类和切换页面的功能。 虽然看不懂:

  1. 为什么类 PageOne 和 PageTwo 需要从 tk.Frame 继承?
  2. root = Tk() 的等价物在哪里(因为在我看过的其他初学者教程中,这一步是必不可少的)。

代码:

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

【问题讨论】:

  • “为什么需要将 tk.Frame 传入并在 PageOne 和 PageTwo 中初始化” - tk.Frame 没有在任何地方传入。你是在问他们为什么从tk.Frame继承
  • “root = Tk() 的等价物在哪里” - 那就是app = SampleApp()
  • @BryanOakley,是的,抱歉,我已经改写了我的第一个问题。 SampleApp() 与 Tk() 的作用如何?

标签: python class oop tkinter


【解决方案1】:

为什么类 PageOne 和 PageTwo 需要从 tk.Frame 继承?

他们不必从tk.Frame 继承。因为它们包含其他小部件,所以tk.Frame 是自然选择。您可以使用tk.Canvas 或任何其他小部件(不过,使用tk.Buttontk.Scrollbar 之类的小部件毫无意义)

其中 root = Tk()

就是这行代码:

app = SampleApp()

SampleApp 继承自 tk.Tk,因此其行为完全相同。如果您愿意,可以将 app 重命名为 root

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-07
    • 2020-09-20
    • 2020-04-17
    • 1970-01-01
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多