首页 > 解决方案 > 如何使用 Tkinter 在树视图表输出中包装行值?

问题描述

没有包装和编辑的树视图输出我需要在树视图小部件中包装一个行值。我在下面添加了一个示例代码,可以重现我的问题。第一列“属性”中的值应包装成多行,而不是在一个句子中获取行值。因此,可读性受到影响。另外,是否可以使这些行可编辑以添加我们自己的文本?第一列(属性)的第二行值要求对其进行编辑。是否可以在树视图中使行可编辑?请注意,目前我将树视图行值作为存储在变量中的列表列表,以便插入到树视图中。但是出于以下代码中的演示目的,这些值是手动给出的。

第 1 列的第一行值需要包装成多行。需要使第 1 列的第二行值可编辑。] 1

from tkinter import *
import tkinter as tk
from tkinter import ttk


root= Tk()
style = ttk.Style()
style.configure("mystyle.Treeview", highlightthickness=0, bd=0, font=('Calibri', 11)) # Modify the font of the body
style.configure("mystyle.Treeview.Heading", font=('Calibri', 13,'bold')) # Modify the font of the headings
style.layout("mystyle.Treeview", [('mystyle.Treeview.treearea', {'sticky': 'nswe'})]) # Remove the borders
tree = ttk.Treeview(root,style="mystyle.Treeview")  # this is the treeview widget to display the dataframe
column_list_account = ["ATTRIBUTE", "VALUE"]  # These are the headings
tree['columns'] = column_list_account  # We assign the column list to the widgets columns
tree["show"] = "headings"  # this hides the default column
for column in column_list_account:  # for each column
    tree.heading(column, text=column)  # let the column heading =column name
    tree.column(column, width=100, stretch =YES)  # set the column size to 50px
    tree.place(relheight=1,
                relwidth=1)  # set the height and width of the widget to 100% of its container (self.frame1)
treescroll = tk.Scrollbar(root)  # create a scrollbar
treescroll.configure(command=tree.yview)  # make it vertical
tree.configure(yscrollcommand=treescroll.set)  # assign the scrollbar to the Treeview Widget
treescroll.pack(side="right", fill="y")  # make the scrollbar fill the yaxis of the Treeview widget

tree.insert("", "end", values=("Observation_Number ():when the system identidies the keyword is Observation_Number, it will convert the current observation number to character and assign to the target variable.", "Normal"))  # inserts each list into the treeview
tree.insert("", "end", values =("Edit me", "Is it possible to edit me as well?"))

root.mainloop()

标签: pythonuser-interfacetkintertreeview

解决方案


推荐阅读