I'm trying to create a medical calculator tool for risk assessment and prognosis using check buttons in Tkinter. Each item checked should increase the risk score by one point, which changes the score in real time in the window simultaneously as check buttons are checked (that's the ultimate goal). I created the check buttons using a loop, but made sure to store every instance of tk.IntVar() separately in a list, so that when a button is checked, it's corresponding IntVar instance is assigned to the checkbutton variable, then this IntVar is called into a function and it's value is collected using .get method, and is added to the total score (or at least that was the theory). This is the code:
from tkinter import ttk
root=tk.Tk()
root.geometry('600x400')
root.resizable(False,False)
check_frame= ttk.Frame(root, padding=(10,50,10,5))
check_frame.grid(row=0, column=0, columnspan=3)
for x in range(18):
check_frame.rowconfigure(x, weight=1, minsize=50)
check_frame.columnconfigure(x, weight=1, minsize=50)
score=0
Int_Var=[] # This is the list where IntVar() for each check button is collected
# The following function calls IntVar value using IntVar.get() and adds it to the total score
def change_score(x):
global score
m= x.get()
score+=m
print(m, score)
# This is the list for the text items of the check buttons
admission=['Age in years > 55 years', 'WBC count > 16000 cells/mm3', 'Blood glucose > 11.11 mmol/L (> 200 mg/dL)', 'Serum AST > 250 IU/L', 'Serum LDH > 350 IU/L']
# This is the loop that creates the check buttons
# Each tk.IntVar() instance is assigned to a variable (point) and appended to Int_Var list
# Finally change_score() function is called on the check button's corresponding IntVar value
# found in the Int_Var list
for i in range(len(admission)):
point=tk.IntVar()
Int_Var.append(point)
checkbox= tk.Checkbutton(check_frame, text=admission[i], variable= point, onvalue=1, offvalue=0, command= lambda : change_score(Int_Var[i]))
checkbox.grid(row=i)
root.mainloop()
This is what happens when i activate the check buttons, the IntVar value appears to be zero, as does the total score (printed side to side) even though I checked 3 boxes in the example:
However, when i close the window and terminate the program, then check for the registered IntVar values in the Int_Var list, this is what I find!! :
The IntVar values were correctly registered in the Int_Var list!!!
I don't seem to understand what's going on exactly or where is the problem. I would very much appreciate the help. Thank you.
*Note: This is my first question on Stack Overflow. I apologize beforehand if I broke any rules or traditions, and for the long post with too much details....