【发布时间】:2018-12-25 15:54:05
【问题描述】:
我正在使用下面的类来绘制带有可选元素的可滚动表格。当用户单击其中一个项目时,我需要将整行突出显示(有效)。但目前您一次只能选择一行。如何修改下面的代码以允许一次选择(和突出显示)多行?
from Tkinter import *
import Tkinter as tk
class MultiListbox_fuse(Frame):
def __init__(self,master,lists):
Frame.__init__(self,master,borderwidth=1,relief=SUNKEN)
self.lists = []
self.columns=[]
for l,w in lists:
frame = Frame(self); frame.pack(side=LEFT, expand=YES, fill=BOTH)
Label(frame, text=l, borderwidth=1, relief=RAISED).pack(fill=X)
lb = Listbox(frame, width=w, height=30, borderwidth=0, selectborderwidth=0,
relief=FLAT, exportselection=FALSE, selectmode=MULTIPLE)
lb.pack(expand=YES, fill=BOTH)
self.lists.append(lb)
lb.bind('<B1-Motion>', lambda e, s=self: s._select(e.y))
lb.bind('<Button-1>', lambda e, s=self: s._select(e.y))
lb.bind('<Leave>', lambda e: 'break')
lb.bind('<B2-Motion>', lambda e, s=self: s._b2motion(e.x, e.y))
lb.bind('<Button-2>', lambda e, s=self: s._button2(e.x, e.y))
lb.bind('<Button-4>', lambda e, s=self: s._scroll(SCROLL, 1, PAGES))
lb.bind('<Button-5>', lambda e, s=self: s._scroll(SCROLL, -1, PAGES))
lb.bind("<MouseWheel>", self.OnMouseWheel)
#self.add(frame)
Label(master, borderwidth=1, relief=FLAT).pack(fill=X)
sb = Scrollbar(master, orient=VERTICAL, command=self._scroll,borderwidth=1)
sb.pack(fill=Y,side=RIGHT,expand=NO)
for l in self.lists:
l['yscrollcommand']=sb.set
#self.add(frame)
self.pack(expand=YES,fill=BOTH)
self.sortedBy=-1
self.previousWheel=0
def _select(self, y,state=16):
row = self.lists[0].nearest(y)
if state==16:self.selection_clear(0, END)
self.selection_set(row)
## print self.curselection()
return 'break'
def _button2(self, x, y):
for l in self.lists: l.scan_mark(x, y)
return 'break'
def _b2motion(self, x, y):
for l in self.lists: l.scan_dragto(x, y)
return 'break'
def _scroll(self, *args):
for l in self.lists:
apply(l.yview, args)
return 'break'
def clickon(self,e):
self._sortBy(self.columns.index(e.widget['text']))
def _sortBy(self, column):
""" Sort by a given column. """
if column == self.sortedBy:
direction = -1 * self.direction
else:
direction = 1
elements = self.get(0, END)
self.delete(0, END)
elements.sort(lambda x, y: self._sortAssist(column, direction, x, y))
self.insert(END, *elements)
self.sortedBy = column
self.direction = direction
def _sortAssist(self, column, direction, x, y):
c = cmp(x[column], y[column])
if c:
return direction * c
else:
return direction * cmp(x, y)
def curselection(self):
return self.lists[0].curselection()
def delete(self, first, last=None):
for l in self.lists:
l.delete(first, last)
def get(self, first, last=None):
result = []
for l in self.lists:
result.append(l.get(first,last))
if last: return apply(map, [None] + result)
return result
def index(self, index):
self.lists[0].index(index)
def insert(self, index, *elements):
for e in elements:
i = 0
for l in self.lists:
l.insert(index, e[i])
i = i + 1
def size(self):
return self.lists[0].size()
def see(self, index):
for l in self.lists:
l.see(index)
def selection_anchor(self, index):
for l in self.lists:
l.selection_anchor(index)
def selection_clear(self, first, last=None):
for l in self.lists:
l.selection_clear(first, last)
def selection_includes(self, index):
return self.lists[0].selection_includes(index)
def selection_set(self, first, last=None):
for l in self.lists:
l.selection_set(first, last)
def OnMouseWheel(self, event):
for l in self.lists:
l.yview("scroll", event.delta,"units")
# this prevents default bindings from firing, which
# would end up scrolling the widget twice
return "break"
root = Tk()
root.minsize(width=650, height=580)
root.maxsize(width=650, height=580)
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
x = (w/2) - 300
y = (h/2) - 250
root.geometry('%dx%d+%d+%d' % (650, 550, x, y-80))
root.wm_title("Results displayed")
tabelka = MultiListbox_fuse(root, (('Costam1',8), ('Costam2',8)))
tabelka.pack()
for a in range(1,100):
tabelka.insert(END, (str("abc1"), str("def2")))
root.mainloop()
【问题讨论】:
-
此代码不运行。请提供Minimal, Complete, and Verifiable example。我试图猜测我应该如何使用这门课,但没有成功。请同时提供使用示例。
-
@figbeam - 好的 - 我现在已经编辑了我的帖子以包含使用此类的完整代码
-
试图减少到最小的例子,发现你有一个函数和一个名为
index的变量。他们似乎没有干涉,但这是一件坏事...... -
感谢您的关注!我一定会重命名其中一个!
-
添加了答案,但更多的是评论。我包含了我收到的错误消息,并认为答案有更好的格式化机会......
标签: python user-interface tkinter listbox tkinter-layout