[英]How to assign textvariable to different variable names within a loop - Python - Tkinter
Code: 码:
for i in range(len(self.models_chosen)):
self.test.insert(i, vars())
self.model_entry_dict_text["entry" + str(i)] = ttk.Label(
self.entry_frame,
text="Quantity of: " + self.models_chosen[i]
)
self.model_entry_dict_text["entry" + str(i)].pack(fill=X)
self.model_entry_dict["entry" + str(i)] = Spinbox(self.entry_frame, from_=0, to=2000,)
self.model_entry_dict["entry" + str(i)].pack(fill=X)
self.models_entry_box_created = True
self.old_models = self.models_chosen
What I have done is made a dictionary within a for in range
called - 我所做的是在
for in range
内制作了一个字典-
self.model_entry_dict["entry" + str(i)]
So it would be: 因此它将是:
self.model_entry_dict["entry1"] = Spinbox(self..)
self.model_entry_dict["entry2"] = Spinbox(self..)
self.model_entry_dict["entry3"] = Spinbox(self..)
... ...
depending on the range. 取决于范围。
For each of the spinboxes
that I have created, I need the value from inside them (which is why I have put the text variable option as part of the arguments) - The problem is I don't know how to have a different variable for each spinbox
created due to the spinboxes
being created in a loop. 对于我创建的每个
spinboxes
,我都需要它们内部的值(这就是为什么我将text variable选项作为参数的一部分的原因)-问题是我不知道如何为它使用不同的变量每个spinbox
由于创建到spinboxes
在一个循环中被创建。
Say I put self.textvariable
, all the spinboxes
would have the same value. 说我把
self.textvariable
放进去,所有的spinboxes
都具有相同的值。 An example of what I want to have would be 我想拥有的一个例子是
textvariable = self.variable+i
so: 所以:
self.variable1 to be the text variable for self.model_entry_dict["entry1"]
self.variable2 to be the text variable for self.model_entry_dict["entry2"]
... etc.
To which I then just do: 然后我要做的是:
self.variable1.get()
- And it should get the number that is inside the spinbox
self.variable1.get()
-它应该拿到的是里面的数字spinbox
But I don't think it's possible to add a string/integer to a variable name. 但是我认为不可能在变量名中添加字符串/整数。
Here you can see an image of the models I have chosen in the listbox, I then pressed update button and it generated 3 spinboxes, now I want to retrieve the numbers in the spinboxes (whether it be stored into 3 different variables or in a list/dictionary/array): 在这里,您可以看到我在列表框中选择的模型的图像,然后按“更新”按钮,它生成了3个Spinbox,现在我想检索Spinbox中的数字(无论是存储在3个不同的变量中还是在列表中) / dictionary / array):
http://gyazo.com/a7c9847ec16b16fb443082066b42f890 http://gyazo.com/a7c9847ec16b16fb443082066b42f890
example of what I'm looking for is (using the image provided above): 我正在寻找的示例(使用上面提供的图像):
test1 = self.variable1.get()
print(test1)
output:
1800
test2 = self.variable2.get()
print(test2)
output:
300
test3 = self.variable3.get()
print(test3)
output:
30
I'm sorry if this may seem a little confusing, I started using Python for a week or so, so it's probably sloppy. 很抱歉,这似乎有点令人困惑,我开始使用Python一周左右,所以它可能很草率。 I'm sure there's probably better ways of doing this and different methods.
我确信可能有更好的方法可以做到这一点,也可以有不同的方法。
You don't need to give each variable a name. 您无需为每个变量命名。 Just use a list:
只需使用一个列表:
self.vars = []
for i in range(len(self.models_chosen)):
var = StringVar()
self.vars.append(var)
...
self.model_entry_dict["entry" + str(i)].config(textvariable=var)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.