-1

I have a dictionary that contains tkinter labels, but below I'm only including the first part of the dictionary

When I use variables, in the example below I get no errors.

from tkinter import *

root = Tk()

dic = {'response1':Label(root, bg='white')}

lbl = dic['response1']
lbl.config(text='Hey')
lbl.pack()

mainloop()

But when I do it without variables, like this

from tkinter import *

root = Tk()

dic = {'response1':Label(root, bg='white')}

dic['response1'].config(text='Hey').pack()

mainloop()

I get this error

AttributeError: 'NoneType' object has no attribute 'pack'

and since that, I need to declare a variable for each label in the dictionary so that I can avoid this error. So I'm asking how to declare a variable for each item in dictionary, where the key is the variable name. So response1 = Label(root, bg='white') and so on for each item in the dictionary.

IsaacK0
  • 39
  • 5
  • 2
    Why not just leave them as dictionary values? – wim Jan 02 '20 at 20:01
  • @wim Because I need them as a variable type, the actual dict contains of tkinter buttons that I need to be able to do operations on. And if I just do dict['key'] I can not perform operations on them – IsaacK0 Jan 02 '20 at 20:03
  • What sort of operation can you do on a local variable that you can't do on a dictionary value? – wim Jan 02 '20 at 20:03
  • @wim I have them as tkinter buttons so I need to be able to operations like `.config(text='Some text')` and `var1.pack()`. Otherwise, if I do `dict['var1'].pack()`, I get this error: `AttributeError: 'NoneType' object has no attribute 'pack'` – IsaacK0 Jan 02 '20 at 20:06
  • Perhaps you can post the [mcve] which demonstrates such an `AttributeError`. – wim Jan 02 '20 at 20:07
  • @IsaacK0 please supply the code you tried to do that with. I'm assuming there's some different error in your code. There should be no difference between a dictionary reference and regular one. – OsmosisJonesLoL Jan 02 '20 at 20:08
  • @OsmosisJonesLoL I will update my question – IsaacK0 Jan 02 '20 at 20:09
  • @IsaacK0 just looked up a little of tkinter to know what you are talking about pack is a method on tkinter widgets, even if you did `var1.pack()` I'd be skeptical of it working correctly. The assumption if you are making buttons they would look like `button = Button(frame, text=var1)` or `button = Button(frame, text=dict['var1'])` and then you can call `button.pack()` – OsmosisJonesLoL Jan 02 '20 at 20:16
  • @OsmosisJonesLoL Now it's updated, I tried to show a bit about what you looked up above – IsaacK0 Jan 02 '20 at 20:20
  • 1
    @wim The question is updated now – IsaacK0 Jan 02 '20 at 20:25
  • I've modified my answer to reflect your updated question. – OsmosisJonesLoL Jan 02 '20 at 20:30

1 Answers1

2

You don't need to use variables, your error actually comes from you trying to use the results of .config, to .pack.

Try the following:

from tkinter import *

root = Tk()

dic = {'response1':Label(root, bg='white')}

dic['response1'].config(text='Hey')
dic['response1'].pack()

mainloop()
OsmosisJonesLoL
  • 244
  • 1
  • 4