问题描述
限时送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:关键词]
发布评论