选定列表框项目中的可滚动复选框

时间:2018-10-11 12:53:20

标签: python tkinter

如何将滚动条添加到每个列表框项目至少20个复选框的列表中?我希望复选框位于某种可以滚动的文本小部件内。这是PREVIOUS POST的继续提问。

1 个答案:

答案 0 :(得分:1)

更新是因为注释中的原因,请参见:感谢 Bryan Oakley &#x1F642 ; :

[带有字典的版本]

 导入tkinter
从tkinter导入*


def myfunction(事件):
    canvas1.configure(scrollregion = canvas1.bbox(“ all”))


def onselect(evt):
    #注意此处Tkinter将事件对象传递给onselect()
    w = evt.widget
    x = 0
    索引= int(w.curselection()[0])
    值= w.get(索引)
    打印(“您选择了%d个项目:“%s””%(索引,值))

    为y启用:
        对于list_for_listbox中的项目:
            checkbuttons [item] [y] [1] .grid_forget()
        checkbuttons [value] [y] [1] .grid(row = x,column = 0)
        #Label(frame2,text =“ some text”)。grid(row = x,column = 1)
        x + = 1


def printcommand():
    对于list_for_listbox中的项目:
        为y启用:
            打印(项目+“ [” + y +“]” + str(checkbuttons [项目] [y] [0] .get()))


主= tkinter.Tk()
master.title(“复选框测试”)
master.geometry(“ 750x500”)

#enable = ['按钮1','按钮2','按钮3','按钮4','按钮5','按钮6','按钮7']
启用= []
对于范围(1,15)中的x_number_of_items:
    enable.append(“ button” + str(x_number_of_items))

list_for_listbox = [“一个”,“两个”,“三个”,“四个”]

列表框=列表框(主)
listbox.place(x = 5,y = 5,宽度= 100,高度= 10 + 16 * len(list_for_listbox))
listbox.update()

frame1 =帧(主,borderwidth = 1,浮雕=沟槽,highlightthickness = 1,highlightbackground =“ black”,
               Highlightcolor =“ black”)
frame1.place(x = listbox.winfo_width()+ 10,y = 5,width = 300,height = listbox.winfo_height())
canvas1 =画布(frame1)
框架2 =框架(画布1,高度= 500)
scrollbar1 =滚动条(frame1,orient =“ vertical”,command = canvas1.yview)
canvas1.configure(yscrollcomman = scrollbar1.set)
scrollbar1.pack(side =“ right”,fill =“ y”)
canvas1.pack(side =“ left”)
canvas1.create_window((0,0),window = frame2,anchor ='nw')
frame2.bind(“ ”,myfunction)

printbutton =按钮(母版,文本=“打印”,command = printcommand)
printbutton.place(x = 100,y = 250)

复选按钮= {}
对于list_for_listbox中的项目:
    listbox.insert(结束,项目)
    checkbuttons [item] =(dict())
    为y启用:
        temp_var = BooleanVar()
        checkbuttons [item] [y] = [temp_var,Checkbutton(frame2,text = y,variable = temp_var)]

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

打印(启用)

mainloop()

printcommand()
 

[具有动态变量的版本]

 导入tkinter
从tkinter导入*


def myfunction(事件):
    canvas1.configure(scrollregion = canvas1.bbox(“ all”))


def onselect(evt):
    #注意此处Tkinter将事件对象传递给onselect()
    w = evt.widget
    x = 0
    索引= int(w.curselection()[0])
    值= w.get(索引)
    打印(“您选择了%d个项目:“%s””%(索引,值))

    为y启用:
        对于list_for_listbox中的项目:
            globals()[“ checkbox {} {}”。format(item,y)]。grid_forget()
        globals()[“ checkbox {} {}”。format(value,y)]。grid(row = x,column = 0)
        #在这里您可以添加文本
        x + = 1


主= tkinter.Tk()
master.title(“复选框测试”)
master.geometry(“ 750x500”)

启用= ['按钮1','按钮2','按钮3','按钮4','按钮5','按钮6','按钮7']
list_for_listbox = [“一个”,“两个”,“三个”,“四个”]

列表框=列表框(主)
listbox.place(x = 5,y = 5,宽度= 100,高度= 10 + 16 * len(list_for_listbox))
listbox.update()

框架1 =框架(母版,边框宽度= 1,浮雕= GROOVE)
frame1.place(x = listbox.winfo_width()+ 10,y = 5,width = 300,height = listbox.winfo_height())
canvas1 =画布(frame1)
框架2 =框架(画布1,高度= 500)
scrollbar1 =滚动条(frame1,orient =“ vertical”,command = canvas1.yview)
canvas1.configure(yscrollcomman = scrollbar1.set)
scrollbar1.pack(side =“ right”,fill =“ y”)
canvas1.pack(side =“ left”)
canvas1.create_window((0,0),window = frame2,anchor ='nw')
frame2.bind(“ ”,myfunction)

对于list_for_listbox中的项目:
    listbox.insert(结束,项目)
    为y启用:
        globals()[“ var {} {}”。format(item,y)] = BooleanVar()
        打印(“复选框” +项目+ y)
        globals()[“ checkbox {} {}”。format(item,y)] = Checkbutton(frame2,text = y,
                                                                variable = globals()[“ var {} {}”。format(item,y)])

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

打印(启用)

mainloop()

对于list_for_listbox中的项目:
    为y启用:
        print(item +“ [” + y +“]” + str(globals()[“ var {} {}”。format(item,y)]。get()))