Luka1
Luka1

Reputation: 71

Problems with creating label in tkinter

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

Upvotes: 0

Views: 211

Answers (1)

Kevin
Kevin

Reputation: 76184

('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')

Upvotes: 5

Related Questions