如何在python的tkinter中设置Treeview的宽度 [英] How to set width of Treeview in tkinter of python
问题描述
最近,我使用 tkinter
TreeView
在 Python
中显示许多列.具体来说,树视图中有 49 列数据.我使用 grid
来管理我的小部件.
Recently, I use tkinter
TreeView
to show many columns in Python
. Specifically, 49 columns data in a treeview. I use grid
to manage my widgets.
我发现树视图的宽度只取决于列的宽度.
I found out the width of the treeview only depends on the width of columns.
我的问题是,如何设置 Treeview 的宽度.(默认宽度是所有列的宽度之和)
My question is, How can I set the width of the Treeview. (The default width is the summation of all columns' width)
当我的所有列宽都设置为 20 时.这是 49 列.:)
When all my column width is set as 20. This is 49 columns. :)
这是我的主要代码:
frame = Frame(self.master, width = 1845, height = 670)
frame.place(x = 20, y = 310)
tree_view = Treeview(frame, height = 33, selectmode = "extended")
for i in range(len(id_list)):
tree_view.column(id_list[i], width = width_list[i], anchor = CENTER)
tree_view.heading(id_list[i] , text = text_list[i])
tree_view["show"] = "headings"
# Omit the declaration of scrollbar
tree_view['yscroll'] = self.y_scollbar.set
tree_view['xscroll'] = self.x_scollbar.set
tree_view.grid(row = 0, column = 0, sticky = NSEW)
for item in detail_info_list:
tree_view.insert("",0, text = str(item.id), value=(...))
id_list
,width_list
,text_list
用于存储列信息.detail_info_list
用于存储 Treeview 中显示的数据.
id_list
,width_list
,text_list
is used to store columns information.
detail_info_list
is to store the data showed in the Treeview.
我的目标是当我定义某个列的大宽度(例如:3000)时,我的树视图可以按预期显示数据.但是树状视图在我的屏幕上,水平滚动条也无法滑动.
My target is when I define a large width(for example: 3000) of some column, my treeview could show the data as I expected. But the treeview is across my screen and the horizontal scrollbar also can't slide.
当第 17 列设置为 1500 时:
When 17th column is set as 1500:
我看不到我的按钮,也无法滑动水平滚动条.
I can't see my buttons, and I can't slide the horizontal scrollbar.
另外,我尝试了一些解决方案.
Also, I have tried some solution.
- 定义一个框架作为 Treeview 的父级.并定义宽度和高度来约束 Treeview.但它没有用.
- 我查阅了一些文档并搜索了这个问题.我找不到任何将宽度设置为常量的解决方案.
推荐答案
经过一个多星期的搜索,我偶然发现了一个非常简单的方法来实现魔法".
After searching for over a week now, I stumbled upon a very easy way to do the 'magic'.
- 我读取了主窗口的当前大小
- 将数据添加到treeView
- 根据条目的最大长度设置每列的宽度(使用
measure
) - 在我的主窗口上调用
update
函数(不确定是否需要) - 将主窗口的大小设置为存储的值
- I read the current size of my mainWindow
- Add the data to the treeView
- Set the width of each column depending on the max length of an entry (by using
measure
) - Calling the
update
function on my mainWindow (not sure if this is needed) - Setting the size of the mainWindow to the stored values
这是我的代码:
def reloadTreeViewData(self):
# get current size of main window
curMainWindowHeight = self.parent.winfo_height()
curMainWindowWidth = self.parent.winfo_width()
#delete all
for child in self.tree.get_children():
self.dicData = {}
self.tree.delete(child)
# reload all
count = 0
for row in self.data:
adding_string = []
for element in self.indexlist:
adding_string.append(str(row[element]))
insert_ID = self.tree.insert('', 'end', text=count, values=adding_string)
# save the data in it's original types in a dictionary
self.dicData[insert_ID] = [row[x] for x in self.indexlist]
count += 1
# reset column width
max_colum_widths = [0] * len(self.indexlist)
for child in self.tree.get_children():
rowData = self.dicData[child]
for idx, rowIdx in enumerate(self.indexlist):
item = rowData[idx]
new_length = self.myFont.measure(str(item))
if new_length > max_colum_widths[idx]:
max_colum_widths[idx] = new_length
for idx, item in enumerate(self.attributeList):
if int(max_colum_widths[idx]) < 50:
self.tree.column(item, width=50)
else:
self.tree.column(item, width=int(max_colum_widths[idx]))
# restore the size of the mainWindow
self.parent.update()
self.parent.geometry("" + str(curMainWindowWidth) + "x" + str(curMainWindowHeight))
我的 treeView 标题存储在 self.attributeList
中,但数据来自数据库,我想显示更多列.所以我使用存储为 self.indexlist
的映射列表.
My headings for the treeView are stored in self.attributeList
but the data comes from a database, which has way more columns I want to show. So I use a mapping list which is stored as self.indexlist
.
self.myFont
定义如下:
import tkinter.font as tkFont
self.myFont = tkFont.Font(self, "Calibri", 12)
我使用 treeview 来显示数据和一个单独的字典来保存数据,使用 child-id(由 insert
返回)作为键.通过这种方式,我保留了插入到 treeView 中的真实数据.这对于像0005"这样的字符串很重要,当您从树视图中调用它并丢失前导零时,它将返回 5(整数).
I use the treeview just to show the data and a seperate dictionary to save the data, by using the child-id (returned by insert
) as key. This way I keep the real data I inserted into the treeView. This is important for strings like '0005' which will return as 5 (integer) when you recall it from the treeview and you lose the leading zeros.
我希望这个解决方案可以帮助其他人.如果有更方便的解决方案,请随时发表评论.
I hope this solution helps others. If there is a more convenient solution feel free to comment.
如果您对我的代码有任何疑问,请随时提问.
If you have any questions about my code, feel free to ask.
这篇关于如何在python的tkinter中设置Treeview的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!