[英]How can i give different variable-names to comboboxes in a for-loop using tkinter (python 3)
I am a beginner and working on a program for my raspberry pi 3B + (in python 3).我是一名初学者,正在为我的树莓派 3B +(在 python 3 中)开发一个程序。 I would like to make a program to turn the GPIO pins on and off.
我想编写一个程序来打开和关闭 GPIO 引脚。 I hereby try to use as little code as possible.
我在此尝试使用尽可能少的代码。 However, the problem is that in the for loop that I made, the combobxes don't get their own name, so I can't read them.
但是,问题是在我制作的 for 循环中,组合框没有自己的名字,所以我无法读取它们。 I like to give them the name combo+str(nr) but python doesn't want to do this.
我喜欢给他们起名字 combo+str(nr) 但 python 不想这样做。 I would also like to link an action to each option.
我还想将操作链接到每个选项。 Does anyone know a solution for this?
有谁知道这个的解决方案?
Here is my code:这是我的代码:
#imports--------------------------------------------------
from tkinter import *
from tkinter import ttk
#variabelen and defs--------------------------------------------------
here I want to place the definition (s) for the comboboxes.在这里,我想放置组合框的定义。 Again as short as possible.
再次尽可能短。
#window setup--------------------------------------------------
root = Tk()
root.title("Raspberry Pi - GPIO")
#labels--------------------------------------------------
clm = 0
nr = 1
for a in range (4):
clm = clm+3
for b in range (10):
name = "GPIO"+str(nr)
nr = nr+1
naam = Label(root, text = name)
naam.grid(column = clm, row = b)
#comboboxes--------------------------------------------------
clm = 1
nr = 1
for a in range (4):
clm = clm+3
for b in range (10):
nr = nr+1
optie = ttk.Combobox(root, state="readonly", width = 5, values = ["ON","OFF"])
optie.current(1)
optie.grid(column = clm, row = b)
#white space--------------------------------------------------
clm = 2
for a in range (3):
clm = clm+3
for b in range (10):
naam = Label(root, text = " ")
naam.grid(column = clm, row = b)
#window + mainloop--------------------------------------------------
root.resizable(0,0)
root.mainloop()
sorry if my english is not so good, normally i speak dutch.抱歉,如果我的英语不太好,通常我会说荷兰语。 thank you in advance.
先感谢您。
What you need are references to the objects you created, and it doesn't have to be names.您需要的是对您创建的对象的引用,它不必是名称。 You can use containers like
list
or dict
to store them.您可以使用
list
或dict
之类的容器来存储它们。
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("Raspberry Pi - GPIO")
all_combos = {} #create an empty dictionary
for col in range(4):
all_combos[col] = [] #create a list base on column index
for row in range(1,11):
Label(root,text=f"GPIO{col*10+row}").grid(row=row,column=col*2)
c = ttk.Combobox(root, state="readonly", width = 5, values = ["ON","OFF"])
c.current(1)
c.grid(row=row,column=col*2+1)
all_combos[col].append(c) #add each combobox to the column
root.resizable(0,0)
root.mainloop()
Now you get a dictonary all_combos
which stores all your combobox objects.现在你得到一个字典
all_combos
存储你所有的 combobox 对象。 You can access any specific combobox by passing indexes, eg您可以通过传递索引来访问任何特定的 combobox,例如
print (all_combos[0][5].get())
#OFF
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.