Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 [Tkinter] Scrollbar, Frame and size of Frame
#1
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)

        scrollbar.config(command=self.canvas.yview)

        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"

        self.update_idletasks()
        self.canvas.config(scrollregion=self.canvas.bbox(self.windows_item))


root = tk.Tk()

root.geometry('1000x700+100+50')
root.resizable('False','False')
header = Frame(root)
body = Frame(root)
footer = Frame(root)
header.pack()
body.pack(fill='x')
footer.pack()
header.configure(width=200, height=100)


l1=Label(header, text="The header")
l1.grid(column=0,row=0)
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)

scrollable_body.update()

root.mainloop()

Quote
#2
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):
        tk.Tk.__init__(self)
        app=Application(self, width=1200, height=800, title="MySizedFrame")
        app.mainloop()

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
        parent.geometry(f"{width+25}x{height+25}")
        self.configure(width=width)
        self.configure(height=height)
        parent.title(title)
        self.grid(sticky='nsew')
        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__':
    GuiClass()
   
Quote
#3
Larz60+, thank you Shy
I used "tkscrolledframe"
from tkscrolledframe import ScrolledFrame
It's easier, i think
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to disable focus on Frame in Tkinter? szafranji 1 309 May-13-2020, 10:45 PM
Last Post: DT2000
  [Tkinter] Needs help to switch frame with functions scotesse 6 214 May-10-2020, 05:18 PM
Last Post: Larz60+
  automatically generates a frame taratata2020 0 98 Apr-19-2020, 09:21 AM
Last Post: taratata2020
  Scroll frame with MouseWheel Nemesis 1 173 Mar-25-2020, 09:29 PM
Last Post: Nemesis
  [Tkinter] Help with Scrollbar JJota 6 553 Mar-10-2020, 05:25 AM
Last Post: Larz60+
  [Tkinter] Tkinter bringing variables from 1 frame to another zukochew 6 3,636 Dec-26-2019, 05:27 AM
Last Post: skm
  Unable to put background image on Tkinter Frame jenkins43 2 2,517 Nov-27-2019, 11:38 AM
Last Post: jenkins43
  [Tkinter] Update variable between class/frame edwin6938 6 513 Nov-22-2019, 08:13 AM
Last Post: edwin6938
  [Tkinter] Menu in frame Elmit 2 636 Sep-20-2019, 01:39 PM
Last Post: Denni
  [Tkinter] Scrollbar doesn't work on Canvas in Tkinter DeanAseraf1 3 2,883 Sep-19-2019, 03:26 PM
Last Post: joe_momma

Forum Jump:


Users browsing this thread: 1 Guest(s)