tkinter treeview更改列字体大小 [英] tkinter treeview change column font size

查看:2015
本文介绍了tkinter treeview更改列字体大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我有以下三个代码:

tree["columns"] = ("one", "two", "three")
tree.column("one", width=150)
tree.column("two", width=150)
tree.column("three", width=150)
tree.heading("one", text="Naar")
tree.heading("two", text="Spoor")
tree.heading("three", text="Vetrektijd")
tree['show'] = 'headings'

但是我要做的是将字体大小更改为三列中的20 但是我该怎么办呢? 由于在互联网上,我读到一些有关Style()的信息,但这在我的代码中不起作用

解决方案

有两种解决方案值得关注.

首先是您所指出的Style()的使用,如果我们要更改Treeview.Heading文本的样式,它可以让我们设置它的样式.

如下所示:

from tkinter import *
import tkinter.ttk as ttk

root = Tk()

tree = ttk.Treeview(root)
tree.pack()

style = ttk.Style()
style.configure("Treeview.Heading", font=(None, 100))

tree["columns"] = ("one", "two", "three")
tree.column("one", width=150)
tree.column("two", width=150)
tree.column("three", width=150)
tree.heading("one", text="Naar")
tree.heading("two", text="Spoor")
tree.heading("three", text="Vetrektijd")
tree['show'] = 'headings'

这可以通过样式化窗口小部件内的特定元素来实现.让我们来分解一下.

style = ttk.Style()告诉tkinter我们正在创建一种样式,并将其存储在变量style中.

style.configure()允许我们配置刚刚创建的样式.

"Treeview.Heading"是列标题元素的名称.

font=(None, 100)是一种骗人的"方式,可以在不更改字体本身的情况下增加字体大小.如果要更改字体样式,请用所需的字体替换None.也许Comic Sans MS.


另一种选择是使用tkinter内置的名为nametofont的功能,让我们更深入地了解字体.

我们可以执行以下操作:

from tkinter import *
from tkinter.font import nametofont
import tkinter.ttk as ttk

root = Tk()

tree = ttk.Treeview(root)
tree.pack()

#nametofont("TkHeadingFont").configure(size=100)

tree["columns"] = ("one", "two", "three")
tree.column("one", width=150)
tree.column("two", width=150)
tree.column("three", width=150)
tree.heading("one", text="Naar")
tree.heading("two", text="Spoor")
tree.heading("three", text="Vetrektijd")
tree['show'] = 'headings'

哪个似乎可以达到相同的结果,对吧?

我们在这里所做的不同实际上是将tkinter属性的字体修改为TkHeadingFont,并告诉它将其大小更改为100.这意味着,如果您要在其他地方使用该字体,它也会以相同的样式显示.

这涉及到必须在程序顶部声明from tkinter.font import nametofont.

从美学上讲,这两种方法都能达到相同的结果.

Right now i have this threecode:

tree["columns"] = ("one", "two", "three")
tree.column("one", width=150)
tree.column("two", width=150)
tree.column("three", width=150)
tree.heading("one", text="Naar")
tree.heading("two", text="Spoor")
tree.heading("three", text="Vetrektijd")
tree['show'] = 'headings'

but what i want to do is change the font size to 20 of the three columns But how can i do that? Since on the internet i read something about Style() but that doesnt work in my code

解决方案

There are two solutions which jump to mind.

The first is the use of Style() as you pointed out, which will let us set the style of the Treeview.Heading text if we want to change that.

This looks something like the below:

from tkinter import *
import tkinter.ttk as ttk

root = Tk()

tree = ttk.Treeview(root)
tree.pack()

style = ttk.Style()
style.configure("Treeview.Heading", font=(None, 100))

tree["columns"] = ("one", "two", "three")
tree.column("one", width=150)
tree.column("two", width=150)
tree.column("three", width=150)
tree.heading("one", text="Naar")
tree.heading("two", text="Spoor")
tree.heading("three", text="Vetrektijd")
tree['show'] = 'headings'

This works by styling specific elements inside the widget. Let's break this down.

style = ttk.Style() tells tkinter that we are creating a style and we're storing it inside of the variable style.

style.configure() allows us to configure the style we just created.`

"Treeview.Heading" is the name of the element for the column headings.

font=(None, 100) is a "cheaty" way of increasing font size without having to change the font itself. If you wanted to change the font style substitute None with whichever font you wanted. Perhaps Comic Sans MS.


The other option is using a function tkinter has built in called nametofont which let's us mess with the fonts at a deeper level.

We can do something like the below:

from tkinter import *
from tkinter.font import nametofont
import tkinter.ttk as ttk

root = Tk()

tree = ttk.Treeview(root)
tree.pack()

#nametofont("TkHeadingFont").configure(size=100)

tree["columns"] = ("one", "two", "three")
tree.column("one", width=150)
tree.column("two", width=150)
tree.column("three", width=150)
tree.heading("one", text="Naar")
tree.heading("two", text="Spoor")
tree.heading("three", text="Vetrektijd")
tree['show'] = 'headings'

Which seems to achieve the same result, right?

What we're doing differently here is actually modifying the font that tkinter attributes to TkHeadingFont and telling it to change it's size to 100. Meaning that if you were to use that font somewhere else it would also appear in the same style.

This involves having to declare from tkinter.font import nametofont at the top of your program however.

Either method achieves the same result aesthetically speaking.

这篇关于tkinter treeview更改列字体大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆