Frame .__ init__是做什么的? [英] What does Frame.__init__ do?
问题描述
在下面的代码第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屋!