Python 向滚动文本小部件添加复选按钮列表

Python 向滚动文本小部件添加复选按钮列表,python,tkinter,Python,Tkinter,我需要有一个滚动文本小部件内的checkbuttons,每行一个checkbutton,每个按钮后面有一些文本。我发现此解决方案存在堆栈溢出: 这里有一个被剪掉的代码: for i in range(1000): bg = 'grey' if i % 2 == 0: bg = 'white' cb = tk.Checkbutton(text="checkbutton #%s" % i, bg=bg, width=35, justify=tk.LEFT)

我需要有一个滚动文本小部件内的checkbuttons,每行一个checkbutton,每个按钮后面有一些文本。我发现此解决方案存在堆栈溢出:

这里有一个被剪掉的代码:

for i in range(1000):
    bg = 'grey'
    if i % 2 == 0:
        bg = 'white'
    cb = tk.Checkbutton(text="checkbutton #%s" % i, bg=bg, width=35, justify=tk.LEFT)
    text.window_create("end", window=cb)
    text.insert("end", "\n") # to force one checkbox per line

这对我来说没有多大意义,因为虽然复选按钮显示正确,但您无法访问它们中的每一个。还是我错了?

与任何其他python对象一样,为了在其上调用方法,您需要有一个引用。最简单的解决方案是在列表或字典中保留对小部件和变量的引用

例如:

import tkinter as tk

class Example(object):
    def __init__(self):

        root = tk.Tk()
        text = tk.Text(root, cursor="arrow")
        vsb = tk.Scrollbar(root, command=text.yview)
        button = tk.Button(root, text="Get Values", command=self.get_values)
        text.configure(yscrollcommand=vsb.set)

        button.pack(side="top")
        vsb.pack(side="right", fill="y")
        text.pack(side="left", fill="both", expand=True)

        self.checkbuttons = []
        self.vars = []
        for i in range(20):
            var = tk.IntVar(value=0)
            cb = tk.Checkbutton(text, text="checkbutton #%s" % i,
                                variable=var, onvalue=1, offvalue=0)
            text.window_create("end", window=cb)
            text.insert("end", "\n")
            self.checkbuttons.append(cb)
            self.vars.append(var)
        text.configure(state="disabled")

        root.mainloop()

    def get_values(self):
        for cb, var in zip(self.checkbuttons, self.vars):
            text = cb.cget("text")
            value = var.get()
            print("%s: %d" % (text, value))

if __name__ == "__main__":
    Example()

与任何其他python对象一样,为了对其调用方法,需要有一个引用。最简单的解决方案是在列表或字典中保留对小部件和变量的引用

例如:

import tkinter as tk

class Example(object):
    def __init__(self):

        root = tk.Tk()
        text = tk.Text(root, cursor="arrow")
        vsb = tk.Scrollbar(root, command=text.yview)
        button = tk.Button(root, text="Get Values", command=self.get_values)
        text.configure(yscrollcommand=vsb.set)

        button.pack(side="top")
        vsb.pack(side="right", fill="y")
        text.pack(side="left", fill="both", expand=True)

        self.checkbuttons = []
        self.vars = []
        for i in range(20):
            var = tk.IntVar(value=0)
            cb = tk.Checkbutton(text, text="checkbutton #%s" % i,
                                variable=var, onvalue=1, offvalue=0)
            text.window_create("end", window=cb)
            text.insert("end", "\n")
            self.checkbuttons.append(cb)
            self.vars.append(var)
        text.configure(state="disabled")

        root.mainloop()

    def get_values(self):
        for cb, var in zip(self.checkbuttons, self.vars):
            text = cb.cget("text")
            value = var.get()
            print("%s: %d" % (text, value))

if __name__ == "__main__":
    Example()

你说的“没有访问权限”是什么意思?你为什么没有权限?在这个特定的代码中,您不断覆盖
cb
变量,但是没有理由不能将复选按钮添加到列表或字典中。是的,这就是我的意思:永久覆盖绑定到复选按钮的变量。你能告诉我如何使用列表或字典来获取每个选中按钮的状态吗?在上面的示例中,是否执行类似cb[i]=tk.Checkbutton的操作。。。工作?这里没什么特别的事。解决这个问题的方法与在循环中创建字符串、数字或任何其他类型的对象的方法相同。把它们添加到列表中,添加到字典中,你可以随心所欲地做。嗯,不管我怎么做,都不管用。所有选中按钮,以及文本小部件中的大量超链接,都是在循环中创建的通用超链接。我不能使用任何类型的列表索引来访问通常用于访问checkbutton状态的变量。我检查是否选中checkbutton的标准方法是使用“My_variable=IntVar()”。当我尝试将其与类似“my_variable[I]=IntVar()”的列表索引一起使用时,会收到一条错误消息。我在创建通用超链接时也遇到类似问题。你说的“没有访问权限”是什么意思?你为什么没有权限?在这个特定的代码中,您不断覆盖
cb
变量,但是没有理由不能将复选按钮添加到列表或字典中。是的,这就是我的意思:永久覆盖绑定到复选按钮的变量。你能告诉我如何使用列表或字典来获取每个选中按钮的状态吗?在上面的示例中,是否执行类似cb[i]=tk.Checkbutton的操作。。。工作?这里没什么特别的事。解决这个问题的方法与在循环中创建字符串、数字或任何其他类型的对象的方法相同。把它们添加到列表中,添加到字典中,你可以随心所欲地做。嗯,不管我怎么做,都不管用。所有选中按钮,以及文本小部件中的大量超链接,都是在循环中创建的通用超链接。我不能使用任何类型的列表索引来访问通常用于访问checkbutton状态的变量。我检查是否选中checkbutton的标准方法是使用“My_variable=IntVar()”。当我尝试将其与类似“my_variable[I]=IntVar()”的列表索引一起使用时,会收到一条错误消息。我在创建通用超链接时也遇到过类似的问题。谢谢你,布莱恩,你真的帮了我很多忙!谢谢你,布莱恩,你真的帮了我很多!