如何使 ttk 树视图 row/item 充当按钮?
How to make a ttk treeview row/item act as a button?
请不要使用Class、___init____等,因为这些东西都超出了我的知识和理解范围。本质上,如果有人可以告诉我如何在相同的 margin/indentation 代码级别执行此操作,那将不胜感激。
在 windows
上使用 python3.4.3
谢谢
oystein-hr给了你解决方案,你可以正常使用它。
基于 Byan Oakley 示例代码的示例:
和 Bryan Oakley 建议使用 <<TreeviewSelect>>
:
import tkinter as tk
from tkinter import ttk
def OnDoubleClick(event):
item = tree.selection()
print('item:', item)
print('event:', event)
item = tree.selection()[0]
print("you clicked on", tree.item(item,"text"))
root = tk.Tk()
tree = ttk.Treeview()
tree.pack()
for i in range(10):
tree.insert("", "end", text="Item %s" % i)
#tree.bind("<Double-1>", OnDoubleClick) # double click
#tree.bind("<Button-1>", OnDoubleClick) # single click
tree.bind("<<TreeviewSelect>>", OnDoubleClick) # single click, without "index out of range" error
root.mainloop()
没有行 "act as a button" 因为您可以单击它来 运行 功能。
请不要使用Class、___init____等,因为这些东西都超出了我的知识和理解范围。本质上,如果有人可以告诉我如何在相同的 margin/indentation 代码级别执行此操作,那将不胜感激。
在 windows
上使用 python3.4.3谢谢
oystein-hr给了你解决方案,你可以正常使用它。
基于 Byan Oakley 示例代码的示例:
和 Bryan Oakley 建议使用 <<TreeviewSelect>>
:
import tkinter as tk
from tkinter import ttk
def OnDoubleClick(event):
item = tree.selection()
print('item:', item)
print('event:', event)
item = tree.selection()[0]
print("you clicked on", tree.item(item,"text"))
root = tk.Tk()
tree = ttk.Treeview()
tree.pack()
for i in range(10):
tree.insert("", "end", text="Item %s" % i)
#tree.bind("<Double-1>", OnDoubleClick) # double click
#tree.bind("<Button-1>", OnDoubleClick) # single click
tree.bind("<<TreeviewSelect>>", OnDoubleClick) # single click, without "index out of range" error
root.mainloop()
没有行 "act as a button" 因为您可以单击它来 运行 功能。