Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表中每个项目的变量_Python_Variables_Tkinter - Fatal编程技术网

Python 列表中每个项目的变量

Python 列表中每个项目的变量,python,variables,tkinter,Python,Variables,Tkinter,我有一个名为武器的变量列表,我需要在武器切换系统中为该列表中的每个项目制作单选按钮。(使用Tkinter) 最初我尝试过这样做: for item in Weapons: x = Radiobutton(equipped, text=item['name'], variable=player['weapon'], value=item) x.pack() 但这失败了,因为所有单选按钮的值都相同。 我不知道需要多少按钮,因为这取决于武器内部变量的数量 我如何才能做

我有一个名为武器的变量列表,我需要在武器切换系统中为该列表中的每个项目制作单选按钮。(使用Tkinter)

最初我尝试过这样做:

for item in Weapons:
     x = Radiobutton(equipped, text=item['name'], variable=player['weapon'], 
     value=item)
     x.pack()
但这失败了,因为所有单选按钮的值都相同。 我不知道需要多少按钮,因为这取决于武器内部变量的数量


我如何才能做到这一点?

下面是使用
exec
来完成评论中的要求:

n=1
for item in Weapons:
     exec('x'+str(n)+'= Radiobutton(equipped, text=item['name'], variable=player['weapon'], value=item)'
     exec('x'+str(n)+'.pack()'
     n+=1

下面是使用
exec
按注释中的要求执行的操作:

n=1
for item in Weapons:
     exec('x'+str(n)+'= Radiobutton(equipped, text=item['name'], variable=player['weapon'], value=item)'
     exec('x'+str(n)+'.pack()'
     n+=1

变量
选项的值必须是一个特殊tkinter变量的实例(例如:
StringVar
)。不能在字典中使用普通变量或元素。

变量选项的值必须是一个特殊tkinter变量的实例(例如:
StringVar
)。你不能在字典中使用普通变量或元素。

标签
rpg
用大写字母写着,
不要用于角色扮演游戏
什么是
玩家[‘武器》]
?这是一个特殊的tkinter变量吗?@Bryan Oakley player是一个字典对象,武器是另一个。它指的是当前“装备”的字典对象(武器)。标签
rpg
用大写字母表示,
不用于角色扮演游戏
什么是
玩家['武器]
?这是一个特殊的tkinter变量吗?@Bryan Oakley player是一个字典对象,武器是另一个。它指的是当前“装备”的字典对象(武器)。我想要它,这样我就可以在武器中每n个都有xn=radiobutton(…),所以如果武器中有3个项目,我想要x1、x2和x3,这是我唯一能想到的尝试动态创建变量名的方法,几乎从来都不是一个好主意。将它们存储在字典中要简单得多。无论如何,这并不能解决实际问题,这与radiobutton的
变量
选项的值有关。我希望它能使武器中每n个都有xn=radiobutton(…),所以如果武器中有3个项目,我希望是x1,x2和x3这是我能想到的唯一尝试这种方法动态创建变量名几乎从来都不是一个好主意。将它们存储在字典中要简单得多。无论如何,这并不能解决实际问题,这与radiobutton的
变量
选项的值有关。