3

AttributeError: MyGUI instance has no attribute 'tk'

Also, how do I make the created window have a fixed size and not be able to resize with the mouse? Or after changing label value by clicking on button.

My code:

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()

What's wrong? Thanks

2 Answers 2

4

You should invoke the super constructor for Frame. Not sure, but I guess this will set the tk attribute that the quit command relies on. After that, there's no need to create your own Tk() instance.

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

Of course, you will also have to change the constructor calls for your widgets accordingly, e.g.,

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

or better (or at least shorter): set all the attributes directly in the constructors:

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

Also add self.pack() to your constructor.

(Alternatively, you could change the quit command to self.__mainWindow.quit, but I think the above is better style for creating Frames, see e.g. here.)

Sign up to request clarification or add additional context in comments.

Comments

1

This error typically means you are calling SomeTKSuperClass.__init__ and forgetting the first parameter, which must be self. Remember that __init__ is a class method (static function) in this context, not an instance method, which means that you must explicitly pass it self.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.