0

this is what i got

this is what i got

And only when I maximize the window I can see the scroll bar

after increases window

I want to stick the scrollbar (horizontal and vertical) in treeview. regardless of the window size.

I'm trying to add scrollbar to label with treeview regardless of the window size. this is my code:

def mainGUI():
    root = tk.Tk()
    root.geometry("700x300")
    root.title("test")
    root.columnconfigure(0, weight=1)

    data = [["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"]]


    results_lf = ttk.LabelFrame(root, text="Results:")
    results_lf.grid(row=1, column=0, padx=20, pady=0, sticky='ew')

    resultsLabel = Label(results_lf)
    resultsLabel.pack(fill='x',expand=True, side=LEFT)

    columnsHeader = ["1", "2", "3", "4", "5", "6"]

    tree = ttk.Treeview(resultsLabel, columns=columnsHeader, show='headings')
    tree.heading('1', text='1')
    tree.heading('2', text='2')
    tree.heading('3', text='3')
    tree.heading('4', text='4')
    tree.heading('5', text='5')
    tree.heading('6', text='6')


    for line in data:
        tree.insert('', tk.END, values=line)
    tree.pack(side=LEFT)

    sb_v = Scrollbar(resultsLabel, orient=VERTICAL)
    sb_v.pack(side=RIGHT, fill=Y)

    sb_h = Scrollbar(resultsLabel, orient=HORIZONTAL)
    sb_h.pack(side=BOTTOM, fill=X)

    tree.config(yscrollcommand=sb_v.set)

    sb_v.config(command=tree.yview)
    sb_h.config(command=tree.xview)

    root.mainloop()


mainGUI()
gvd
  • 19
  • 4
  • Try to use `grid()` instead of `pack()`, it is easier. – acw1668 Feb 05 '23 at 14:52
  • @acw1668. It doesn't work. Unless he is adding `tk.` to every widget and keyword. He is using `root= tk.TK()` – toyota Supra Feb 05 '23 at 15:22
  • Change this `Scrollbar(resultsLabel` to `Scrollbar(results_lf` – toyota Supra Feb 05 '23 at 15:25
  • 1
    @toyotaSupra I have seen many beginners import tkinter using both `import tkinter as tk` and `from tkinter import *`. Also it is not the main cause of OP issue as OP has posted images which means the application can be executed. – acw1668 Feb 05 '23 at 15:27
  • @acw1668. I had too many problem. So I gave up. – toyota Supra Feb 05 '23 at 15:30
  • Change this `resultsLabel.pack` to `resultsLabel.grid` You will be able to see Label on window at the bottom. You must set this `row =1` , `row=1` and `row=2. Instead of using pack() – toyota Supra Feb 05 '23 at 15:30
  • It is not recommended to use a `Label` widget as the parent of the treeview and the scrollbars. Use `Frame` widget instead. – acw1668 Feb 06 '23 at 01:25

2 Answers2

1

When using pack, the order in which you add widgets matters. pack will allocate an entire side of the available space for each widget that is added.

When you pack the tree on the left, the packer allocates the entire left side for that widget. That means that things added to the top, right, or bottom will all be to the right of what was packed to the left. So, when you later pack the horizontal scrollbar it will be to the right of everything that is on the left.

The simple solution is to pack the two scrollbars before packing the tree. Personally I find it best to group together all calls to pack or place for children with a common parent. It makes visualizing the layout easier, and makes it easier to make changes like this.

So, if you move all of the calls to pack for these three widgets to the end of the function, it should look like this:

sb_v.pack(side=RIGHT, fill=Y)
sb_h.pack(side=BOTTOM, fill=X)
tree.pack(side=LEFT)

For an answer with images that describe how the packer works, see this answer to the question Tkinter pack method confusion

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Label is not visible. It is hidden behind treeview. – toyota Supra Feb 05 '23 at 20:58
  • there is any way to add checkbox to each row? – gvd Feb 08 '23 at 16:30
  • @gvd: the treeview doesn't support inserting widgets. However, you can insert images so you can add an image of an unchecked checkbox. When the user clicks on it you can toggle between checked and unchecked images. – Bryan Oakley Feb 08 '23 at 17:24
  • @BryanOakley do you have any source for that..? or maybe share a simple code of it? – gvd Feb 09 '23 at 07:42
0
  • In line 29, change results_lf.grid(row=1, to results_lf.grid(row=0,
  • In line 31, change ttk.LabelFrame(root, to ttk.Label(results_lf,
  • in line 32, change to pack to grid and row to resultsLabel.grid(row=0, column=0, padx=5,pady=0, sticky='w')
  • in line 36, ttk.Treeview(resultsLabel, to ttk.Treeview(results_lf,
  • in line 47, change to pack to grid and row to tree.grid(row=1,column=0,padx=5,pady=10, sticky='nw')
  • In line 49, change Scrollbar(resultsLabel to ttk.Scrollbar(results_lf, #Vertical
  • in line 50 change to pack to grid and row to sb_v.grid(row=1,column=1, sticky='ns')
  • in line change Scrollbar(resultsLabel, to ttk.Scrollbar(results_lf, #HORIZPNTAL
  • in line 53 change to pack to grid and row to sb_h.grid(row=2,column=0, sticky='we')

Code:

import tkinter as tk
from tkinter import ttk
 
def mainGUI():
    root = tk.Tk()
    root.geometry("700x300")
    root.title("test")
    root.rowconfigure(0, weight=2)

    data = [["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"],
        ["this is a long text","this is a long text","this is a long text","this is a long text","this is a long text","this is a long text"]]


    results_lf = ttk.LabelFrame(root, text="Results:")
    results_lf.grid(row=0, column=0, sticky='nw')

    resultsLabel = ttk.Label(results_lf, text='hello')
    resultsLabel.grid(row=0, column=0, padx=5,pady=0, sticky='w')

    columnsHeader = ["1", "2", "3", "4", "5", "6"]

    tree = ttk.Treeview(results_lf, columns=columnsHeader, show='headings')
    tree.heading('1', text='1')
    tree.heading('2', text='2')
    tree.heading('3', text='3')
    tree.heading('4', text='4')
    tree.heading('5', text='5')
    tree.heading('6', text='6')


    for line in data:
        tree.insert('', tk.END, values=line)
    tree.grid(row=1,column=0,padx=5,pady=10, sticky='nw')

    sb_v = ttk.Scrollbar(results_lf, orient=tk.VERTICAL)
    sb_v.grid(row=1,column=1, sticky='ns')

    sb_h = ttk.Scrollbar(results_lf, orient=tk.HORIZONTAL)
    sb_h.grid(row=2,column=0, sticky='we')
    

    tree.config(yscrollcommand=sb_v.set)

    sb_v.config(command=tree.yview)
    sb_h.config(command=tree.xview)

    root.mainloop()


mainGUI()

Screenshot: you Label is visible on top left:

enter image description here

toyota Supra
  • 3,181
  • 4
  • 15
  • 19