1

I am curious to know if there is any real difference other than preference between using .config vs [] on a widget to update parameters.

For example if I wish to change the text of a label most would do this:

label.config(text='new')

However one could also do:

label['text'] = 'new'

My assumption is that all of the widgets parameters are stored in a dictionary and that is why this works. Is there any performance difference or is there any reason to use one method over the other?

There does not seam to be any clear documentation I can find on the 2nd option.

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79

1 Answers1

2

I am curious to know if there is any real difference other than preference between using .config vs [] on a widget to update parameters.

No, there is no difference. Using label['text'] literally just calls label.configure(text=...). See the __setitem__ and __getitem__ methods in the tkinter.Misc class.

My assumption is that all of the widgets parameters are stored in a dictionary

No, that's not how it works. The widget parameters are stored internally to the widget, which is implemented in an embedded tcl interpreter. The tkinter widgets merely define __getitem__ to call the configure method.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks Bryan. Would you happen to have a link to the documentation that explains that `[]` calls the `configure` method? Or a link on the `__getitem__` as I would like to read on it further. – Mike - SMT Nov 04 '19 at 16:40
  • I should have known better on my assumption. I know how a class is structured I am not sure why I thought it was a dictionary. Maybe because that is one way to edit the value of a key in a dict so I just thought dictionary. – Mike - SMT Nov 04 '19 at 16:42
  • NVM I found this post on [__getitem__](https://stackoverflow.com/questions/43627405/understanding-getitem-method). Thanks again. – Mike - SMT Nov 04 '19 at 16:44