简体   繁体   English

如何使用 tkinter(python 3)在 for 循环中为组合框提供不同的变量名称

[英]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.您可以使用listdict之类的容器来存储它们。

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.

相关问题 如何在循环内将textvariable分配给不同的变量名-Python-Tkinter - How to assign textvariable to different variable names within a loop - Python - Tkinter 如何在循环中创建多个具有不同名称的tkinter小部件? - How can I create multiple tkinter widgets with different names in a loop? 如何在Python中使用for循环或其他迭代方法连接“变量名”? - How to concat 'variable names' with for-loop or other iteration methods in Python? 如何在 Tkinter 中为循环中创建的相同按钮提供唯一名称? - How can I give identical buttons created in a loop unique names in Tkinter? 如何使用“for”循环和时间延迟替换变量? 我正在使用 Python 和 Tkinter - How can I replace a variable using a “for” loop and a time delay? I'm using Python and Tkinter 如何并行化这个 Python for 循环? - How can I parallelize this Python for-loop? 有没有办法通过python-pptx使用for循环保存具有不同名称的多个简报演示文稿? - Is there a way to save multiple power-point presentations with different names using a for-loop through python-pptx? "在 for 循环中创建的组合框中的 python tkinter 参考" - python tkinter reference in comboboxes created in for loop 如何在python中的for循环中创建对象? - How can I create objects in a for-loop in python? 我如何将Java代码转换为python for循环 - how can i convert java code to python for-loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM