Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 646
Bjarne stroustoup Avatar asked Sep 03 '25 03:09

Bjarne stroustoup


1 Answers

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

like image 125
Lafexlos Avatar answered Sep 08 '25 00:09

Lafexlos