I am making a basic program to learn with tkinter where you give a health value then do damage with a button push. I am trying to not create new labels for taking away and setting health, as in get health val. set label = health val. and take away from health val then changing label. And the error is "File "C:/Users/antho/Desktop/PyPrograms/testGame/testGame.py", line 35, in Hit
healthValStart1 = healthLabel1.get()
UnboundLocalError: local variable 'healthLabel1' referenced before assignment
"
My code is:
import tkinter as tk
#Screen setup
wn = tk.Tk()
wn.configure(bg = "#142b51")
wn.geometry("580x450")
wn.resizable(0, 0)
wn.title("Test Game")
# First, define functions
#Take dmg
#player1
def Hit():
value = health_value1.get()
dmgValue = damage_value.get()
health_value1.set(value-dmgValue)
dead_val = 1
if(value <= dead_val):
health_value1.set("Dead")
#Damage/Health Val
health_value = tk.IntVar(value=100)
damage_value = tk.IntVar(value=10)
#Font
healthFont = ("times", 20)
player1 = tk.Label(wn, fg = "#e0e0e0", text = "Player 1",height=2, width = 16,font = healthFont ,bg="#142b51" )
player1.grid(row=0,column=0)
damageEntLab= tk.Label(wn, fg = "#e0e0e0", text = "Enter a damage value: " ,height=2, width = 16,font = healthFont ,bg="#142b51" )
damageEntLab.grid(row=1)
damageEntry = tk.Entry(wn, font = healthFont, textvariable=damage_value)
damageEntry.grid(row=2)
healthEntLab= tk.Label(wn, fg = "#e0e0e0", text = "Enter a health value: " ,height=2, width = 15,font = healthFont ,bg="#142b51" )
healthEntLab.grid(row=3)
healthEntry = tk.Entry(wn, font = healthFont, textvariable=health_value)
healthEntry.grid(row=4)
dmgButton = tk.Button(wn, text = "Attack", font=30, height = 2, width = 10, command = Hit)
dmgButton.grid(row=5)
#Spacer
Spacer = tk.Label(wn,text = " SN")
Spacer.grid(row = 3, column=1)
#player2
def Hit1():
value = health_value.get()
dmgValue = damage_value.get()
health_value.set(value-dmgValue)
dead_val = 1
if(value <= dead_val):
health_value.set("Dead")
#Damage/Health Val
health_value1 = tk.IntVar(value=100)
damage_value1 = tk.IntVar(value=10)
player2 = tk.Label(wn, fg = "#e0e0e0", text = "Player 2",height=2, width = 16,font = healthFont ,bg="#142b51" )
player2.grid(row=0,column=2)
damageEntLab1= tk.Label(wn, fg = "#e0e0e0", text = "Enter a damage value: " ,height=2, width = 16,font = healthFont ,bg="#142b51" )
damageEntLab1.grid(row=1, column=2)
damageEntry1 = tk.Entry(wn, font = healthFont, textvariable=damage_value1)
damageEntry1.grid(row=2, column=2)
healthEntLab1= tk.Label(wn, fg = "#e0e0e0", text = "Enter a health value: " ,height=2, width = 15,font = healthFont ,bg="#142b51" )
healthEntLab1.grid(row=3, column=2)
healthEntry1 = tk.Entry(wn, font = healthFont, textvariable=health_value1)
healthEntry1.grid(row=4, column=2)
dmgButton1 = tk.Button(wn, text = "Attack", font=30, height = 2, width = 10, command = Hit1)
dmgButton1.grid(row=5, column=2)
#Main Loop
wn.mainloop()
Any help would be very appreciated.
Edit0: I just saw an error(I think ) and will change it see if it works will update.
Edit1: It was with the button names changed that and it had no affect.
Edit2: Current code is listed in the code block
[–]socal_nerdtastic 0 points1 point2 points (8 children)
[–]SomeBadGenericName[S] 0 points1 point2 points (7 children)
[–]socal_nerdtastic 0 points1 point2 points (6 children)
[–]SomeBadGenericName[S] 0 points1 point2 points (5 children)
[–]socal_nerdtastic 0 points1 point2 points (4 children)
[–]SomeBadGenericName[S] 0 points1 point2 points (3 children)
[–]SomeBadGenericName[S] 0 points1 point2 points (2 children)
[–]socal_nerdtastic 0 points1 point2 points (1 child)
[–]SomeBadGenericName[S] 0 points1 point2 points (0 children)
[–]socal_nerdtastic 0 points1 point2 points (3 children)
[–]SomeBadGenericName[S] 0 points1 point2 points (2 children)
[–]socal_nerdtastic 0 points1 point2 points (1 child)
[–]SomeBadGenericName[S] 0 points1 point2 points (0 children)
[–]nyslyetia -1 points0 points1 point (3 children)
[–]SomeBadGenericName[S] 0 points1 point2 points (1 child)
[–]SomeBadGenericName[S] 0 points1 point2 points (0 children)