本文介绍了来自选定列表框项目的可滚动复选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将滚动条添加到每个列表框项至少有 20 个复选框的列表中?我希望复选框位于某种可以滚动的文本小部件中.这是来自 PREVIOUS POST一>.
How do you add a scroll bar to a list of atleast 20 checkboxes per listbox item? I want the checkboxes to be within some sort of text widget that can be scrolled. This is a continued question from PREVIOUS POST.
推荐答案
UPDATE 由于评论中的推理,请参阅:感谢 布莱恩奥克利 🙂:
UPDATE because of reasoning in the comments see: Thanks to Bryan Oakley 🙂 :
[带字典的版本]
import tkinter
from tkinter import *
def myfunction(event):
canvas1.configure(scrollregion=canvas1.bbox("all"))
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:
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():
for item in list_for_listbox:
for y in enable:
print(item + " [" + y + "] " + str(checkbuttons[item][y][0].get()))
master = tkinter.Tk()
master.title("Checkboxes test")
master.geometry("750x500")
# enable = ['button 1', 'button 2', 'button 3', 'button 4', 'button 5', 'button 6', 'button 7']
enable = []
for x_number_of_items in range(1, 15):
enable.append("button " + str(x_number_of_items))
list_for_listbox = ["one", "two", "three", "four"]
listbox = Listbox(master)
listbox.place(x=5, y=5, width=100, height=10 + 16*len(list_for_listbox))
listbox.update()
frame1 = Frame(master, borderwidth=1, relief=GROOVE, highlightthickness=1, highlightbackground="black",
highlightcolor="black")
frame1.place(x=listbox.winfo_width() + 10, y=5, width=300, height=listbox.winfo_height())
canvas1 = Canvas(frame1)
frame2 = Frame(canvas1, height=500)
scrollbar1 = Scrollbar(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("<Configure>", myfunction)
printbutton = Button(master, text="Print", command=printcommand)
printbutton.place(x=100, y=250)
checkbuttons = {}
for item in list_for_listbox:
listbox.insert(END, item)
checkbuttons[item] = (dict())
for y in enable:
temp_var = BooleanVar()
checkbuttons[item][y] = [temp_var, Checkbutton(frame2, text=y, variable=temp_var)]
listbox.bind('<<ListboxSelect>>', onselect)
print(enable)
mainloop()
printcommand()
[带有动态变量的版本]
import tkinter
from tkinter import *
def myfunction(event):
canvas1.configure(scrollregion=canvas1.bbox("all"))
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)].grid_forget()
globals()["checkbox{}{}".format(value, y)].grid(row=x,column=0)
# here you can add Text
x += 1
master = tkinter.Tk()
master.title("Checkboxes test")
master.geometry("750x500")
enable = ['button 1', 'button 2', 'button 3', 'button 4', 'button 5', 'button 6', 'button 7']
list_for_listbox = ["one", "two", "three", "four"]
listbox = Listbox(master)
listbox.place(x=5, y=5, width=100, height=10 + 16*len(list_for_listbox))
listbox.update()
frame1 = Frame(master, borderwidth=1, relief=GROOVE)
frame1.place(x=listbox.winfo_width() + 10, y=5, width=300, height=listbox.winfo_height())
canvas1 = Canvas(frame1)
frame2 = Frame(canvas1, height=500)
scrollbar1 = Scrollbar(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("<Configure>", myfunction)
for item in list_for_listbox:
listbox.insert(END, item)
for y in enable:
globals()["var{}{}".format(item, y)] = BooleanVar()
print("checkbox" + item + y)
globals()["checkbox{}{}".format(item, y)] = Checkbutton(frame2, text=y,
variable=globals()["var{}{}".format(item, y)])
listbox.bind('<<ListboxSelect>>', onselect)
print(enable)
mainloop()
for item in list_for_listbox:
for y in enable:
print(item + " [" + y + "] " + str(globals()["var{}{}".format(item, y)].get()))
这篇关于来自选定列表框项目的可滚动复选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!