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

查看:375
本文介绍了Frame .__ init__是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)
        [...]

此处带有正确缩进的完整代码

推荐答案

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

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.

此处AppUI显式调用其父类的 __init__() 方法.

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

您还可以使用 super() 函数执行此操作,通常你会的;它在多重继承场景中基本上是必需的.但是由于Tkinter使用旧类" ,因此您必须这是这里的老方法.

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.

这篇关于Frame .__ init__是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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