Question

I'm using Python and Tkinter and was wondering whether there is anyway I could do something like this because currently it gives me an error. I would like to be able to configure a widget with user input as a string to decide what to configure, hence why the variable 'string_variable' needs to be a string.

tk_widget.config(string_variable = variable)

At the moment, I get an error saying "TclError: unknown option -'string_of_stringvar_here'" Please help me! Thank you for your replies in advance. - Ed

Was it helpful?

Solution

You can treat a widget like a dictionary, with attributes being keys. For example:

label = tk.Label(root)
...
some_attribute = "background"
some_value = "red"
label[some_attribute] = some_value

You can also build up a dictionary of attributes and values, and pass that to the config method:

values = {some_attribute: some_value}
label.config(values)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top