了解 Tkinter __init__ 中的父级和控制器 [英] Understanding parent and controller in Tkinter __init__

查看:30
本文介绍了了解 Tkinter __init__ 中的父级和控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解以下代码的含义:

I want to understand what the below code means:

class PageOne(tk.Frame):

    def __init__(self, parent, controller):

什么是selfparentcontroller?这些工具在这里的作用和范围是什么?

What are self, parent and controller? What is the role and scope of these tools here?

我相信self和Java中的this很相似,但是parentcontroller有什么用呢?

I believe self is similar to this in Java, but what's the use of parent and controller?

在代码流的后面我可以看到:

Later in the code flow I can see:

button1 = tk.Button(self, text="Back to Home",
      command=lambda: controller.show_frame(StartPage))

已经定义了一个名为show_frame的函数,但是为什么要使用控制器来调用这个函数?

There is a function already defined called show_frame, but why is the controller being used to call this function?

推荐答案

粗略地说,原始代码1试图使用伪MVC(模型、视图和控制器)架构.虽然没有模型"部分——只有一个视图"(一些框架)和一个控制器"(主要应用程序).因此,对控制器对象的引用.原始代码实际上是为了展示如何堆叠"帧而编写的,因此它的 MVC 实现非常浅薄且文档不足,因为这不是示例的重点.

Roughly speaking, the original code1 attempted to use a pseudo-MVC (model, view and controller) architecture. Though without the "model" part -- there was just a "view" (some frames) and a "controller" (the main application). Hence, the reference to a controller object. The original code was actually written to show how to "stack" frames, so it's MVC implementation is very shallow and under-documented since that wasn't the point of the example.

回答您的具体问题:

self 代表当前对象.这是一个类的任何方法的常见第一个参数.正如您所建议的,它类似于 Java 的 this.

self represents the current object. This is a common first parameter for any method of a class. As you suggested, it's similar to Java's this.

parent 表示充当当前对象的父级的小部件.tkinter 中除根窗口之外的所有小部件都需要一个父窗口(有时也称为 master)

parent represents a widget to act as the parent of the current object. All widgets in tkinter except the root window require a parent (sometimes also called a master)

controller 代表一些其他对象,旨在充当多个小部件页面的公共交互点.这是对页面解耦的尝试.也就是说,每个页面不需要知道其他页面.如果它想与另一个页面交互,例如使其可见,它可以要求控制器使其可见.

controller represents some other object that is designed to act as a common point of interaction for several pages of widgets. It is an attempt to decouple the pages. That is to say, each page doesn't need to know about the other pages. If it wants to interact with another page, such as causing it to be visible, it can ask the controller to make it visible.

您问已经定义了一个名为 show_frame 的函数,但为什么要使用控制器来调用此函数?" 注意 show_frame 是在单独的文件中定义的类,在这种情况下是主程序类.它没有在其他类中定义.为了让其他类能够调用它,它们必须在主类的实例上调用它.该实例在这些其他类的上下文中被命名为 controller.

You asked "There is a function already defined called show_frame, but why is the controller being used to call this function?" Notice that show_frame is defined in a separate class, in this case the main program class. It is not defined in the other classes. For the other classes to be able to call it, they must call it on an instance of the main class. That instance is named controller in the context of these other classes.

1 注意:尽管您可能在在线教程中找到了原始代码,但它最初来自此 stackoverflow 答案:在 tkinter 中的两个帧之间切换

1 Note: even though you likely found the original code in an online tutorial, it originally came from this stackoverflow answer: Switch between two frames in tkinter

这篇关于了解 Tkinter __init__ 中的父级和控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆