有没有办法改变 tkinter Treeview 标题的高度? [英] Is there a way to change height of tkinter Treeview heading?

查看:103
本文介绍了有没有办法改变 tkinter Treeview 标题的高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在更改 Treeview.heading 的高度时遇到问题.我已经找到了一些关于 Treeview.column 尺寸的答案,但是当我在文档中访问 Treeview.heading 时,没有一个词是关于改变高度的当文本不适合(并包装它)时动态标题,甚至只是硬编码标题的高度(以像素为单位).我不必将文本分成两行,但是当我将它保持那么长时,整个表格(因为它有很多条目)占据了整个屏幕.我想让它更小,因此我需要拆分更长的条目.

I got a problem with changing the height of the Treeview.heading. I have found some answers about the dimensions of Treeview.column, but when I access Treeview.heading in the documentation, there is not a single word about changing the height of the heading dynamically when the text doesn't fit (and wrapping it) or even just hard-coding height of the heading in pixels. I don't have to split the text to two rows, but when I just keep it that long the whole table (as it has many entries) takes up the whole screen. I want to keep it smaller, therefore I need to split longer entries.

这是它的样子:

推荐答案

我找不到任何文档来验证这一点,但看起来标题的高度是由第一列中的标题决定的.

I can't find any documentation to verify this but it looks like the height of the heading is determined by the heading in the first column.

重现问题

col_list = ('Name', 'Three\nLine\nHeader', 'Two\nline')
tree = Treeview(parent, columns=col_list[1:])
ix = -1
for col in col_list:
    ix += 1
    tree.heading(f'#{ix}', text=col)

修复

col_list = ('Name\n\n', 'Three\nLine\nHeader', 'Two\nline')

或者,如果你想让它看起来更漂亮

or, if you want to make it look prettier

col_list = ('\nName\n', 'Three\nLine\nHeader', 'Two\nline')

唯一的问题是我还没有弄清楚如何将标题放在两行标题上

The only problem is I haven't figured out how to centre the heading on a two line header

编辑

如果它是顶级窗口,则换行符有效,但如果它是对话框则无效.另一种方法是设置样式.我不知道为什么会这样.

The newlines work if it is the top level window but not if it is a dialog. Another way of doing this is to set the style. I've got no idea why this works.

style = ttk.Style()
style.configure('Treeview.Heading', foreground='black')

这篇关于有没有办法改变 tkinter Treeview 标题的高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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