【问题标题】:How to display a variable with tkinter如何使用 tkinter 显示变量
【发布时间】:2022-11-15 04:29:53
【问题描述】:

我想我是否可以像 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)

您好,我想知道如何在“SCORE”旁边显示变量“payer_score”。 谢谢。

【问题讨论】:

  • 您能否添加一个预期输入 -> 输出的示例?
  • 为此,您可能会更好地将标签嵌入到画布中。否则,每次乐谱更改时,您都必须继续删除并重新创建文本。然而,要从最基本的意义上回答你的问题~使用fstrings:f'SCORE: {player_score}'

标签: python tkinter


【解决方案1】:

您可以使用画布itemconfig 方法执行此操作。首先,您需要对画布项目进行引用。这是create_text 的返回值,我已将其分配给score_text

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

然后可以使用它来更新文本

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

每次要更改文本时都必须这样做,player_score 更改时它不会自动更改。

【讨论】:

    猜你喜欢
    • 2019-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多