我想使用鼠标指针选择由tkinter.treeview()创建的表格中的单元格,并打印出单元格的值。我该怎么做?
以下是创建tkinter.treeview表格的示例代码。在这种情况下,我想使用鼠标指针单击其中一个值,比如“机场”。但是,当我单击该单元格时,整行(即小部件项)会被选中。我该如何避免这种情况,以便获得我想要的结果?
以下是创建tkinter.treeview表格的示例代码。在这种情况下,我想使用鼠标指针单击其中一个值,比如“机场”。但是,当我单击该单元格时,整行(即小部件项)会被选中。我该如何避免这种情况,以便获得我想要的结果?
import tkinter as tk
import tkinter.ttk as ttk
class App(tk.Frame):
def __init__(self, parent, *args, **kwargs):
ttk.Frame.__init__(self, parent, *args, **kwargs)
self.tree = ttk.Treeview(parent, columns=("size", "modified"))
self.tree["columns"] = ("date", "time", "loc")
self.tree.column("date", width=80)
self.tree.column("time", width=80)
self.tree.column("loc", width=100)
self.tree.heading("date", text="Date")
self.tree.heading("time", text="Time")
self.tree.heading("loc", text="Loc")
self.tree.bind('<ButtonRelease-1>', self.selectItem)
self.tree.insert("","end",text = "Name",values = ("Date","Time","Loc"))
self.tree.insert("","end",text = "John",values = ("2017-02-05","11:30:23","Airport"))
self.tree.insert("","end",text = "Betty",values = ("2014-06-25","18:00:00","Orchard Road"))
self.tree.grid()
def selectItem(self, event):
curItem = self.tree.focus()
print (self.tree.item(curItem))
if __name__ == "__main__":
window = tk.Tk()
app = App(window)
window.mainloop()
.identify_element(x, y)
是方法吗? - Sun Bearpointerxy
返回相对于屏幕的坐标。 - Bryan Oakley