from tkinter import ttk
from tkinter import *
root = Tk()
mystyle = ttk.Style()
# mystyle.theme_use('vista') # alt, clam, default, .. 也尝试了多种不同的主题(依然对行的样式无效)
# mystyle.configure( '.', relief = 'flat', borderwidth = 3 ) # Windows中 borderwidth参数无效(据Stack Overflow的回答)
mystyle.configure( 'Custom.Treeview', backgroud='silver', foreground='black', bd=0, font=('Calibri', 11))
mystyle.configure("Custom.Treeview.Heading", font=('Calibri', 13,'bold')) # 表头的字体设置
mystyle.map('Custom.Treeview', background=[('selected', 'green')], foreground=[('selected', 'yellow')]) # 设置选中时的背景和前景颜色
# 更复杂的样式设置,需要自己查文档,自己验证,对于"边框"我觉得已经没必要搞了,且Windows系统可能真的不支持。。
tree = ttk.Treeview(root, columns=('col1','col2','col3'),
style="Custom.Treeview") # 应用自定义的样式
tree.column('col1', width=100, anchor='center')
tree.column('col2', width=100, anchor='center')
tree.column('col3', width=100, anchor='center')
tree.heading('col1', text='col1')
tree.heading('col2', text='col2')
tree.heading('col3', text='col3')
def onDBClick(event):
item = tree.selection()[0]
print( "you clicked on ", tree.item(item, "values"))
for i in range(10):
tree.insert('',i,values=('a'+str(i),'b'+str(i),'c'+str(i)))
tree.bind("<Double-1>", onDBClick)
tree.pack()
root.mainloop()
|