def onDoubleClick(self, event):
''' Executed, when a row is double-clicked. Opens
read-only EntryPopup above the item's column, so it is possible
to select text '''
# close previous popups
# self.destroyPopups()
# what row and column was clicked on
rowid = self._tree.identify_row(event.y)
column = self._tree.identify_column(event.x)
# get column position info
x,y,width,height = self._tree.bbox(rowid, column)
# y-axis offset
# pady = height // 2
pady = 0
# place Entry popup properly
text = self._tree.item(rowid, 'text')
self.entryPopup = EntryPopup(self._tree, rowid, text)
self.entryPopup.place( x=0, y=y+pady, anchor=W, relwidth=1)
def OnDoubleClick(treeView):
# First check if a blank space was selected
entryIndex = treeView.focus()
if '' == entryIndex: return
# Set up window
win = Toplevel()
win.title("Edit Entry")
win.attributes("-toolwindow", True)
####
# Set up the window's other attributes and geometry
####
# Grab the entry's values
for child in treeView.get_children():
if child == entryIndex:
values = treeView.item(child)["values"]
break
col1Lbl = Label(win, text = "Value 1: ")
col1Ent = Entry(win)
col1Ent.insert(0, values[0]) # Default is column 1's current value
col1Lbl.grid(row = 0, column = 0)
col1Ent.grid(row = 0, column = 1)
col2Lbl = Label(win, text = "Value 2: ")
col2Ent = Entry(win)
col2Ent.insert(0, values[1]) # Default is column 2's current value
col2Lbl.grid(row = 0, column = 2)
col2Ent.grid(row = 0, column = 3)
col3Lbl = Label(win, text = "Value 3: ")
col3Ent = Entry(win)
col3Ent.insert(0, values[2]) # Default is column 3's current value
col3Lbl.grid(row = 0, column = 4)
col3Ent.grid(row = 0, column = 5)
def UpdateThenDestroy():
if ConfirmEntry(treeView, col1Ent.get(), col2Ent.get(), col3Ent.get()):
win.destroy()
okButt = Button(win, text = "Ok")
okButt.bind("<Button-1>", lambda e: UpdateThenDestroy())
okButt.grid(row = 1, column = 4)
canButt = Button(win, text = "Cancel")
canButt.bind("<Button-1>", lambda c: win.destroy())
canButt.grid(row = 1, column = 5)
然后确认更改:
def ConfirmEntry(treeView, entry1, entry2, entry3):
####
# Whatever validation you need
####
# Grab the current index in the tree
currInd = treeView.index(treeView.focus())
# Remove it from the tree
DeleteCurrentEntry(treeView)
# Put it back in with the upated values
treeView.insert('', currInd, values = (entry1, entry2, entry3))
return True
class Tableview(ttk.Treeview):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
tv.bind("<Double-1>", lambda event: self.onDoubleClick(event))
def onDoubleClick(self, event):
''' Executed, when a row is double-clicked. Opens
read-only EntryPopup above the item's column, so it is possible
to select text '''
# close previous popups
try: # in case there was no previous popup
self.entryPopup.destroy()
except AttributeError:
pass
# what row and column was clicked on
rowid = self.identify_row(event.y)
column = self.identify_column(event.x)
# handle exception when header is double click
if not rowid:
return
# get column position info
x,y,width,height = self.bbox(rowid, column)
# y-axis offset
pady = height // 2
# place Entry popup properly
text = self.item(rowid, 'values')[int(column[1:])-1]
self.entryPopup = EntryPopup(self, rowid, int(column[1:])-1, text)
self.entryPopup.place(x=x, y=y+pady, width=width, height=height, anchor='w')
# in Treeview setup routine
self.tview.tree.bind("<<TreeviewSelect>>", self.TableItemClick)
# in TableItemClick()
selitems = self.tview.tree.selection()
if selitems:
selitem = selitems[0]
text = self.tview.tree.item(selitem, "text") # get value in col #0
8条答案
按热度按时间h22fl7wq1#
经过长时间的研究,我还没有发现这样的功能,所以我想有任何。TK是非常简单的接口,它允许程序员从基础构建“高级”功能。所以我想要的行为这样。
这是将ttk.Treeview组合为self._tree的类中的方法
EntryPopup是Entry的一个非常简单的子类:
h4cxqtbf2#
您也可以弹出一个工具窗口,其中列出了可编辑的字段和条目,以更新值。本示例有一个三列的树视图,并且没有使用子类。
将您的双击绑定到以下内容:
然后确认更改:
以下是如何删除条目:
ztmd8pv53#
我已经尝试了@dakov解决方案,但它不适合我,因为我的treeView有多个列,还有一些原因。
EntryPopup类
flmtquvp4#
经过这么多的研究,而做我的项目得到了这个代码,它帮助了我很多。双击你要编辑的元素,作出所需的更改,然后单击“确定”按钮,我认为这正是你想要的
python #tkinter #treeview #editablerow
New rowEditable row
06odsfpq5#
这只是为构造函数中设置的指定路径创建一个树。你可以将你的事件绑定到那个树上的项目。事件函数以一种可以以多种方式使用项目的方式保留。在这种情况下,双击它时,它会显示项目的名称。希望这对某些人有帮助。
kmpatx3s6#
你不应该这样做手动有准备使用包有这个功能和许多更如tkintertable它有一些疯狂的功能
还有pygubu-editable-treeview,如果你对pygubu感兴趣,
至于你不应该自己编写代码的原因,为了做一个好的treeview,你需要构建更多的功能,使你的gui更容易使用,但是这样的功能需要数百行代码来创建。(需要很长时间才能正确)除非你正在制作一个自定义的treeview小部件,否则不值得付出努力。
hfwmuf9z7#
我已经调整了@DCOPTimDowd代码,使编辑单元格在视觉上更吸引人和更容易。
改进:
改进总是受欢迎的:github file
截图:
prdp8dxp8#
我不知道如何使行可编辑,但要捕获单击行,您可以使用
<<TreeviewSelect>>
虚拟事件。这将通过bind()
方法绑定到例程,然后使用selection()
方法获取所选项目的id。这些是来自现有程序的片段,但显示了调用的基本序列: