Collin Lehmann
Collin Lehmann

Reputation: 69

'str' object has no attribute 'items'

I have to write a so-called "Facharbeit", in which I chose to solve a difficult question of a test(about which I probably have to ask questions later). So, I first had to summon a tkinter window and I added a Label like this:

b = tkinter.Label(GUI,"Facharbeit:Bundeswettbewerbsaufgabe")
b.place(GUI,"top")
``
line 16, b = tkinter.Label(GUI,"Facharbeit:Bundeswettbewerbsaufgabe")
  File "C:\Users\rebec\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 3148, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Users\rebec\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2569, in __init__
    classes = [(k, v) for k, v in cnf.items() if isinstance(k, type)]
AttributeError: 'str' object has no attribute 'items'

So, I made an error in this line:

b = tkinter.Label(GUI,"Facharbeit:Bundeswettbewerbsaufgabe")

But I can't see any errors there, can anyone help me?

Upvotes: 1

Views: 1077

Answers (1)

Mustafa Aydın
Mustafa Aydın

Reputation: 18315

tkinter.Label's signature is:

tkinter.Label(master=None, cnf={}, **kw)

what you did is:

tkinter.Label(GUI, "Facharbeit:Bundeswettbewerbsaufgabe")

Since you passed both arguments positionally, GUI corresponds to master (fine) and "Fachar..." does to cnf (short for configuration) - not fine. In fact, you passed a string to what expects a dictionary, and it attempted to get .items() of it, hence the error.

Either do:

tkinter.Label(GUI, {"text": "Facharbeit:Bundeswettbewerbsaufgabe"})

or

tkinter.Label(GUI, text="Facharbeit:Bundeswettbewerbsaufgabe")

where this latter option is thanks to that **kw part; documentation states what options you can pass there as a keyword argument:

STANDARD OPTIONS

    activebackground, activeforeground, anchor,
    background, bitmap, borderwidth, cursor,
    disabledforeground, font, foreground,
    highlightbackground, highlightcolor,
    highlightthickness, image, justify,
    padx, pady, relief, takefocus, text,      <-- text here!
    textvariable, underline, wraplength

Upvotes: 3

Related Questions