tkinter-listbox,treectrl

treectrl

https://sourceforge.net/projects/tktreectrl/files/tktreectrl/tktreectrl-2.4.1/
有两个项目
把整个目录拷贝过去就可以了

TkTreectrl (考到 Lib)
treectrl2.4.1 (考到 tcl)

http://tkintertreectrl.sourceforge.net/

https://sourceforge.net/projects/tkintertreectrl/files/TkinterTreectrl-2.0/
https://sourceforge.net/projects/tktreectrl/files/tktreectrl/tktreectrl-2.4.1/

通常是虚拟环境的 env/xxx/Lib\site-packages 目录 还有个 env/xxx/tcl 目录,要使用修复后的版本

Put it in tcl folder (e.g your conda directory where you get this error –C:\Users<username>\AppData\Local\Continuum\miniconda3\tcl)

You will get rid of two types of of error:

Can’t find package error
Invalid argument Error

https://www.geeksforgeeks.org/creating-a-multiple-selection-using-tkinter/

"""
The selectmode option of a listbox widget can be single, browse, multiple or extended.
single – Selects one line of text.
browse – It is a default option where the user can select one line of text.
multiple – Selects multiple lines of text without dragging from first line of option to last line.
extended – User can select and drag adjacent multiple lines of text.
"""

self.mlb = treectrl.MultiListbox()
self.mlb.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
self.mlb.focus_set()
self.mlb.configure(selectcmd=self.tree_selected, selectmode='extended')
self.mlb.config(columns=('名称', '测量类型'))
self.mlb.insert('end', *map(unicode, ('圆弧1', '圆弧')))
self.mlb.insert('end', *map(unicode, ('内边缘左边距', '直径')))

def tree_selected(self, selected):
print('tree_selected items:', selected)
self.treeSolutionItem = selected
if not len(selected):
print('tree_selected zero')
return

scrollbar

leftFrame = tk.Frame()

mlbSolutionFrame = tk.Frame(leftFrame)
scrollbarSolution = tk.Scrollbar(mlbSolutionFrame, orient=tk.HORIZONTAL)
self.mlbSolution = treectrl.MultiListbox(mlbSolutionFrame,xscrollcommand=scrollbarSolution.set)
scrollbarSolution.config(command=self.mlbSolution.xview)
scrollbarSolution.pack(side=tk.BOTTOM, fill=tk.X)
self.mlbSolution.pack(side=tk.TOP, fill=tk.Y, expand=1)
self.mlbSolution.focus_set()
self.mlbSolution.configure(selectcmd=self.tree_solution_selected, selectmode='single')
self.mlbSolution.config(columns=('方案名称', '创建时间'))
mlbSolutionFrame.pack(side=tk.TOP, fill=tk.BOTH, expand=1)

mlbMeasureFrame = tk.Frame(leftFrame)
scrollbarMeasure = tk.Scrollbar(mlbMeasureFrame, orient=tk.HORIZONTAL)
self.mlbMeasure = treectrl.MultiListbox(mlbMeasureFrame,xscrollcommand=scrollbarMeasure.set)
scrollbarMeasure.config(command=self.mlbMeasure.xview)
scrollbarMeasure.pack(side=tk.BOTTOM, fill=tk.X)
self.mlbMeasure.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
self.mlbMeasure.focus_set()
# 单选
self.mlbMeasure.configure(selectcmd=self.tree_measure_selected, selectmode='single')
# 多选
self.mlbMeasure.configure(selectcmd=self.tree_measureRect_selected, selectmode='multiple')
self.mlbMeasure.config(columns=('测量名称', '测量类型'))
mlbMeasureFrame.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
leftFrame.pack(side=tk.LEFT, fill=tk.BOTH)

添加数据

self.mlbSolution.insert('end', *map(unicode, (row[1], row[2])))

删除选中项

item = self.mlbSolution.curselection()
self.mlbSolution.delete(item[0])

选中

这里使用项目个数 - 2 来定位最后一个
之前使用 treeId 会因为有删除动作导致 找不到存在项目

self.mlbSolution.select_set(self.mlbSolution.item_count() - 2)

清空

self.mlbMeasureResult.delete(0, 'end')