1

I am trying to add a horizontal & vertical scrollbar in Tkinter. The vertical scrollbar works but the horizontal scrollbar does not work and I don't know why. Actually, the horizontal scrollbar appears on the window but not scrollable. I would be really happy if someone has a solution for this problem.

Thanks in advance!

import tkinter as tk
from tkinter import ttk

class Example(tk.Frame):
    def __init__(self, parent):

        tk.Frame.__init__(self, parent)
        self.canvas = tk.Canvas(self, borderwidth=0, background="#ffffff")
        self.frame = tk.Frame(self.canvas, background="#ffffff")
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.vsb.set)
        self.hsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
        self.canvas.configure(xscrollcommand=self.hsb.set)

        self.vsb.pack(side="right", fill="y")
        self.hsb.pack(side="bottom", fill="x")
        self.canvas.pack(side="left", fill="both", expand=True)
        self.canvas_window = self.canvas.create_window((4,4), window=self.frame, anchor="nw", tags="self.frame")

        self.frame.bind("<Configure>", self.onFrameConfigure) #bind an event whenever the size of the viewPort frame changes.
        self.canvas.bind("<Configure>", self.onFrameConfigure)#Enables to scroll
        #self.canvas.unbind_all("<MouseWheel>")

        self.items_variables1 = {"f1_Apple1": 1, "f1_Apple2": 1, "f1_Apple3": 1, "f1_Apple4": 0, "f1_Apple5": 0,"f1_Apple6": 0, "f1_Apple7": 1, "f1_Apple8": 0, "f1_Apple9": 0,"f1_Applex": 0, "f1_Appley": 0, "f1_Applez": 0}
        self.items_variables2 = {"f1_Banana1": 1, "f1_Banana2": 0, "f1_Banana3": 1, "f1_Banana4": 0, "f1_Banana5": 0, "f1_Banana6": 0,  "f1_Banana7": 1, "f1_Banana8": 0, "f1_Banana9": 0, "f1_Bananax": 0, "f1_Bananay": 0, "f1_Bananaz": 1}

        self.tabControl = ttk.Notebook(self.frame, width=100, height=1100) # Create a tabcontrol
        self.tab1 = ttk.Frame(self.tabControl) #Create a tab
        self.tab1.pack()#(fill=BOTH, expand=1)
        self.tabControl.add(self.tab1, text="TEST TAB1")
        self.tabControl.pack(expand=1, fill="both") #Pack to make visible

        self.label1 = tk.Label(self.tab1, text='ADD 1', fg='white', bg='black')
        self.getIt1 = ttk.LabelFrame(self.tab1, labelwidget=self.label1)
        self.getIt1.grid(column=0, row=0, padx=20, pady=10)

        self.label2 = tk.Label(self.tab1, text = 'ADD 2', fg='white', bg='black')
        self.getIt2 = ttk.LabelFrame(self.tab1, labelwidget=self.label2)
        self.getIt2.grid(column=0, row=1, padx=20, pady=10)

        varsapple = self.check_button(2, 1, self.getIt1, self.items_variables1)
        print(varsapple["f1_Apple1"].get())
        varsbanana = self.check_button(2, 1, self.getIt2, self.items_variables2)
        print(varsbanana["f1_Banana1"].get())

    def onFrameConfigure(self, event):
        '''Reset the scroll region to encompass the inner frame'''
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

    def onCanvasConfigure(self, event):
        '''Reset the canvas window to encompass inner frame when required'''
        canvas_width = event.width
        self.canvas.itemconfig(self.canvas_window, width=canvas_width) #whenever the size of the canvas changes alter the window region respectively.

    def check_button(self, r, c, frame, items):
        row = r
        for item in items:
            items[item] = tk.IntVar(value=items[item])
            checkbox = tk.Checkbutton(frame, text=item[3:-1], variable=items[item])
            if items[item].get() == 1:
                checkbox.select()
                checkbox.configure(state='disable')
            else:
                checkbox.deselect()
                print("in")
            checkbox.grid(column=c, row=row, sticky=tk.W)
            row += 1
        return items

if __name__ == "__main__":
    root = tk.Tk()
    example = Example(root)
    example.pack(fill="both", expand=True)
    root.mainloop()
Kahraman
  • 33
  • 8

1 Answers1

0

You've explicitly set the width of the notebook to 100 pixels. That is less than the width of the canvas so there's nothing to scroll. If you want it wider so that it can scroll, make it wider than the canvas.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685