如何将 Treeview 列 header 变成按钮?
How to make a Treeview column header into a Button?
我试图通过向标题添加按钮来改进我的 GUI 的功能,但找不到关于此按钮的任何示例或信息。在这个例子中有没有办法让标题变成一个可点击的按钮? Treeview
将从数据库中填充,因此,如果我能让按钮正常工作,这意味着我可以以比在“框”外设置更多按钮更好的方式来排序显示。
import tkinter as tk
from tkinter import ttk
screen = tk.Tk()
screen.title('This One')
screen.geometry('890x400')
style = ttk.Style()
style.theme_use("clam")
screen.grid_rowconfigure(1, weight=1)
screen.grid_columnconfigure(0, weight=1)
cols = ('TOKEN', 'F-500', 'F-250', 'F-100', 'F-24', 'POS','NEG')
box = ttk.Treeview(screen, columns=cols, show='headings')
for col in cols:
box.heading(col, text=col)
box.grid(row=1, column=0, columnspan=2,sticky='nsew')
box.column("TOKEN", width=95)
box.column("F-500", width=85, anchor='e')
box.column("F-250", width=85, anchor='e')
box.column("F-100", width=85, anchor='e')
box.column("F-24", width=85, anchor='e')
box.column("POS", width=75, anchor='center')
box.column("NEG", width=75, anchor='center')
closeButton = tk.Button(screen, text="Close", width=15, command=exit).grid(row=10, column=0)
screen.mainloop()
Treeview 的标题已经是带有可选关键字 command
的 按钮 。要在 forloop 中使用它,您可以使用 lambda
和 annoynmous function.
import tkinter as tk
from tkinter import ttk
screen = tk.Tk()
screen.title('This One')
screen.geometry('890x400')
style = ttk.Style()
style.theme_use("clam")
screen.grid_rowconfigure(1, weight=1)
screen.grid_columnconfigure(0, weight=1)
cols = ('TOKEN', 'F-500', 'F-250', 'F-100', 'F-24', 'POS','NEG')
box = ttk.Treeview(screen, columns=cols, show='headings')
for col in cols:
if col == 'TOKEN':
box.heading(col, text=col, command =lambda: print('token'))
box.grid(row=1, column=0, columnspan=2,sticky='nsew')
box.column("TOKEN", width=95)
box.column("F-500", width=85, anchor='e')
box.column("F-250", width=85, anchor='e')
box.column("F-100", width=85, anchor='e')
box.column("F-24", width=85, anchor='e')
box.column("POS", width=75, anchor='center')
box.column("NEG", width=75, anchor='center')
closeButton = tk.Button(screen, text="Close", width=15, command=exit).grid(row=10, column=0)
screen.mainloop()
不使用 lambda:
def token():
print('token')
cols = ('TOKEN', 'F-500', 'F-250', 'F-100', 'F-24', 'POS','NEG')
box = ttk.Treeview(screen, columns=cols, show='headings')
for col in cols:
if col == 'TOKEN':
box.heading(col, text=col, command =token)
box.grid(row=1, column=0, columnspan=2,sticky='nsew')
我试图通过向标题添加按钮来改进我的 GUI 的功能,但找不到关于此按钮的任何示例或信息。在这个例子中有没有办法让标题变成一个可点击的按钮? Treeview
将从数据库中填充,因此,如果我能让按钮正常工作,这意味着我可以以比在“框”外设置更多按钮更好的方式来排序显示。
import tkinter as tk
from tkinter import ttk
screen = tk.Tk()
screen.title('This One')
screen.geometry('890x400')
style = ttk.Style()
style.theme_use("clam")
screen.grid_rowconfigure(1, weight=1)
screen.grid_columnconfigure(0, weight=1)
cols = ('TOKEN', 'F-500', 'F-250', 'F-100', 'F-24', 'POS','NEG')
box = ttk.Treeview(screen, columns=cols, show='headings')
for col in cols:
box.heading(col, text=col)
box.grid(row=1, column=0, columnspan=2,sticky='nsew')
box.column("TOKEN", width=95)
box.column("F-500", width=85, anchor='e')
box.column("F-250", width=85, anchor='e')
box.column("F-100", width=85, anchor='e')
box.column("F-24", width=85, anchor='e')
box.column("POS", width=75, anchor='center')
box.column("NEG", width=75, anchor='center')
closeButton = tk.Button(screen, text="Close", width=15, command=exit).grid(row=10, column=0)
screen.mainloop()
Treeview 的标题已经是带有可选关键字 command
的 按钮 。要在 forloop 中使用它,您可以使用 lambda
和 annoynmous function.
import tkinter as tk
from tkinter import ttk
screen = tk.Tk()
screen.title('This One')
screen.geometry('890x400')
style = ttk.Style()
style.theme_use("clam")
screen.grid_rowconfigure(1, weight=1)
screen.grid_columnconfigure(0, weight=1)
cols = ('TOKEN', 'F-500', 'F-250', 'F-100', 'F-24', 'POS','NEG')
box = ttk.Treeview(screen, columns=cols, show='headings')
for col in cols:
if col == 'TOKEN':
box.heading(col, text=col, command =lambda: print('token'))
box.grid(row=1, column=0, columnspan=2,sticky='nsew')
box.column("TOKEN", width=95)
box.column("F-500", width=85, anchor='e')
box.column("F-250", width=85, anchor='e')
box.column("F-100", width=85, anchor='e')
box.column("F-24", width=85, anchor='e')
box.column("POS", width=75, anchor='center')
box.column("NEG", width=75, anchor='center')
closeButton = tk.Button(screen, text="Close", width=15, command=exit).grid(row=10, column=0)
screen.mainloop()
不使用 lambda:
def token():
print('token')
cols = ('TOKEN', 'F-500', 'F-250', 'F-100', 'F-24', 'POS','NEG')
box = ttk.Treeview(screen, columns=cols, show='headings')
for col in cols:
if col == 'TOKEN':
box.heading(col, text=col, command =token)
box.grid(row=1, column=0, columnspan=2,sticky='nsew')