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

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('<>', onselect)

print(enable)

mainloop()

解决方案

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('<>', onselect)

print(enable)

mainloop()

You access the vars by globals()["var{}{}".format(item, y)]

example:

for item in list_for_listbox:

for y in enable:

print(item + " [" + y + "] " + str(globals()["var{}{}".format(item, y)].get()))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值