Why Am I Getting Ugly Curly Brackets Around My Text In The Label Widget? - Tkinter
I'm getting curly brackets around the text in my label widget. The output is {Total tries: 0} instead of Total tries: 0. Here is a short version of my code: class Cell: def che
Solution 1:
self.label["text"] = "Total tries: 0",
There is a comma at the end of the line. The comma changes the value being assigned to self.label["text"]
from a string to a tuple. Remove the comma, and the curly braces get removed.
Solution 2:
I don't know why that happens; however, when I've used Tkinter, I've always done text updates either with a StringVar
or using the config
method. Here's a page with some examples.
Example using a StringVar
:
# in class Memorydefcreate_widgets(self):
self.labelText = StringVar()
self.label = Label(self, textvariable = self.labelText)
... rest of method ...
defupdate_tries(self):
self.labelText.set("Total tries: " + str(self.tries))
Solution 3:
I had a similar problem but none of these solutions worked for me. For anyone in the future that struggles with this, I get an error on text that contains a space in such as "Health and Fitness"
which would be printed as {Health and Fitness}
.
For me, the solution was to not instantiate the labels such as:
score = 25
tk.Label(container, text=("Health and Fitness",score)).pack()
But rather, like this:
toPrint = "Health and Fitness" + str(score)
tk.Label(container, text=toPrint).pack()
Post a Comment for "Why Am I Getting Ugly Curly Brackets Around My Text In The Label Widget? - Tkinter"