Skip to content

Instantly share code, notes, and snippets.

@Rishi-Bidani
Last active May 31, 2021 10:36
Show Gist options
  • Save Rishi-Bidani/76660398183c6d08a363be467953aae4 to your computer and use it in GitHub Desktop.
Save Rishi-Bidani/76660398183c6d08a363be467953aae4 to your computer and use it in GitHub Desktop.
This python class will create a vertical scroll region for you to use in any tkinter project.
import tkinter as tk
from tkinter import ttk
class CreateVerticalScrollRegion:
def __init__(self, frame, canvascolor, scrollSide=tk.RIGHT):
self.frame = frame
self.canvascolor = canvascolor
self.scrollSide = scrollSide
self.canvas = tk.Canvas(self.frame)
self.canvasFrame = tk.Frame(self.canvas)
self.yscroll = ttk.Scrollbar(self.frame, orient="vertical", command=self.canvas.yview)
self.yscroll.pack(side=self.scrollSide, fill="y")
self.canvas.pack(side=self.scrollSide, fill="both", expand="yes")
self.canvas.config(bg=self.canvascolor)
self.canvasFrame.config(bg=self.canvascolor)
self.canvas.configure(yscrollcommand=self.yscroll.set)
self.canvas.create_window((0, 0), window=self.canvasFrame, anchor="nw")
def removeScroll(self):
self.yscroll.pack_forget()
def bindScrollAction(self):
self.canvas.bind("<Configure>", lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all")))
def returnFrame(self):
return self.canvasFrame
""""
=====================================================================================
HOW TO USE
=====================================================================================
Just call the class with the following parameters
----------
params:
frame: Give it a frame it should create the scrolling screen on
canvascolor: Colour for the background colour
scrollSide: The side the scrollbar should sit on, default is right
---------
methods:
bindScrollAction: This the scrollbar, depending on your situation you can
choose to not bind it if u have less than certain number
of items
removeScroll: Hide the scrollbar, depending on situation
returnFrame: This method will return the frame you can add you widgets
on.
=====================================================================================
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment