Tkinter treeview size for string items

36 views Asked by At

I have a GUI with a treeview widget and a widget with detailed information about the selected item. Now if some of the items in the treeview were too large, they could not be seen because the treeview is too small.

Here is an image of what happens enter image description here

And here is the code:

# This is a sample Python script.

# Press Umschalt+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import tkinter as tk


import tkinter as tk
from tkinter import ttk
from tkinter import Label, Entry, Button, Checkbutton
from tkinter.messagebox import showinfo
from tkinter import *
from tkinter.ttk import Progressbar
import time
import threading


class MainWindow:
    """
    Lib description

    == Table of contents ==

    %TOC%
    """

    ROBOT_LIBRARY_SCOPE = "GLOBAL"
    # ------------------------------------------
    def __init__(self, root: tk.Tk = None):
        self.root = root


        self.create_widgets()

    def create_widgets(self):
        """
        Build and visualize all the widgets of launcher gui tool

        Out\n
        | return_status_bln | False: Error, True: No Error  |\n
        """
        
        self.label_frame_tree = tk.LabelFrame(self.root, text="Projects", font=("Helvetica", 13))   
        self.label_frame_tree.pack(fill=tk.X, side="left", padx=10, pady=5  )   
        
        self.tree = ttk.Treeview(self.label_frame_tree, height=6 )
        self.tree.pack(fill=tk.X, side="left", padx=10, pady=5)

        self.tree.heading('#0', text='Departments', anchor=tk.W)

        # adding data
        id_prj = self.tree.insert('', tk.END, text='Project', iid=0, open=False)
        self.tree.insert('', tk.END, text='Pool', iid=1, open=False)

        # adding children of first node
        value_dct = {"a":1, "b":2}
        id_opwrt = self.tree.insert(id_prj, tk.END, values=value_dct, text='main 1', iid=5, open=False)
        self.tree.insert(id_opwrt, tk.END, text='sub1 ssssssssssssssssssssssssssssssssssssssssssssssss', open=False)
        self.tree.insert(id_opwrt, tk.END, text='sub2', open=False)
        
        values_lst = ("1","2")
        self.tree.insert(id_prj, tk.END, values=values_lst, text='main 2', iid=6, open=False)

        self.label_frame_info = tk.LabelFrame(self.root, text="Info Detail", font=("Helvetica", 13))

        self.label1 = tk.Label(self.label_frame_info, text="Project")
        self.label1.grid(row=0, column=0, padx=10, pady=5)
        self.text_info_1 = tk.Entry(self.label_frame_info,width=40)
        self.text_info_1.grid(row=0, column=1, padx=10, pady=5)
        

        # sub project
        self.label2 = tk.Label(self.label_frame_info, text="Subproject")
        self.label2.grid(row=1, column=0, padx=10, pady=5)
        self.text_info_2 = tk.Entry(self.label_frame_info,width=40)
        self.text_info_2.grid(row=1, column=1, padx=10, pady=5)

        self.label_frame_info.pack(fill=tk.X, side="left", padx=10, pady=5)   


        return_status_bln = True




def main():
    # Create a window
    root = tk.Tk()

    # Set the window title
    root.title("GUI")

    # Set the window size
    root.geometry("900x600")

    # Create the main window
    MainWindow(root)

    # Start the window's event-loop
    root.mainloop()

if __name__ == "__main__":
    print("123")
    main()

So my question is, how can I change the size of the Treeview to show also large items? Is it better to use a grid layout and set the column size?

How can I add a horizontal scrollbar to the treeview?

0

There are 0 answers