0

I wanted if I can do like a create_variable to display a variable with 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". Thanks.

2
  • Could you add an example of expected input -> output? Commented Nov 14, 2022 at 19:36
  • You will probably fare better embedding a label into your canvas for this. Otherwise you will have to keep deleting and recreating the text every time the score changes. However, to answer your question in the most basic sense ~ use fstrings: f'SCORE: {player_score}' Commented Nov 14, 2022 at 19:41

1 Answer 1

1

You can do this with the canvas itemconfig method. First you need make a reference to the canvas item. This is the return value of create_text and I've assigned it to 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.

Sign up to request clarification or add additional context in comments.

Comments

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.