Python - AttributeError:' str'对象没有属性' items'

时间:2015-07-11 09:58:37

标签: python tkinter attributeerror

我是Tkinter的初学者。我正在尝试制作电话簿GUI应用程序。

所以,我刚刚开始,这是我的源代码:

#This is my python 'source.py' for learning purpose

from tkinter import Tk
from tkinter import Button
from tkinter import LEFT
from tkinter import Label
from tkinter import Frame
from tkinter import Pack

wn = Tk()
f = Frame(wn)

b1 = Button(f, "One")
b2 = Button(f, "Two")
b3 = Button(f, "Three")

b1.pack(side=LEFT)
b2.pack(side=LEFT)
b3.pack(side=LEFT)

l = Label(wn, "This is my label!")

l.pack()
l.pack()

wn.mainloop()

当我跑步时,我的程序会出现以下错误:

/usr/bin/python3.4 /home/rajendra/PycharmProjects/pythonProject01/myPackage/source.py
Traceback (most recent call last):
  File "/home/rajendra/PycharmProjects/pythonProject01/myPackage/source.py", line 13, in <module>
    b1 = Button(f, "One")
  File "/usr/lib/python3.4/tkinter/__init__.py", line 2164, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "/usr/lib/python3.4/tkinter/__init__.py", line 2090, in __init__
    classes = [(k, v) for k, v in cnf.items() if isinstance(k, type)]
AttributeError: 'str' object has no attribute 'items'

Process finished with exit code 1

任何人都可以让我知道这里有什么问题吗?

帮助会受到赞赏!

2 个答案:

答案 0 :(得分:2)

你需要说tkinter,那些是"One""Two"等等。

Button(f, text="One")
Label(wn, text="This is my label!")

要回答为什么需要它,你应该检查函数和参数在python中的工作方式。

此外,您可能希望打包Frame,因为您的所有按钮都在其上,您可以使用"left"代替tkinter.LEFT

答案 1 :(得分:0)

你是这个意思吗?

from tkinter import Tk
from tkinter import Button
from tkinter import LEFT
from tkinter import Label
from tkinter import Frame
from tkinter import Pack

wn = Tk()
f = Frame(wn)

b1 = Button(f, text="One")# see that i added the text=
# you need the text="text" instead of just placing the text string 
# Tkinter doesn't know what the "One","Two",... is for.
b2 = Button(f, text="Two")
b3 = Button(f, text="Three")

b1.pack(side=LEFT)
b2.pack(side=LEFT)
b3.pack(side=LEFT)

l = Label(wn, "This is my label!")

l.pack()
l.pack()

wn.mainloop()