【问题标题】:Python - AttributeError: 'str' object has no attribute 'items'Python - AttributeError:'str'对象没有属性'items'
【发布时间】:2015-09-30 02:18:38
【问题描述】:

我是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

谁能告诉我这里出了什么问题?

我们将不胜感激!

【问题讨论】:

    标签: python tkinter attributeerror


    【解决方案1】:

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

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

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

    另外,您可能想要打包您的Frame,因为您的所有按钮都在上面,您可以使用"left" 而不是tkinter.LEFT

    【讨论】:

    • 我照做了,但是no luck :(
    • 谢谢.. 终于成功了!! :) 在2 minutes 之后我会接受你的回答。 :)
    • @Bjarnestroustoup 你不完全是 Bjarne,如果你没有自己发现的话:D
    【解决方案2】:

    你是这个意思吗?

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

    【讨论】:

    • 请简要说明
    猜你喜欢
    • 2020-09-22
    • 2013-09-22
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    • 2021-10-22
    • 2020-06-25
    相关资源
    最近更新 更多