Python - AttributeError: 'str' object has no attribute 'items'

Python - AttributeError: 'str' object has no attribute 'items'

我是 Tkinter 的初学者。我正在尝试制作一个 phone 图书 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

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

我们将不胜感激!

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

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

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

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

你是这个意思吗?

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