2013-10-23 81 views
1

AttributeError: MyGUI instance has no attribute 'tk'Python Tkinter實例沒有屬性'tk'

另外,如何讓創建的窗口具有固定的大小,並且不能用鼠標調整大小?或者點擊按鈕更改標籤值後。

我的代碼:

from Tkinter import* 

class MyGUI(Frame): 

    def __init__(self): 
     self.__mainWindow = Tk()  

    #lbl 
     self.labelText = 'label message' 
     self.depositLabel = Label(self.__mainWindow, text = self.labelText) 

    #buttons 
     self.hi_there = Button(self.__mainWindow) 
     self.hi_there["text"] = "Hello", 
     self.hi_there["command"] = self.testeo 

     self.QUIT = Button(self.__mainWindow) 
     self.QUIT["text"] = "QUIT" 
     self.QUIT["fg"] = "red" 
     self.QUIT["command"] = self.quit 

    #place on view 
     self.depositLabel.pack() 
     self.hi_there.pack() #placed in order! 
     self.QUIT.pack() 

    #What does it do? 
     mainloop() 

    def testeo(self): 

     self.depositLabel['text'] = 'c2value' 

     print "testeo" 

    def depositCallBack(self,event): 
     self.labelText = 'change the value' 
     print(self.labelText) 
     self.depositLabel['text'] = 'change the value' 

myGUI = MyGUI() 

有什麼不對? 謝謝

回答

4

您應該調用Frame的超級構造函數。不確定,但我想這會設置quit命令依賴的tk屬性。之後,不需要創建自己的Tk()實例。

def __init__(self): 
    Frame.__init__(self) 
    # self.__mainWindow = Tk() 

當然,你也將不得不改變的構造你的部件相應要求,例如,

self.hi_there = Button(self) # self, not self.__mainWindow 

或更好(或者至少更短):直接在構造函數設置的所有屬性:

self.hi_there = Button(self, text="Hello", command=self.testeo) 

還可以將self.pack()添加到您的構造函數中。

(或者,你可以改變quit命令self.__mainWindow.quit,但我認爲上面是更好的形式來製作框架,例如見here

相關問題