Multilines text depending on the TreeView width

65 Views Asked by At

I am building a plugin for thonny and I have a problem about building a TreeView. I have a tree view that contains only one column and it has several rows. Each row has its text, and the text can be too long. I should tell the tree view to cut the text so that it appears in the next line.

Here's my code:

class TestTreeView(ttk.Frame):    
    def __init__(self, master):
        ttk.Frame.__init__(self, master)
        self._init_widgets()

    def _init_widgets(self):
        # init and place scrollbar
        self.vert_scrollbar = SafeScrollbar(self, orient=tk.VERTICAL)
        self.vert_scrollbar.grid(row=0, column=1, sticky=tk.NSEW)

        self.horz_scrollbar = SafeScrollbar(self, orient=tk.HORIZONTAL)
        self.horz_scrollbar.grid(row=1, column=0, sticky=tk.NSEW)
        
        self.tree = ttk.Treeview(self)
        
        # scrollbars
        self.vert_scrollbar["command"] = self.tree.yview
        self.horz_scrollbar["command"] = self.tree.xview
        
        self.tree.grid(row=0, column=0, sticky=tk.NSEW)
        
        # configure the scroll bars on the tree view
        self.tree.configure(yscrollcommand=self.vert_scrollbar.set, xscrollcommand=self.horz_scrollbar.set) 
        
        # set single-cell frame
        self.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)

        # init tree events
        self.tree.bind("<<TreeviewSelect>>", self._on_select, True)

        # configure the only tree column
        self.tree.column("#0", anchor=tk.W, stretch=True, width=1000, minwidth=1000)
        self.tree["show"] = ("tree",)

        self.tree.tag_configure('orange', foreground='orange')
        self.tree.tag_configure('red', foreground='red')
        self.tree.tag_configure('green', foreground='darkgreen')

        ttk.Style().configure('Treeview', rowheight=40, rowwidth=40)

As you can see in the following picture, I want the last line (Tests Run: 2, Failures...) to be on several lines if the width of the view is small than the width of the rows.

enter image description here

In general, I want something responsive depending on the size of the view.

Is this possible?

0

There are 0 best solutions below