pxc3110
pxc3110

Reputation: 243

AttributeError: 'str' object has no attribute 'keys'

The following is a simple tkinter program that displays an empty window:

# firstTkinter2.py
from Tkinter import Tk, Label
top = Tk()
l = Label(top, "Hello World")
l.pack()
# Give the window a title.
top.title("My App")
# Change the minimum size.
top.minsize(400, 400)
# Change the background colour.
top.configure(bg = "green")
# Run the widget.
top.mainloop()

I run the above code but it encountered an error:

zhiwei@zhiwei-Lenovo-Rescuer-15ISK:~/Documents/python programs/tkinter$ python tk2.py
Traceback (most recent call last):
  File "tk2.py", line 4, in <module>
    l = Label(top, "Hello World")
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2600, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 2094, in __init__
    for k in cnf.keys():
AttributeError: 'str' object has no attribute 'keys'

How can I fix this error and let it run properly?

Upvotes: 1

Views: 9420

Answers (2)

user8060120
user8060120

Reputation:

Try to replace:

l = Label(top, "Hello World")

on

l = Label(top, text="Hello World")
#              ^^^^^^^

Upvotes: 4

ChristianFigueroa
ChristianFigueroa

Reputation: 804

Use Label(top, text="Hello World")

You have to use the text argument.

Upvotes: 1

Related Questions