简体   繁体   English

TTK Treeview:按内容缩放行

[英]TTK Treeview: scale row with content

Good evening.晚上好。

I am trying to work with the ttk Treeview widget and want to append entries with more than one line to it.我正在尝试使用 ttk Treeview 小部件并希望 append 条目超过一行。 Unfortunately, if I try to do this more than once, the second entry will just cover the first, thus making the content basically unreadable, as shown here .不幸的是,如果我多次尝试这样做,第二个条目将覆盖第一个条目,从而使内容基本上不可读,如此处所示 Because every entry has a different number of lines, I cannot set the height of the rows in the Treeview to a specific value;因为每个条目都有不同的行数,所以我无法将 Treeview 中的行高设置为特定值; instead, every single one has to be scaled independently, according to the number of lines.相反,每一个都必须根据行数独立缩放。

The problem can be illustrated here:问题可以在这里说明:

from tkinter import *
from tkinter import ttk

root = Tk()

decCols = ('colOne', 'colTwo')

decLog = ttk.Treeview(root, columns=decCols, show='headings')
decLog.heading('colOne', text='Column 1')
decLog.heading('colTwo', text='Column 2')
decLog.grid(row=0, column=0)

one = "Lorem"

mult = 'Ipsum \n Dolor \n'

data = (one, mult)

decLog.insert('', END, values=data)
decLog.insert('', END, values=data)

root.mainloop()

If anyone knew how to solve this problem, I would be grateful for a simple and effective answer.如果有人知道如何解决这个问题,我将不胜感激一个简单而有效的答案。

Thanks in advance, Jiscona在此先感谢,吉斯科纳

You can set the row height for every row in the Treeview using ttk.Style您可以使用ttk.StyleTreeview中的每一行设置行高。

style = ttk.Style()
style.configure('Treeview', rowheight=24) 
# try setting row height to ~2x your font size

The caveats here are:这里的警告是:

  • You'll have to size your rows to fit two lines of text (or whatever is the greatest number of lines you expect to accommodate)您必须调整行的大小以适合两行文本(或您希望容纳的最大行数)
  • Rows with fewer lines than that will be larger than necessary行数少于该行数的行将大于必要的行数

If you'd like to eliminate the guesswork in sizing to fit your font, you can use tkinter's Font module to query your chosen font's actual height如果您想消除大小以适合您的字体的猜测,您可以使用 tkinter 的Font模块来查询您选择的字体的实际高度

from tkinter.font import Font

font = Font(family='Arial', size=20)  # use whatever font you like
line_height = font.metrics()['linespace']  # returns an integer
# set Treeview row height based on font size
style = ttk.Style()
style.configure('Treeview', rowheight=line_height) 

Unfortunately there's no easy way that I'm aware of to set Treeview row heights dynamically, but I hope this helps.不幸的是,我知道没有简单的方法可以动态设置Treeview行高,但我希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM