Python Forum
[Tkinter] Scrollbar, Frame and size of Frame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Scrollbar, Frame and size of Frame
Hello everybody.
I use this script and it works good.
But, i don't know how to do Frame(body) more bigger by vertically.
Could somebody help me? Rolleyes Rolleyes Rolleyes

import tkinter as tk
from tkinter import ttk
from tkinter import *

class Scrollable(tk.Frame):
       Make a frame scrollable with scrollbar on the right.
       After adding or removing widgets to the scrollable frame, 
       call the update() method to refresh the scrollable area.

    def __init__(self, frame, width=16):

        scrollbar = tk.Scrollbar(frame, width=width)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y)#, expand=False)

        self.canvas = tk.Canvas(frame, yscrollcommand=scrollbar.set)
        self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)


        self.canvas.bind('<Configure>', self.__fill_canvas)

        # base class initialization
        tk.Frame.__init__(self, frame)         

        # assign this obj (the inner frame) to the windows item of the canvas
        self.windows_item = self.canvas.create_window(0,0, window=self, anchor=tk.NW)

    def __fill_canvas(self, event):
        "Enlarge the windows item to the canvas width"

        canvas_width = event.width
        self.canvas.itemconfig(self.windows_item, width = canvas_width)        

    def update(self):
        "Update the canvas and the scrollregion"


root = tk.Tk()

header = Frame(root)
body = Frame(root)
footer = Frame(root)
header.configure(width=200, height=100)

l1=Label(header, text="The header")
ttk.Label(footer, text="The Footer").pack()

scrollable_body = Scrollable(body, width=32)

for i in range(30):
    ttk.Button(scrollable_body, text="I'm a button in the scrollable frame").grid(column=0,row=i)
    ttk.Button(scrollable_body, text="I'm").grid(column=1,row=i)


first, since you are inheriting tk.Frame, I'd move the initialization of that as the first item in init, and don't pass frame to init, rather use:
Note that I added defaults for width, height and title
import tkinter as tk

class GuiClass(tk.Tk):
    def __init__(self):
        app=Application(self, width=1200, height=800, title="MySizedFrame")

class Application(tk.Frame):    
    def __init__(self, parent, width=800, height=600, title="No Title"):
        tk.Frame.__init__(self, parent)
        # parent needs to be larger than frame, to accomidate scrollbar
        self.add_widgets(self, width, height)
    def add_widgets(self, parent, width, height):
        scrollbar = tk.Scrollbar(parent)
        scrollbar.pack(side=tk.RIGHT, fill=tk.Y, expand=True)

        self.canvas = tk.Canvas(parent, bd=5, width=width, height=height, yscrollcommand=scrollbar.set)
        self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

if __name__ == '__main__':
Larz60+, thank you Shy
I used "tkscrolledframe"
from tkscrolledframe import ScrolledFrame
It's easier, i think

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Frame with treeview virencm 5 2,647 Feb-01-2022, 04:58 PM
Last Post: deanhystad
  [Tkinter] Scrollbar apffal 7 1,455 Oct-11-2021, 08:26 PM
Last Post: deanhystad
  [Tkinter] Trouble changing Font within tkinter frame title AnotherSam 1 1,243 Sep-30-2021, 05:57 PM
Last Post: menator01
  [Tkinter] How to create scrollable frame mandiatutti 7 1,602 Aug-07-2021, 03:34 PM
Last Post: deanhystad
  [Tkinter] using different frame for some widgets rwahdan 1 1,444 Jul-13-2021, 08:32 AM
Last Post: Yoriz
  tkinter frame camera opencv Nick_tkinter 9 2,764 Mar-21-2021, 06:41 PM
Last Post: Nick_tkinter
Question [Tkinter] How to configure scrollbar dimension? water 6 1,858 Jan-03-2021, 06:16 PM
Last Post: deanhystad
  align frame inside canvas ro_btz 1 1,038 Sep-11-2020, 04:38 PM
Last Post: Larz60+
  Creating a frame with 4 command buttons Heyjoe 5 1,350 Aug-21-2020, 03:16 PM
Last Post: deanhystad
  [Tkinter] Trying to change font size w/o changing button size python63 3 7,202 Aug-05-2020, 01:04 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020