Python(Tkinter)-从列表框创建复选框列表 [英] Python (Tkinter)- Create Checkbox list from listbox

查看:1294
本文介绍了Python(Tkinter)-从列表框创建复选框列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a checkbox list for every listbox item. So, I have a listbox created with 4 different items, "one", "two", "three", "four". I want to have a list of corresponding checkbox items for each of the listbox entries. When I click on a listbox entry, it should have a list of checkboxes on the right and when I click on a different listbox entry it should have a list of checkboxes that would be independent of the other listbox items. All checkbox lists are independent of each other, meaning you can check 4 checkboxes when the first listbox entry is selected, but when I select the second listbox entry it should show 0 things checked (because they are independent).

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()

推荐答案

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

Interesting question and I have been working on it for 30 min. There are of course several ways, here the probably shortest and most dynamic:

#!/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)]

示例:

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

这篇关于Python(Tkinter)-从列表框创建复选框列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆