AttributeError:class Frame没有属性'tk'

时间:2015-07-28 17:45:21

标签: python tkinter

我正在编写自己的库,因此我可以更快,更轻松地使用某些功能。目前,我正在使用python的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();

这一切都在一个文件中(file_one.py)。

在另一个文件(file_two.py)中,我包含了这个文件:

include file_one as f

file_two中的代码是:

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

是的,我知道我不需要Title, width, height中的值def guiFrameNew,因为该函数不会创建框架。

在我启动file_two.py后,python Interpreter说:

> 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'

我不知道为什么,因为函数def guiTextboxReadonlyNew(...)类似于函数

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

def guiTextboxNew(...)有效!

我的档案有什么问题?

1 个答案:

答案 0 :(得分:3)

include假设您import(实际情况如此,因为您可以导入模块file_one)。

Entry()将帧对象作为第一个参数,而不是Frame类。你应该做 -

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

此外,在语句后的python中实际上不需要;(分号)。