Bjarne stroustoup
Bjarne stroustoup

Reputation: 45

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

I'm a beginner in Tkinter. I'm trying to make a phone book GUI application.

So, I'm just in the beginning step, Here is my source code:

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

As i run, my program gives the following error:

/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

Can anyone please let me know what's wrong here?

HELP WOULD BE APPRECIATED!

Upvotes: 4

Views: 10181

Answers (2)

YJiqdAdwTifMxGR
YJiqdAdwTifMxGR

Reputation: 148

did you mean this?

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

Upvotes: 0

Lafexlos
Lafexlos

Reputation: 7735

You need to say tkinter, what are those "One", "Two" etc.. for.

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

To answer why you need that, you should check how functions and arguments work in python.

Also, you might want to pack your Frame since all your buttons on it and you can use "left" instead of tkinter.LEFT

Upvotes: 7

Related Questions