向 Treeview 表的标题(文本粗体和背景色)添加样式

编程入门 行业动态 更新时间:2024-10-27 19:24:56
本文介绍了向 Treeview 表的标题(文本粗体和背景色)添加样式 - Tkinter Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个小 Tkinter GUI,里面有一张桌子.我想让标题中的文本加粗并更改其背景颜色.我知道我们可以通过 ttk.Style() 和 configure 来做到这一点,但表格中没有任何变化.它看起来仍然很简单,还是我做错了.

I have this little Tkinter GUI where I have a table. I want to make the text bold in the headings and also change its background color. I know that we can do this by ttk.Style() and configure but nothing is changing in the table. It's still looking plain or am I doing this wrong.

请帮忙.

from tkinter import ttk
import tkinter as tk
from tkinter import *

window = tk.Tk()
window.state('zoomed')
treev = ttk.Treeview(window, selectmode ='browse')
treev.place(x= 600, y= 200, width= 350, height=350)

treev["columns"] = ('1', '2')
treev['show'] = 'headings'

style = ttk.Style()
style.configure('mytreeview.Headings', background='gray', font=('Arial Bold', 10))


ID = [1,2,3,4,5]
Names = ['Tom', 'Rob', 'Tim', 'Jim', 'Kim']

treev.column("1", width = 100, anchor ='c')
treev.column("2", width = 100, anchor ='c')

treev.heading("1", text ="ID")
treev.heading("2", text ="Names")


for x, y in zip(ID, Names):
    treev.insert("", 'end', values =(x, y))

window.mainloop()

推荐答案

要配置样式,您需要一个这样调用的布局:

To configure an style you need a layout that is called so:

style.layout('my.Treeview',
             [('Treeview.field', {'sticky': 'nswe', 'border': '1', 'children': [
                 ('Treeview.padding', {'sticky': 'nswe', 'children': [
                     ('Treeview.treearea', {'sticky': 'nswe'})
                     ]})
                 ]})
              ])    

这段代码创建了一个名为 my.Treeview 的新布局,并复制了 Treeview 的数据.然后,在使用该名称创建布局后,您可以使用以下命令对其进行配置:

This code makes a new layout called my.Treeview and copies the data of Treeview. Then, after you have a layout created with that name you can configure it with:

style.configure('my.Treeview.Heading', background='gray', font=('Arial Bold', 10))

并且不要忘记在您喜欢的小部件上使用该样式:

and dont forget to use that style on the widget you like with:

treev = ttk.Treeview(window, selectmode ='browse',style='my.Treeview')

这篇关于向 Treeview 表的标题(文本粗体和背景色)添加样式 - Tkinter Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:23:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390858.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:样式   粗体   背景色   文本   标题

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!