鱼C论坛

 找回密码
 立即注册
查看: 3621|回复: 4

[已解决]Treeview内部如何创建表格线??

[复制链接]
发表于 2021-8-14 23:14:11 | 显示全部楼层 |阅读模式
30鱼币
下面的代码里的表格,里面是没有表格线,请问如何创建表格线,并设置行颜色等,谢谢!!
  1. from tkinter import ttk
  2. from tkinter import *
  3. root = Tk()
  4. tree = ttk.Treeview(root, columns=('col1','col2','col3'))

  5. tree.column('col1', width=100, anchor='center')
  6. tree.column('col2', width=100, anchor='center')
  7. tree.column('col3', width=100, anchor='center')
  8. tree.heading('col1', text='col1')
  9. tree.heading('col2', text='col2')
  10. tree.heading('col3', text='col3')

  11. def onDBClick(event):
  12.     item = tree.selection()[0]
  13.     print( "you clicked on ", tree.item(item, "values"))
  14.    
  15. for i in range(10):
  16.     tree.insert('',i,values=('a'+str(i),'b'+str(i),'c'+str(i)))
  17. tree.bind("<Double-1>", onDBClick)


  18. tree.pack()
  19. root.mainloop()
复制代码

20210814231022.png
最佳答案
2021-8-14 23:14:12
Windows似乎设置不了边框,或非常麻烦。
列名(表头)字体颜色等可以改,行的颜色也可以改。

参考:
https://stackoverflow.com/questions/19749476/3-different-issues-with-ttk-treeviews-in-python
......

最佳答案

查看完整内容

Windows似乎设置不了边框,或非常麻烦。 列名(表头)字体颜色等可以改,行的颜色也可以改。 参考: https://stackoverflow.com/questions/19749476/3-different-issues-with-ttk-treeviews-in-python ......
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-14 23:14:12 | 显示全部楼层    本楼为最佳答案   
Windows似乎设置不了边框,或非常麻烦。
列名(表头)字体颜色等可以改,行的颜色也可以改。

参考:
https://stackoverflow.com/questions/19749476/3-different-issues-with-ttk-treeviews-in-python
......
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-15 09:19:28 | 显示全部楼层
求解答,谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-8-15 22:46:58 | 显示全部楼层

  1. from tkinter import ttk
  2. from tkinter import *
  3. root = Tk()
  4. mystyle = ttk.Style()
  5. # mystyle.theme_use('vista') # alt, clam, default, .. 也尝试了多种不同的主题(依然对行的样式无效)
  6. # mystyle.configure(  '.', relief = 'flat',  borderwidth = 3 )  # Windows中 borderwidth参数无效(据Stack Overflow的回答)
  7. mystyle.configure(  'Custom.Treeview', backgroud='silver', foreground='black', bd=0, font=('Calibri', 11))
  8. mystyle.configure("Custom.Treeview.Heading", font=('Calibri', 13,'bold'))  # 表头的字体设置
  9. mystyle.map('Custom.Treeview', background=[('selected', 'green')], foreground=[('selected', 'yellow')])  # 设置选中时的背景和前景颜色
  10. # 更复杂的样式设置,需要自己查文档,自己验证,对于"边框"我觉得已经没必要搞了,且Windows系统可能真的不支持。。

  11. tree = ttk.Treeview(root, columns=('col1','col2','col3'),
  12.                 style="Custom.Treeview") # 应用自定义的样式

  13. tree.column('col1', width=100, anchor='center')
  14. tree.column('col2', width=100, anchor='center')
  15. tree.column('col3', width=100, anchor='center')
  16. tree.heading('col1', text='col1')
  17. tree.heading('col2', text='col2')
  18. tree.heading('col3', text='col3')

  19. def onDBClick(event):
  20.     item = tree.selection()[0]
  21.     print( "you clicked on ", tree.item(item, "values"))
  22.    
  23. for i in range(10):
  24.     tree.insert('',i,values=('a'+str(i),'b'+str(i),'c'+str(i)))
  25. tree.bind("<Double-1>", onDBClick)

  26. tree.pack()
  27. root.mainloop()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-8-18 18:40:47 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 06:07

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表