我正在使用Python设计《龙与地下城》中的角色表,使用Tkinter来处理图形界面。但是,如果对应于同一技能的复选框处于活动状态,我想向列表中添加元素(在本例中为“技能”)。 复选框按钮适用于某些命令,例如exit(“ root.destroy”),但是,这似乎没有任何作用。 我创建了一个Character类,该Character具有一个空的Proficencies列表,并且要添加一个熟练程度,我创建了一个函数,只要将匹配的复选框的值设置为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
答案 0 :(得分:0)
对于遇到相同错误的人,我想我已经找到了错误。 代替:
#include <stdio.h>
int var1 = 94;
int var2 = 76;
int main(void)
{
void *var1Loc = &var1;
void *var2Loc = &var2;
printf("var1 address is: %p\n", var1Loc);
printf("var2 address is: %p\n", var2Loc);
printf("diff is: %td\n", &var2 - &var1);
return 0;
}
我写道:
def addSkill():
if exec("var" + str(counter) + ".get() == 1"):
Character.proficiencies.append(skills[counter].replace('_', ' '))
elif exec("var" + str(counter) + ".get() == 0"):
pass
现在它可以工作了:)
答案 1 :(得分:0)
下面是一个示例,该示例遵循了布莱恩·奥克利(Bryan Oakley)的建议,避免使用exec()
而不是动态创建命名变量,我认为您会同意它比您所使用的代码更容易阅读和理解。
import tkinter as tk
SKILL_NAMES = ('Athletics', 'Acrobatics', 'Sleight of Hand', 'Stealth', 'Arcana',
'History', 'Investigation', 'Nature', 'Religion', 'Animal Handling',
'Insight', 'Medicine', 'Perception', 'Survival', 'Deception',
'Intimidation', 'Performance', 'Persuasion')
class Character:
proficiencies = []
abilities = {"Strength": 12, "Dexterity": 16, "Constitution": 14,
"Intelligence": 10, "Wisdom": 16, "Charisma": 9}
skills = dict.fromkeys(SKILL_NAMES, 0)
def modifier(score):
return (int(score) - 10) // 2
root = tk.Tk()
# Create tk.IntVars for holding value of each skill.
skill_vars = {skill: tk.IntVar() for skill in Character.skills}
# tkinter Checkbutton callback.
def addSkill(skill, var):
""" Add skill to proficiencies if Checkbutton is now checked. """
if var.get() == 1:
Character.proficiencies.append(skill)
# Create a Checkbutton corresponding to each skill.
for skill in Character.skills:
btn = tk.Checkbutton(root, text=skill, variable=skill_vars[skill],
command=lambda name=skill, var=skill_vars[skill]: addSkill(name, var))
btn.pack()
root.mainloop()
for index, skill in enumerate(Character.skills):
if index == 0:
ability = "Strength"
elif 1 <= index <= 3:
ability = "Dexterity"
elif 4 <= index <= 8:
ability = "Intelligence"
elif 9 <= 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