Frame.__init__ 是做什么的?[英] What does Frame.__init__ do?

本文是小编为大家收集整理的关于Frame.__init__ 是做什么的?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

在以下代码中,第5行,Frame.__init__做什么?有人可以解释其背后的概念吗?非常感谢!

from Tkinter import *
class AppUI(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master, relief=SUNKEN, bd=2)
        [...]

编辑: 完整的代码与正确的沉索

推荐答案

类AppUI基于Tkinter的类Frame.这意味着AppUI类是类型的 Frame类型,但某些行为略有不同或定制.这意味着AppUI类的方法可能需要(实际上,通常需要)从Frame类调用代码.也就是说,AppUI想要做与Frame类,以及的其他事情相同的事情.这就是这里发生的事情:当您实例化AppUI时,您希望首先将其初始化为Frame,然后执行AppUI特定的初始化.

在这里AppUI明确调用其父班的 __init__() __init__() __init__() __init__()一个>方法.

您也可以使用 super() 函数 - 功能 - 通常你会的;基本上是在多个传播方案中需要的.但是因为tkinter使用"老式类" 您必须做这是这里的旧方式.

本文地址:https://www.itbaoku.cn/post/2091143.html

问题描述

in the following code, line 5, what does Frame.__init__ do? Could someone explain the concept behind it? Thanks a lot!

from Tkinter import *
class AppUI(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master, relief=SUNKEN, bd=2)
        [...]

Edit: Full Code with correct indention here

推荐答案

The class AppUI is based on the class Frame from Tkinter. This means the AppUI class is a type of Frame but with some behaviors slightly different or customized. Which means that the methods of the AppUI class may need to (in fact, usually will need to) call code from the Frame class. That is, AppUI wants to do the same thing as the Frame class, and also something else. That's what's happening here: when you instantiate an AppUI, you want it to be initialized as a Frame first, and then perform the AppUI-specific initialization.

Here AppUI explicitly calls its parent class's __init__() method.

You can also do this using the super() function—and usually you would; it's basically required in multiple-inheritance scenarios. But because Tkinter uses "old-style classes", you have to do it the old way here.