【问题标题】:What is the purpose of master and master=none in an init function in python?python的init函数中master和master=none的目的是什么?
【发布时间】:2013-08-05 21:19:06
【问题描述】:

我会假设原因与我得到的 self 相似。为什么 master 在那里?另外,为什么有时是master,有时是master = none?

例如

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

来自 Tkinter 8.5 参考:John W. Shipman 的 Python 图形用户界面。此外,文档使用 python2,而我将使用 python3。我需要主人吗? 更多问题 我尝试在 master 之后添加自定义 arg,它说我无法在默认值之后添加非默认 arg,我应该如何解决?不是默认的 arg self 必须是第一个吗?

def __init__(self, master=None,inputDict):
    tk.Frame.__init__(self, master)
    self.grid(sticky=tk.N+tk.S+tk.E+tk.W)
    self.createWidgets()
def createWidgets(self,inputDict):
    top=self.winfo_toplevel()
    top.rowconfigure(0, weight=1)
    top.columnconfigure(0, weight=1)
    self.rowconfigure(0, weight=1)
    self.columnconfigure(0, weight=1)
tempDict = {}
for k,v in inputDict.items():
        if 1<=v[0]<=3:
            tempDict[k] = static_keys(*v[1:])
        elif v[0] ==4:
            tempDict[k] = dynamic_keys(*v[1:])
        elif  v[0]==5:
            tempDict[k] = key_labels(*v[1:])
return tempDict

【问题讨论】:

    标签: python class python-2.7 python-3.x tkinter


    【解决方案1】:

    您的Application 类是tk.Frame 的子类。 master 参数是父窗口小部件,它是一个可选参数(默认为none),在初始化时将传递给Application 类的新实例。

    Take a look here for more info.

    【讨论】:

    • 应该叫父级。除非我允许,否则我会的。
    • 在调用初始化函数时,如果 def 后面没有 =0,我还需要指定一个主控吗?自己呢?
    • @fozbstuios 抱歉回答迟了...self 是指向存储新实例的变量的指针。所以在a=Application() 中,self 将指向a。如果def __init__() 没有=None,则master 参数将成为必需的
    • 我可以用 parent 代替 master=None 吗?
    • @SandeepPrasadKushwaha 如果我理解正确,您想将您的 Application 对象放在您拥有的另一个父对象下,对吧?在那种情况下,只需表明我在做master=your_parent_object
    猜你喜欢
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-02
    • 1970-01-01
    • 2011-08-07
    • 2011-07-13
    相关资源
    最近更新 更多