Python(Tkinter)-从列表框创建复选框列表

时间:2018-10-09 18:34:51

标签: python tkinter listbox

我想为每个列表框项目创建一个复选框列表。因此,我创建了一个列表框,其中包含4个不同的项目:“一个”,“两个”,“三个”,“四个”。我想为每个列表框条目都有一个对应的复选框项的列表。当我单击一个列表框条目时,它应该在右侧具有一个复选框列表,而当我单击另一个列表框条目时,它应该具有一个与其他列表框项目无关的复选框列表。所有复选框列表都是相互独立的,这意味着在选择第一个列表框条目时可以选中4个复选框,但是当我选择第二个列表框条目时,它应该显示0项已选中(因为它们是独立的)。

import tkinter
from tkinter import *


master = tkinter.Tk()
master.geometry("750x500")

listbox = Listbox(master)
listbox.place(x=3,y=0)


for item in ["one", "two", "three", "four"]:
    listbox.insert(END, item)

enable = {'button 1','button 2', 'button 3'}

def onselect(evt):
    # Note here that Tkinter passes an event object to onselect()
    w = evt.widget
    x=0
    index = int(w.curselection()[0])
    value = w.get(index)
    print ('You selected item %d: "%s"' % (index, value))
    for item in enable:
        checkboxes = Checkbutton(master, text=item, variable=item)
        checkboxes.place(x=300,y=0+x)
        x+=50

listbox.bind('<<ListboxSelect>>', onselect)

print(enable)

mainloop()

2 个答案:

答案 0 :(得分:1)

看起来您的程序需要记住一些状态。

您可以将BooleanVar()传递给Checkbutton小部件,该小部件将使该BooleanVar保持最新。

#!/usr/bin/env python3

import tkinter
from tkinter import *

master = tkinter.Tk()
master.geometry("750x500")

listbox = Listbox(master)
listbox.place(x=3, y=0)

gui_state = {
    "one": {
        'button 1': BooleanVar(),
        'button 2': BooleanVar(),
        'button 3': BooleanVar(),
    },
    "two": {
        'button 1': BooleanVar(),
        'button 2': BooleanVar(),
        'button 3': BooleanVar(),
    },
    "three": {
        'button 1': BooleanVar(),
        'button 2': BooleanVar(),
        'button 3': BooleanVar(),
    },
    "four": {
        'button 1': BooleanVar(),
        'button 2': BooleanVar(),
        'button 3': BooleanVar(),
    },
}

for item in gui_state.keys():
    listbox.insert(END, item)


def bind_checkboxen(master, category):
    # render checkboxes for category. Mutate category when checkboxes are toggled.
    global checkboxes

    # delete old checkboxes
    for checkbox in checkboxes:
        checkbox.destroy()
    checkboxes = []

    x = 0

    # create new ones based on category fields
    for key in category.keys():
        checkbox = Checkbutton(master, text=key, variable=category[key])
        checkbox.place(x=300, y=0 + x)
        checkboxes.append(checkbox)
        x += 50


checkboxes = []


def onselect(evt):
    # Note here that Tkinter passes an event object to onselect()
    w = evt.widget
    index = int(w.curselection()[0])
    value = w.get(index)
    print('You selected item %d: "%s"' % (index, value))
    bind_checkboxen(master, gui_state[value])


listbox.bind('<<ListboxSelect>>', onselect)

mainloop()

答案 1 :(得分:1)

有趣的问题,我已经研究了30分钟。当然有几种方法,这里可能是最短和最动态的:

#!/usr/bin/env python3

import tkinter
from tkinter import *

master = tkinter.Tk()
master.geometry("750x500")

listbox = Listbox(master)
listbox.place(x=3,y=0)

enable = ['button 1', 'button 2', 'button 3']
list_for_listbox = ["one", "two", "three", "four"]

for item in list_for_listbox:
    listbox.insert(END, item)
    for y in enable:
        globals()["var{}{}".format(item, y)] = BooleanVar()
        globals()["checkbox{}{}".format(item, y)] = Checkbutton(master, text=y, variable=globals()["var{}{}".format(item, y)])

def onselect(evt):
    # Note here that Tkinter passes an event object to onselect()
    w = evt.widget
    x=0
    index = int(w.curselection()[0])
    value = w.get(index)
    print ('You selected item %d: "%s"' % (index, value))

    for y in enable:
        for item in list_for_listbox:
            globals()["checkbox{}{}".format(item, y)].place_forget()
        globals()["checkbox{}{}".format(value, y)].place(x=300,y=0+x)
        x+=50

listbox.bind('<<ListboxSelect>>', onselect)

print(enable)

mainloop()

您通过globals()["var{}{}".format(item, y)]

访问var。

示例:

for item in list_for_listbox:
    for y in enable:
        print(item + " [" + y + "] " + str(globals()["var{}{}".format(item, y)].get()))