0

is there a way I could name the widget based on the argument from the function.

    def display_widget(self, widget_name, value):
            self.sv_[widget_name] = StringVar()
            self.label_[widget_name] = Label(textvariable=self.sv_widget_name)
            self.sv_[widget_name].set(value)
Jack
  • 1
  • 1
    Why not just use a dictionary? – Ignacio Vazquez-Abrams Oct 15 '17 at 23:26
  • So dictionary is the only way? will it be slow if I have a large set of widgets to display? – Jack Oct 15 '17 at 23:35
  • No slower than what you want. – Ignacio Vazquez-Abrams Oct 15 '17 at 23:35
  • 1
    A dictionary is much, much better than dynamic variable names. – Bryan Oakley Oct 16 '17 at 00:05
  • The reason I didn't use dictionary is because the widgets in my code could be label, entry, or optionmenu. Is there an example how I could use dictionary for different widget type with different widget name? Thanks! – Jack Oct 16 '17 at 00:40
  • Please see [Why you don't want to dynamically create variables](http://stupidpythonideas.blogspot.com.au/2013/05/why-you-dont-want-to-dynamically-create.html). Also see [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables). Some of the answers there show how to create dynamic variable names, but the general advice is that it's a bad idea. – PM 2Ring Oct 16 '17 at 02:20
  • `self.button[widget_name] = Button(...)`, or `self.label[widget_name] = Label(...)` – Bryan Oakley Oct 16 '17 at 02:46
  • Thanks very much for that. – Jack Oct 16 '17 at 03:09

0 Answers0