简体   繁体   English

我们如何使用循环从数组创建复选按钮并打印所选复选按钮的值?

[英]How can we use a loop to create checkbuttons from an array and print the values of the selected checkbutton(s)?

I have an array of strings, and I want to be able to use a loop to quickly create a lot of checkbuttons for them, because the idea is that the user can later add/delete items in the array, so it should be adaptable.我有一个字符串数组,我希望能够使用循环为它们快速创建很多复选按钮,因为这个想法是用户以后可以添加/删除数组中的项目,所以它应该是适应性强的。

I'm not even sure if this is possible with the method I'm trying to use.我什至不确定我尝试使用的方法是否可行。 The problem with the code below is that it only checks the very last checkbutton/the very last item in the array, so it always returns PY_VAR3 or 'd' etc.下面代码的问题是它只检查数组中的最后一个检查按钮/最后一个项目,所以它总是返回 PY_VAR3 或 'd' 等。

It would be amazing if someone could help me understand what to do, even if it's a complete rewrite of the code.如果有人能帮助我理解该做什么,那将是令人惊奇的,即使这是对代码的完全重写。 I'm completely stumped.我完全被难住了。

from Tkinter import *


Window = Tk()

class Test:
    def __init__(self):

        array = ['a', 'b', 'c', 'd']

        def doCheckbutton():
            for i in array:
                self.var = StringVar()
                c = Checkbutton(Window, text='blah', variable=self.var, command=printSelection)
                c.pack()


        def printSelection():
            print(self.var)

        doCheckbutton()

Test()

Window.mainloop()

Solved解决了

from Tkinter import *


Window = Tk()

class Test:
    def __init__(self):

        self.array = ['a', 'b', 'c', 'd']
        self.vars = [] #Array for saved values

        self.doCheckbutton()

    def doCheckbutton(self):
        for i in range(len(self.array)):
            self.vars.append(StringVar()) #create new item in vars array
            c = Checkbutton(Window, text=self.array[i], variable=self.vars[-1], command=lambda i=i: self.printSelection(i), onvalue='on', offvalue='off')
            c.pack()


    def printSelection(self, i):
        print(self.array[i] + ': ' + self.vars[i].get())

Test()

Window.mainloop()

When a checkbutton is ticked/unticked, it prints out statements such as: c: on c: off当勾选/取消勾选一个复选按钮时,它会打印出如下语句: c: on c: off

You can create for each CheckBox a StringVar and save them in a list then Use get method on StringVar to get its value ( lambda is used to passe the index in array list):您可以为每个CheckBox创建一个StringVar并将它们保存在一个列表中,然后在StringVar上使用get方法来获取它的值( lambda用于传递数组列表中的索引):

from Tkinter import *


Window = Tk()

class Test:
    def __init__(self):

        self.array = ['a', 'b', 'c', 'd']
        self.vars = []

        self.doCheckbutton()

    def doCheckbutton(self):
        for i in range(len(self.array)):
            self.vars.append(StringVar())
            self.vars[-1].set(0)
            c = Checkbutton(Window, text=self.array[i], variable=self.vars[-1], command=lambda i=i: self.printSelection(i), onvalue=1, offvalue=0)
            c.pack()


    def printSelection(self, i):
        print(self.vars[i].get())

Test()

Window.mainloop()

I hope this will be helpful.我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM