0

I create simple label in tkinter but it is created with {}, what I don't want to.

gameOver=Label(root, text=('Game over!\nYou scored', number, ' points!'),
                               font=('Arial Black', '26'), bg='red')

That is my code, where number is variable. But it prints "{Game over! You scored} 0 {points!}" That is what get with this code (0 is value of number)

enter image description here

Any ideas to solve this problem are welcome

1 Answer 1

5

('Game over!\nYou scored', number, ' points!') is a tuple of three items, but text probably expects a string instead, and does strange things to arguments of other types. Use string concatenation or format to provide a single string.

gameOver=Label(root, text='Game over!\nYou scored' + str(number) + ' points!',
                           font=('Arial Black', '26'), bg='red')

Or

gameOver=Label(root, text='Game over!\nYou scored {} points!'.format(number),
                           font=('Arial Black', '26'), bg='red')
Sign up to request clarification or add additional context in comments.

3 Comments

I tried first suggestion and it works great. Thanks for help.
Just FYI, the issue here is not that you passed in a tuple, it's because the strings in that tuple had whitespace. Tkinter groups the individual strings in this case to indicate which one contains this space. If you pass in a tuple with no whitepsace, e.g. text=('few', 'words', 'no', 'spaces'), Tkinter will automatically populate the label with spaces between each element of the tuple.
@Jkdc: it's a tiny bit more complicated than that. For example, if your data has a curly brace you'll also get unexpected output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.