簡體   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