繁体   English   中英

是否有理由在超级调用中将master传递给__init__?

[英]Is there a reason to pass master to the __init__ in the super call?

如果我想使用tkinter设置一个类来创建一个非常基本的GUI,是否有任何理由将master传递给__init__()

from tkinter import *

class Application(Frame):
    def __init__(self, master):
        #The original example passes master to __init__()
        super(Application, self).__init__(master)
        self.grid()
        self.create_widget()

    def create_widget(self):
        '''put a button "Blank" with no fucntion in the GUI'''
        self.bttn = Button(self, text = "Blank")
        self.bttn.grid()

root = Tk()
root.title("simple GUI")
root.geometry('200x200')

app = Application(root)

root.mainloop()

from tkinter import *

class Application(Frame):
    def __init__(self, master):
        #Code runs fine with out master being passed
        super(Application, self).__init__()
        self.grid()
        self.create_widget()

    def create_widget(self):
        '''put a button "Blank" with no fucntion in the GUI'''
        self.bttn = Button(self, text = "Blank")
        self.bttn.grid()

root = Tk()
root.title("simple GUI")
root.geometry('200x200')

app = Application(root)

root.mainloop()

可能有一些操作需要主窗口访问主根对象。 您当前的应用程序可能有效,但在更复杂的示例中可能无效。 如果教程推荐该方法,那么我认为最好遵循该方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM