MSauer
MSauer

Reputation: 148

AttributeError: class Frame has no attribute 'tk'

I am writing my own Library, so I can use some functions later faster and easier. At the moment, I am working with python's GUI Library Tkinter. (from tkinter include *)

def guiFrameNew(title, width, height):
        guitmp = Tk();
        return guitmp;

def guiTextboxReadonlyNew(frame, width, text):
        guitmp = Entry(Frame, state="readonly", textvariable=text, width=width);
        guitmp.pack();
        return guitmp;

def guiFrameRun(frame):
        frame.mainloop();

This all is in one file (file_one.py).

In an other file (file_two.py) i included this file:

include file_one as f

Code in file_two is:

main = f.guiFrameNew("Test", 0, 0);
main_tbro = f.guiTextboxReadonlyNew(main, 20, "Some Text");
f.guiFrameRun(main);

Yes, I know that I don't need the values Title, width, height in def guiFrameNew because the function does not create a frame.

After I started the file_two.py the python Interpreter says:

> File "file_two", line 5, in <module>
>     main_tbro = f.guiTextboxReadonlyNew(main, 20, "Some Text");   File "/Users/MyUsername/Documents/py/file_two.py", line 190, in
> guiTextboxReadonlyNew
>     guitmp = Entry(Frame, state="readonly", textvariable=text, width=width);   File
> "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
> line 2447, in __init__
>     Widget.__init__(self, master, 'entry', cnf, kw)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
> line 2027, in __init__
>     BaseWidget._setup(self, master, cnf)   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
> line 2005, in _setup
>     self.tk = master.tk AttributeError: class Frame has no attribute 'tk'

I don't know why, because the function def guiTextboxReadonlyNew(...) is similar to the function

def guiTextboxNew(frame, width):
        guitmp = Entry(frame, width=width);
        guitmp.pack();
        return guitmp;

and def guiTextboxNew(...) works!

What is wrong in my file?

Upvotes: 2

Views: 1028

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90889

Assumming by include you mean import (which is really the case, since you are able to import the module file_one).

The Entry() takes a frame object as the first argument, not the Frame class. You should do -

def guiTextboxReadonlyNew(frame, width, text):
        guitmp = Entry(frame, state="readonly", textvariable=text, width=width)
        guitmp.pack()
        return guitmp

Also, there is not really any need for ; (semi-colon) in python after statements.

Upvotes: 4

Related Questions