【发布时间】:2016-03-16 05:51:30
【问题描述】:
当单击树视图中的项目时,是否可以在包含弹出菜单的子窗口中放置一个 tkinter 树视图。目前菜单显示在右键单击并被定向到适当的功能,但是我无法识别在树视图中选择的项目。
有没有办法在使用菜单后识别树视图中已选择的行?
提前致谢
class Page(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
button = ttk.Button(self, text="Treeview", command= self.ChildWindow)
button.pack()
def ChildWindow(self):
#Create menu
popup = Menu(self, tearoff=0)
popup.add_command(label="Next", command=self.selection)
popup.add_separator()
def do_popup(event):
# display the popup menu
try:
popup.tk_popup(event.x_root, event.y_root, 0)
finally:
# make sure to release the grab (Tk 8.0a1 only)
popup.grab_release()
#Create Treeview
win2 = Toplevel()
new_element_header=['1st']
treeScroll = ttk.Scrollbar(win2)
treeScroll.pack(side=RIGHT, fill=Y)
self.tree = ttk.Treeview(win2,columns=new_element_header, show="headings")
self.tree.heading("1st", text="1st")
self.tree.insert("" , 0, text="Line 1", values=("1A"))
self.tree.pack(side=LEFT, fill=BOTH)
self.tree.bind("<Button-3>", do_popup)
win2.minsize(600,30)
def selection(self, event):
selection = self.tree.set(self.tree.identify_row(event.y))
print selection
【问题讨论】:
-
我以前试过这个。我记不太清了,但我确定我设计了一个菜单并使用了
menu.post()功能。树视图中还有一个函数,例如tree.identify(x, y),可用于查找用户单击的位置是否为单元格、标题等。