Problems with creating label in tkinter

时间秒杀一切 提交于 2020-01-11 13:51:05

问题


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)

Any ideas to solve this problem are welcome


回答1:


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


来源:https://stackoverflow.com/questions/29284989/problems-with-creating-label-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!