Reputation: 1646
I'm stuck with a script in Python 3. I want to append additional text to already existing label in tkinter.
I attempted this:
def labelConfig(string,append=False):
if append:
label.configure(text+=string)
else:
label.configure(text=string)
But it won't compile... How can I do this properly?
Upvotes: 0
Views: 10842
Reputation: 20679
Apart from Bryan Oakley's answer, it also possible to use +=
if you access the text of the label as a value from a dictionary:
def labelConfig(string,append=False):
if append:
label['text'] += string
else:
label['text'] = string
All options that can be getted or setted with configure
have the equivalent syntax widget['option'] = value
, which can be used in situations like this one.
Upvotes: 4
Reputation: 385980
This isn't a tkinter problem, this applies to all of python. You cannot use +=
when setting a positional argument in a function call. Instead, you must get the value, modify it however you want, then assign the new value to the widget.
For example:
def labelConfig(string,append=False):
if append:
text = label.cget("text") + string
label.configure(text=text)
else:
label.configure(text=string)
Upvotes: 3
Reputation: 133
the text+=string
is a statement so it will not return anything. You will need to first get value from label, then concatenate those strings and then pass them as argument
Upvotes: 0