2017-10-16 86 views
-1

我有一個很大的問題使用tkinter與self這裏是我的代碼 請人給一個答案,謝謝!我得到的錯誤是,像self could not be given a variable outside a functionpython調用類似變量

from tkinter import * 
root = Tk() 

class start(): 
    global self 
    self = root 

    def __init__(): 
     self.title('__init__') 
     self.geometry('300x300') 

    __init__(self) 

class window_extra(): 

    def canvas(self): 
     global self 
     selfc = Canvas(self, bg='black').pack() 

    canvas(self) 

self.mainloop() 

謝謝!

+0

是什麼問題?我得到的錯誤與'canvas'中的'self'和'global self'有關。你可以將論點重新命名爲任何你想要的,但是我真的不知道你打算做什麼。你能解釋你的目標嗎? – ryachza

+3

你想在這裏實現什麼樣的世界? –

+0

@ Guydangerous99'全球自我'是一個非常糟糕的主意。一個類中的'self'變量是神聖的,不應該被覆蓋! – slightlynybbled

回答

3

您不應該使用self作爲變量名稱,因爲它用於指定是否某個屬性是類實例的屬性。

您不需要在類中使用全局,因爲在處理通過類需要的變量時,在大多數情況下使用類屬性。

由你所示的代碼,我認爲你正在嘗試做這樣的判斷:

from tkinter import * 

class start(): 

    def __init__(self, root): 
     self.master = root 
     self.master.title('__init__') 
     self.master.geometry('300x300') 
     Canvas(self.master, bg='black').pack() 

root = Tk() 
start(root) 
root.mainloop() 

不過我相信你與編程的面向對象方法掙扎,我建議不使用OOP來如果是這種情況,請從此開始。

也許在YouTube上找幾個教程或點擊Codecadamy。

在回答您的評論:

在我看來,在使用init正確是一個壞主意。我用它作爲一個常規def。我不關心,如果我使用自我全局,除非函數/類變量被稱爲自我。

我尊重的正確使用初始化,但我只是找到,INIT和self.master整個事情我只是沒有得到任何的吧!

不理解事物並不意味着說事情是壞的。使用self.master是爲了提供一個綁定回Tk()變量的類屬性。這允許類中的任何方法與Tk()的實例進行交互。我無法與其他編程語言交流,但使用self在python的OOP中是非常重要的。保留self用於引用對象的實例或類屬性可能不是100%,但它是self的接受和已知使用,並且確實不應該更改/覆蓋。

+0

我唯一遇到的問題是「你不能用自己作爲變量名......」。我不認爲「self」這個名字有什麼特別之處,但也許這就是提問者所缺少的 - 它只是第一個(隱含的)參數,而名字本身並不重要。 – ryachza

+0

@ryachza夠公平的。我已經更新了我的答案,說'不應該使用'而不是'cannot'。 –

+0

@ryachza:你是對的,除了幾乎每個在這個星球上的python程序員都會認爲它指的是一個對象的實例之外,沒有什麼特別的'self'。雖然你可以用任何你認爲合適的方式使用「self」這個名字,但最好不要選擇一個能夠混淆大多數程序員的名字。 –

0

我重構了一些簡單的內容,但我認爲在進一步深入GUI路線之前,您需要更好地理解Python中的對象。我認爲你的意思是這樣的:

from tkinter import * 

# creates a subclass of Tk() called 'Application'; class names by convention 
# use CamelCase; best to stick with convention on this one 

class Application(tkinter.Tk): 

    # you don't have to create an explicit invocation of '__init__', it 
    # is automatically run when you instantiate your class (last line) 

    def __init__(): 
     super().__init__() # initialize the super class (the 'tkinter.Tk()') 

     self.title('__init__') # use 'self' to refer to this object - do not declare it as global! it is only valid within the object! 
     self.geometry('300x300') 

     self.my_canvas = tkinter.Canvas(self, bg='black') # save an instance of your canvas for easy reference later 
     self.my_canvas.pack() # pack as a separate step (only required if you plan to use the canvas later... or ever) 

     self.mainloop() # begin the tkinter loop 

# this 'if __name__ ...' is a good idea in most cases, allows you to import `Application` into other 
# files without actually running it unless you want to from that other file 

if __name__ == '__main__': 
    Application() # start your class 
+0

您應該指出,如果您稍後要直接更新畫布對象,則僅需要在單獨一行中進行打包。 –

+1

@ Mike-SMT編輯過,但很難想象一個畫布創建時不想使用的情景。 – slightlynybbled

+0

但我同意它更多地使用pack(),而不是它在畫布上的事實。如果你打算說在一個單獨的行上使用pack,那麼你應該有一些例子說明爲什麼在某些情況下這是必需的。 OP顯然對Tkinter來說是新的,這是可以幫助他們前進的信息。 –