r0w
r0w

Reputation: 165

What is the 'better' approach to configuring a tkinter widget?

I've worked with tkinter a bit of a time now. There are two ways for configuration or at at least I just know two:

1: frame.config(bg='#123456')

2: frame["bg"] = '#123456'

I use the latter more often. Only if there are more things to be done at the same time, the second seems useful for me. Recently I was wondering if one of them is 'better' like faster or has any other advantage.

I don't think it's a crucially important Question but maybe someone knows it.

Upvotes: 2

Views: 136

Answers (1)

Matthew
Matthew

Reputation: 7590

Studying the tkinter code base, we find the following:

class Frame(Widget):
    # Other code here

class Widget(BaseWidget, Pack, Place, Grid):
    pass

class BaseWidget(Misc):
    # other code here

class Misc:
    # various code
    def __setitem__(self, key, value):
        self.configure({key: value})

Therefore, the two methods are actually equivalent. The line

frame['bg'] = '#123456'

is interpreted as frame.__setitem__('bg','#123456'), which after passing through the inheritance chain finds itself on the internal class Misc which simply passes it to the configure method. As far as your question about efficiency is concerned, the first method is probably slightly faster because it doesn't need to be interpreted as much, but the speed difference is too little to be overly concerned with.

Upvotes: 5

Related Questions