I'm designing a Character Sheet from Dungeons & Dragons in Python, using Tkinter to take care of the graphical interface. However, I wanted to add an element (in this case a "proficiency" in a skill) to a list, if the checkbox corresponding to the same skill is active. The checkbox buttons are working for some commands like exit ("root.destroy") but this in particulary doesn't seem to do anything. I created a class Character, this Character has an empty Proficencies list and to add a proficiency to it, I created a function that did it whenever the matching checkbox value was set to True ("1")
import tkinter as tk
def modifier(score):
score = int(score)
return (score - 10) // 2
class Character:
proficiencies = []
abilities = {"Strength": 12, "Dexterity": 16, "Constitution": 14, "Intelligence": 10, "Wisdom": 16, "Charisma": 9}
skills = {"Athletics": 0, "Acrobatics": 0, "Sleight of Hand": 0, "Stealth": 0, "Arcana": 0, "History": 0,
"Investigation": 0, "Nature": 0, "Religion": 0,"Animal Handling": 0, "Insight": 0, "Medicine": 0,
"Perception": 0, "Survival": 0, "Deception": 0, "Intimidation": 0, "Performance": 0, "Persuasion": 0}
counter = 0
variables = []
skills = []
root = tk.Tk()
def addSkill():
if exec("var" + str(counter) + ".get() == 1"):
Character.proficiencies.append(skills[counter].replace('_', ' '))
elif exec("var" + str(counter) + ".get() == 0"):
pass
for skill in sorted(Character.skills.keys()):
skills.append(skill.replace(" ", "_"))
exec("var" + str(counter) + " = tk.IntVar()")
exec(skill.replace(" ", "") + "= tk.Checkbutton(root, text=skill, variable = var" + str(counter) + ", command = addSkill)")
exec(skill.replace(" ", "") + ".pack()")
variables.append("var" + str(counter))
counter += 1
counter = 0
root.mainloop()
index = 0
for skill in Character.skills:
if index == 0:
ability = "Strength"
elif index >= 1 and index <= 3:
ability = "Dexterity"
elif index >= 4 and index <= 8:
ability = "Intelligence"
elif index >= 9 and index <= 13:
ability = "Wisdom"
elif index >= 14:
ability = "Charisma"
if skill in Character.proficiencies:
Character.skills[skill] = 10 + (modifier(Character.abilities[ability]) + 2) * 2
else:
Character.skills[skill] = 10 + modifier(Character.abilities[ability]) * 2
index += 1