Add scroll bars to tkinter window in python

I have a list of data of of 25 names to be entered in for a 10 classes.

The requires adding scroll bars to the tkinter window

To make the codes look simple, I have made the tkinter window with Class-1 and 3 names.

###  Import Libraries
from tkinter import *
import tkinter as tk 
from tkinter import ttk


###  Create Tkinter window
window = tk.Tk()
window.state("zoomed")


### Create function to strore names for Class-1 
def Update_Class1():
      
    student_Class1_Name1                               = entry_Class1_Name1.get()
    student_Class1_Name1_to_display                    = "\n" + str(student_Class1_Name1) + "\n"
    student_Class1_Name1_to_display_label              = Label(window)
    student_Class1_Name1_to_display_label["text"]      = student_Class1_Name1_to_display
    student_Class1_Name1_to_display_label.grid(column=1, row=4)

    student_Class1_Name2                               = entry_Class1_Name2.get()
    student_Class1_Name2_to_display                    = "\n" + str(student_Class1_Name2) + "\n" 
    student_Class1_Name2_to_display_label              = Label(window)
    student_Class1_Name2_to_display_label["text"]      = student_Class1_Name2_to_display
    student_Class1_Name2_to_display_label.grid(column=2, row=4)

    student_Class1_Name3                               = entry_Class1_Name3.get()
    student_Class1_Name3_to_display                    = "\n"  + str(student_Class1_Name3) + "\n"   
    student_Class1_Name3_to_display_label              = Label(window)
    student_Class1_Name3_to_display_label["text"]      = student_Class1_Name3_to_display
    student_Class1_Name3_to_display_label.grid(column=3, row=4)


### Put Labels in the window
tk.Label(window, text="\n\n\n\n\n\n\n\n Class Information \n\n\n\n\n\n").grid(column=0, row=1)
tk.Label(window, text="\n\n\n\n\n\n\n\n   Class Number    \n\n\n\n\n\n").grid(column=0, row=2)                
tk.Label(window, text="\n\n\n\n\n\n\n\n     Class-1       \n\n\n\n\n\n").grid(column=0, row=3)                   


### Put Entry boxes in window
tk.Label(window, text="\t\t\t\t\t\t Name-1 \t\t\t\t\t\t").grid(column=1, row=2)                                   
entry_Class1_Name1 = tk.Entry(window , width=16)
entry_Class1_Name1.grid(column=1, row=3)

tk.Label(window, text="\t\t\t\t\t\t Name-2 \t\t\t\t\t\t").grid(column=2, row=2)                                   
entry_Class1_Name2 = tk.Entry(window , width=16)
entry_Class1_Name2.grid(column=2, row=3)

tk.Label(window, text="\t\t\t\t\t\t Name-3 \t\t\t\t\t\t").grid(column=3, row=2)                                   
entry_Class1_Name3 = tk.Entry(window , width=16)
entry_Class1_Name3.grid(column=3, row=3)


### Create button for Class-1
Button_Class1 = tk.Button(window, text="Update Class-1", command=Update_Class1)
Button_Class1.grid(column=1, row=6)


###  Insert scroll bars
##vbar = ttk.Scrollbar(window, orient=VERTICAL)      ## vertical scrollbar
##vbar.grid(row=0, column=1, sticky=NS)
## 
##hbar = ttk.Scrollbar(window, orient=HORIZONTAL)    ## horizontal scrollbar
##hbar.grid(row=1, column=0, sticky=EW)
 

### END 
window.mainloop()

(In order to see what actual codes do, simply disable all the \n and \t characters.)

Can somebody please let me know, how do I add the scroll bars to the existing codes so that I can have access to Update button and Name-3 entry box?

1 answer

  • answered 2021-07-27 03:55 Ranjan Pal

    With suggestion of @Bryan Oakley and @TheLizzard I have been able to fix my earlier codes to add scrollbars. The reference has been taking from following link Unable to Scroll Frame using Mouse Wheel & Adding Horizontal Scrollbar

    import tkinter as tk
    from tkinter import *
    
    FIT_WIDTH = "fit_width"
    FIT_HEIGHT = "fit_height"
    
    
    class ScrollableFrame(tk.Frame):
        """
        There is no way to scroll <tkinter.Frame> so we are
        going to create a canvas and place the frame there.
        Scrolling the canvas will give the illution of scrolling
        the frame
        Partly taken from:
            https://blog.tecladocode.com/tkinter-scrollable-frames/
            https://stackoverflow.com/a/17457843/11106801
    
        master_frame---------------------------------------------------------
        | dummy_canvas-----------------------------------------  y_scroll--  |
        | | self---------------------------------------------  | |         | |
        | | |                                                | | |         | |
        | | |                                                | | |         | |
        | | |                                                | | |         | |
        | |  ------------------------------------------------  | |         | |
        |  ----------------------------------------------------  |         | |
        |                                                        |         | |
        | x_scroll---------------------------------------------  |         | |
        | |                                                    | |         | |
        |  ----------------------------------------------------   ---------  |
         --------------------------------------------------------------------
        """
        def __init__(self, master=None, scroll_speed=2,  hscroll=False, vscroll=True, **kwargs):
            assert isinstance(scroll_speed, int), "`scroll_speed` must be an int"
            self.scroll_speed = scroll_speed
    
            self.master_frame = tk.Frame(master)
            self.dummy_canvas = tk.Canvas(self.master_frame, **kwargs)
            super().__init__(self.dummy_canvas)
    
            # Create the 2 scrollbars
            if vscroll:
                self.v_scrollbar = tk.Scrollbar(self.master_frame,orient="vertical",command=self.dummy_canvas.yview)
                self.v_scrollbar.pack(side="right", fill="y")
                self.dummy_canvas.configure(yscrollcommand=self.v_scrollbar.set)
            if hscroll:
                self.h_scrollbar = tk.Scrollbar(self.master_frame,orient="horizontal", command=self.dummy_canvas.xview)
                self.h_scrollbar.pack(side="bottom", fill="x")
                self.dummy_canvas.configure(xscrollcommand=self.h_scrollbar.set)
    
            # Bind to the mousewheel scrolling
            self.dummy_canvas.bind_all("<MouseWheel>", self.scrolling_windows,add=True)
            self.dummy_canvas.bind_all("<Button-4>", self.scrolling_linux, add=True)
            self.dummy_canvas.bind_all("<Button-5>", self.scrolling_linux, add=True)
            self.bind("<Configure>", self.scrollbar_scrolling, add=True)
    
            # Place `self` inside `dummy_canvas`
            self.dummy_canvas.create_window((0, 0), window=self, anchor="nw")
            # Place `dummy_canvas` inside `master_frame`
            self.dummy_canvas.pack(side="top", expand=True, fill="both")
    
            self.pack = self.master_frame.pack
            self.grid = self.master_frame.grid
            self.place = self.master_frame.place
            self.pack_forget = self.master_frame.pack_forget
            self.grid_forget = self.master_frame.grid_forget
            self.place_forget = self.master_frame.place_forget
    
        def scrolling_windows(self, event):
            assert event.delta != 0, "On Windows, `event.delta` should never be 0"
            y_steps = int(-event.delta/abs(event.delta)*self.scroll_speed)
            self.dummy_canvas.yview_scroll(y_steps, "units")
    
        def scrolling_linux(self, event):
            y_steps = self.scroll_speed
            if event.num == 4:
                y_steps *= -1
            self.dummy_canvas.yview_scroll(y_steps, "units")
    
        def scrollbar_scrolling(self, event):
            region = list(self.dummy_canvas.bbox("all"))
            region[2] = max(self.dummy_canvas.winfo_width(), region[2])
            region[3] = max(self.dummy_canvas.winfo_height(), region[3])
            self.dummy_canvas.configure(scrollregion=region)
    
        def resize(self, fit=None, height=None, width=None):
            if fit == FIT_WIDTH:
                super().update()
                self.dummy_canvas.config(width=super().winfo_width())
            if fit == FIT_HEIGHT:
                super().update()
                self.dummy_canvas.config(height=super().winfo_height())
            if height is not None:
                self.dummy_canvas.config(height=height)
            if width is not None:
                self.dummy_canvas.config(width=width)
        fit = resize
    
    
    if __name__ == "__main__":
        # Example 1
        window = tk.Tk()
        window.state("zoomed")
        frame = ScrollableFrame(window, width=window.winfo_screenwidth(), height=window.winfo_screenheight(), hscroll=True, vscroll=True)
        frame.pack()
    
        ### Create function to strore names for Class-1 
        def Update_Class1():
              
            student_Class1_Name1                               = entry_Class1_Name1.get()
            student_Class1_Name1_to_display                    = "\n" + str(student_Class1_Name1) + "\n"
            student_Class1_Name1_to_display_label              = Label(frame)
            student_Class1_Name1_to_display_label["text"]      = student_Class1_Name1_to_display
            student_Class1_Name1_to_display_label.grid(column=1, row=4)
    
            student_Class1_Name2                               = entry_Class1_Name2.get()
            student_Class1_Name2_to_display                    = "\n" + str(student_Class1_Name2) + "\n" 
            student_Class1_Name2_to_display_label              = Label(frame)
            student_Class1_Name2_to_display_label["text"]      = student_Class1_Name2_to_display
            student_Class1_Name2_to_display_label.grid(column=2, row=4)
    
            student_Class1_Name3                               = entry_Class1_Name3.get()
            student_Class1_Name3_to_display                    = "\n"  + str(student_Class1_Name3) + "\n"   
            student_Class1_Name3_to_display_label              = Label(frame)
            student_Class1_Name3_to_display_label["text"]      = student_Class1_Name3_to_display
            student_Class1_Name3_to_display_label.grid(column=3, row=4)
    
    
        ### Put Labels in the frame
        tk.Label(frame, text="\n\n\n\n\n\n\n\n Class Information \n\n\n\n\n\n").grid(column=0, row=1)
        tk.Label(frame, text="\n\n\n\n\n\n\n\n   Class Number    \n\n\n\n\n\n").grid(column=0, row=2)                
        tk.Label(frame, text="\n\n\n\n\n\n\n\n     Class 1       \n\n\n\n\n\n").grid(column=0, row=3)                   
    
    
        ### Put Entry boxes in frame
        tk.Label(frame, text="\t\t\t\t\t\t Name-1 \t\t\t\t\t\t").grid(column=1, row=2)                                   
        entry_Class1_Name1 = tk.Entry(frame , width=16)
        entry_Class1_Name1.grid(column=1, row=3)
    
        tk.Label(frame, text="\t\t\t\t\t\t Name-2 \t\t\t\t\t\t").grid(column=2, row=2)                                   
        entry_Class1_Name2 = tk.Entry(frame , width=16)
        entry_Class1_Name2.grid(column=2, row=3)
    
        tk.Label(frame, text="\t\t\t\t\t\t Name-3 \t\t\t\t\t\t").grid(column=3, row=2)                                   
        entry_Class1_Name3 = tk.Entry(frame , width=16)
        entry_Class1_Name3.grid(column=3, row=3)
    
    
        ### Create button for Class-1
        Button_Class1 = tk.Button(frame, text="Update Class-A", command=Update_Class1)
        Button_Class1.grid(column=1, row=6)
    
    
        ### END
        window.mainloop()
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum