Home > Back-end >  How do you change the text from a canvas create_text in a function
How do you change the text from a canvas create_text in a function

Time:12-23

I've been trying to change text from a button click. I don't know how but if you can either change the text in a function or do it directly from the button, could you please answer if you know how.This is my code.

from time import \*
from random import \*
from math import \*
from tkinter import \*

\#Variables

inter = Tk()
i = Canvas(inter, width=1100, height=650, bg='yellow')
BossHealth = (100)
BHID = i.create_text(170, 30, text=BossHealth, fill='black', fon=('Comic Sans MS', 15))
w = 1100
h = 800
Powers = \['Health 5', 'Damage 10', 'DamageX2'\]

\#Functions

def RanList(LIST):
listlen = len(LIST)
randitem = randint(1, listlen)
randitem = randitem - 1
item = LIST\[randitem\]
return item

def GetPower(Powers):
RanList(Powers)
return powers

def Damage(dmg,h,t):
h = h - dmg
i.itemconfig(i.BHID, text=h)
return h

\#Player Stats

PlayerHealth = (500)
A1Wait = (0)
A2Wait = (1)
A3Wait = (3)
A4Wait = (5)
A1DMG = (5)
A2DMG = (10)
A3DMG = (25)
A4DMG = (50)

\#Boss Stats

BA1Wait = (0)
BA2Wait = (2)
BA3Wait = (4)
BA4Wait = (5)
BA1DMG = (5)
BA2DMG = (10)
BA3DMG = (20)
BA4DMG = (75)

\#Interface

inter.title('Boss Battle')
i.pack()
A1 = Button(inter, height=3, width=6, text='Punch', bg='black',  fg='white', command=Damage(A1DMG, BossHealth, BHID))
A2 = Button(inter, height=3, width=6, text='Rapid\\npunch', bg='black',  fg='white', command=Damage(A2DMG, BossHealth, BHID))
A3 = Button(inter, height=3, width=6, text='Gun', bg='black',  fg='white', command=Damage(A3DMG, BossHealth, BHID))
A4 = Button(inter, height=3, width=6, text='Rocket \\n Launcher', bg='black',  fg='white', command=Damage(A4DMG, BossHealth, BHID))
A1.place(x=950, y=500)
A2.place(x=1025, y=500)
A3.place(x=950, y=575)
A4.place(x=1025, y=575)
i.create_text(90, 30, text='Boss Health: ',fill='black', font=('Comic Sans MS', 15))

I tried searching online but all the answers i found were either for a higher python update(i use python 3.8) or it was for a similar cause.

CodePudding user response:

Typo error in line 31:

Change this:

def Damage(dmg,h,t):
    h = h - dmg
    i.itemconfig(i.BHID, text=h)
    return h

to:

def Damage(dmg,h,t):
    h = h - dmg
    i.itemconfig(BHID, text=h)
    return h

CodePudding user response:

There are issues in your code:

  • command=Damage(...) will execute Damage(...) immediately without clicking the button. Nothing will be performed when the button is clicked later. Use lambda instead: command=lambda:Damage(...)

  • BossHealth is passed as an argument to Damage(). Since Python use pass-by-value for normal integer variable, so updating the argument inside the function will not change BossHealth. Suggest to change BossHealth to IntVar

Below is the modified code (removed unused stuff):

from tkinter import *

#Functions

def Damage(dmg, h, t):
    h.set(h.get() - dmg)
    i.itemconfig(t, text=h.get())

#Player Stats

A1DMG = 5
A2DMG = 10
A3DMG = 25
A4DMG = 50

#Interface

inter = Tk()
inter.title('Boss Battle')

BossHealth = IntVar(value=100) # changed to IntVar

i = Canvas(inter, width=1100, height=650, bg='yellow')
i.pack()

i.create_text(90, 30, text='Boss Health:', fill='black', font=('Comic Sans MS', 15))
BHID = i.create_text(170, 30, text=BossHealth.get(), fill='black', fon=('Comic Sans MS', 15))

# used lambda in command option for below buttons
A1 = Button(inter, height=3, width=6, text='Punch', bg='black', fg='white', command=lambda:Damage(A1DMG, BossHealth, BHID))
A2 = Button(inter, height=3, width=6, text='Rapid\npunch', bg='black', fg='white', command=lambda:Damage(A2DMG, BossHealth, BHID))
A3 = Button(inter, height=3, width=6, text='Gun', bg='black', fg='white', command=lambda:Damage(A3DMG, BossHealth, BHID))
A4 = Button(inter, height=3, width=6, text='Rocket\nLauncher', bg='black', fg='white', command=lambda:Damage(A4DMG, BossHealth, BHID))

A1.place(x=950, y=500)
A2.place(x=1025, y=500)
A3.place(x=950, y=575)
A4.place(x=1025, y=575)

inter.mainloop()
  • Related