简体   繁体   English

如何使用 tkinter 显示变量

[英]How to display a variable with tkinter

I wanted if I can do like a create_variable to display a variable with Tkinter.我想我是否可以像 create_variable 那样用 Tkinter 显示一个变量。

player_score = 0
score = Canvas(window_game, width=300, height=40, bg="black")
score.create_text(50, 20, text="SCORE :", fill="white", font=('Courrier'))
score.grid(row=0,column=0)   #x  #y
lives = Canvas(window_game, width=300, height=40, bg="black")
lives.create_text(50, 20, text="LIVES :", fill="white", font=('Courrier'))
lives.grid(row=1,column=0)

Hello i wanted to know how to display the variable "payer_score" next to the "SCORE".您好,我想知道如何在“SCORE”旁边显示变量“payer_score”。 Thanks.谢谢。

You can do this with the canvas itemconfig method.您可以使用 canvas itemconfig方法执行此操作。 First you need make a reference to the canvas item.首先,您需要参考 canvas 项目。 This is the return value of create_text and I've assigned it to score_text .这是create_text的返回值,我已将其分配给score_text

score_text = score.create_text(50, 20, text="SCORE :", fill="white", font=('Courrier'))

This can then be used to update the text然后可以使用它来更新文本

score.itemconfig(score_text, text = "SCORE : " + str(player_score))

You will have to do this every time you want the text to change, it will not change automatically when player_score changes.每次要更改文本时都必须这样做,它不会在player_score更改时自动更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM