I want each button to be attached to it's variable from whatever iteration of the loop that button was made in.
def clicked():
for widget in root.winfo_children():
widget.destroy()
label=Label(root,text=('clicked'+str(Char1.name)))
label.grid()
nameButton = Button(root, text=Char1.name, command=(clicked))
nameButton.grid()
ageLabel = Label(root, text=Char1.age)
ageLabel.grid(column = columnNo, row = rowNo)
speedLabel = Label(root, text=Char1.speed)
speedLabel.grid(column = columnNo+1, row = rowNo)
hpLabel = Label(root, text=Char1.hp)
hpLabel.grid(column = columnNo+2, row = rowNo)
After each iteartion of the loop, the Char1 variable is updated with new information. How do I get the information from the Char1 variable to be stored so when the button is pressed it outputs "clicked "+whichever button was actaully pressed. At the moment, it doesn't matter which button I press, the output is always "clicked "+whatever the name of the Char1 variable was in the final iteration of the loop. No clue if Iv'e explained that well enough but I hope someone can figure out what I mean.
Here's the full code for reference (copy and pasting messed up the indents also I know it's super messy I haven't got rid of the code from before I started trying to put it into a gui)
import random as r
import os.path
from send2trash import send2trash
from tkinter import *
import time as t
savePath=('C:/Users/sarah/Documents/python/character generator/characters')
characters = []
rowNo=0
root = Tk()
root.title("Character Generator")
root.geometry('1000x500')
for f in os.listdir(savePath):
os.remove(os.path.join(savePath, f))
class Char:
def __init__(self, name, age, speed, hp):
self.name=name
self.age=age
self.speed=speed
self.hp=hp
def CharDef():
global Char1, Char, rowNo
#Defining local variables
speedMin=1
speedMax=2
ageMin=10
ageMax=90
hpMin=5
hpMax=20
columnNo=1
#Calculating midpoints
midpointSpeed = round((int(speedMin)+int(speedMax))/2)
midpointAge = round((int(ageMin)+int(ageMax))/2)
midpointHp = round(int(hpMin)+int(hpMax)/2)
#name
with open('names.txt','r') as f:
allNames=f.read()
names=list(map(str,allNames.split()))
name=r.choice(names).capitalize()
#age
age=r.randint(ageMin,ageMax)
#speed
speed=r.randint(speedMin,speedMax)
if int(round(speed,1)) < int(midpointSpeed):
speed='slow'
elif int(round(speed,1)) >= int(midpointSpeed):
speed='fast'
#health points
hp=r.randint(hpMin,hpMax)
if age > midpointAge and hp > hpMin:
chance=r.randint(1,2)
if chance == 1:
hp=round(90%(hp))
if hp <= 1:
hp=hp+2
Char1 = Char(name,age,speed,hp)
#Is character fast?
if Char1.speed == 'fast':
IsFast = True
elif Char1.speed == 'slow':
IsFast = False
#Is character fit?
if Char1.hp >= midpointHp+-1:
IsFit = True
elif Char1.hp < midpointHp:
IsFit = False
#Is character a good age?
if Char1.age <= midpointAge+-1:
IsYoung = True
elif Char1.age > midpointAge:
IsYoung = False
if IsFit == True and IsFast == True and IsYoung == True:
best = '**'
else:
best=' '
print(best+" "+Char1.name+" "+best)
print(" Age: "+str(Char1.age))
print(" Speed: "+Char1.speed.capitalize())
print(" Health Points: "+str(Char1.hp)+"/"+str(hpMax))
print()
print("--")
print()
#Writing to tkinter window
def clicked():
for widget in root.winfo_children():
widget.destroy()
label=Label(root,text=('clicked'+str(Char1.name)))
label.grid()
nameButton = Button(root, text=Char1.name, command=(clicked))
nameButton.grid()
ageLabel = Label(root, text=Char1.age)
ageLabel.grid(column = columnNo, row = rowNo)
speedLabel = Label(root, text=Char1.speed)
speedLabel.grid(column = columnNo+1, row = rowNo)
hpLabel = Label(root, text=Char1.hp)
hpLabel.grid(column = columnNo+2, row = rowNo)
times=0
while times != 3:
CharDef()
fileName = (str(Char1.name))
fileNameComplete=os.path.join(savePath, fileName+".txt")
f=open(fileNameComplete,'w')
f.write(str(Char1.name.capitalize())+'\n')
f.write(str(Char1.age)+'\n')
f.write(str(Char1.speed)+'\n')
f.write(str(Char1.hp)+'\n')
times+=1
rowNo+=1
f.close()
root.mainloop()
files = os. listdir(savePath)
for file in files:
fileName = file.replace('.txt', "")
characters.append(fileName)
while True:
choice=input("Which character will you pick? ").capitalize()
if choice in characters:
break
elif choice not in characters:
print("invalid")
print()
fileName=os.path.join(savePath, choice+".txt")
with open(fileName,'r') as f:
allInfo=f.read()
info=list(map(str,allInfo.split()))
Char = Char(info[0],info[1],info[2],info[3])
print(Char.name+", "+str(Char.age)+", "+str(Char.speed)+", "+str(Char.hp))