0

i tried to copy and paste the code from furas (i was looking for something like that): Vertical scrollbar for frame in Tkinter, Python

This code create: - a window with tkinter - a frame - and a vertical scrollbar

The only issue is that the frame does not fit to the root window. Can you please let me know how to fit the frame to the main window with that code (i changed few things in the code of the link, please find it below)?

no error messages are generated, but if you run it, you will see that the frame does not fit to the root window

Thank you for your help

import tkinter as tk

from tkinter import *

def on_configure(event):
    # update scrollregion after starting 'mainloop'
    # when all widgets are in canvas
    canvas.configure(scrollregion=canvas.bbox('all'))


root = tk.Tk()
root.geometry("1200x1000")

# --- create canvas with scrollbar ---

canvas = tk.Canvas(root)
canvas.pack(side=tk.LEFT)

scrollbar = tk.Scrollbar(root, command=canvas.yview)
scrollbar.pack(side=tk.LEFT, fill='y')

canvas.configure(yscrollcommand = scrollbar.set)

# update scrollregion after starting 'mainloop'
# when all widgets are in canvas
canvas.bind('<Configure>', on_configure)

# --- put frame in canvas ---

frame = tk.Frame(master=root, width=980, height=980)
canvas.create_window((0,0), window=frame, anchor='nw')
#frame.pack_propagate(0) #Don't allow the widgets inside to determine the frame's width / height
#frame.pack(fill=tk.BOTH, expand=True) #Expand the frame to fill the root window

# --- add widgets in frame ---

l = tk.Label(frame, text="Hello", font="-size 50")
l.pack()

l = tk.Label(frame, text="World", font="-size 50")
l.pack()

l = tk.Label(frame, text="Test text 1\nTest text 2\nTest text 3\nTest text 4\nTest text 5\nTest text 6\nTest text 7\nTest text 8\nTest text 9", font="-size 20")
l.pack()


# --- start program ---

root.mainloop()
DoctorShe
  • 17
  • 4
  • 1
    Just to let you know. `from tkinter import *` is doing nothing for you hear. You can delete that line. That said change `canvas.pack(side=tk.LEFT)` to `canvas.pack(side=tk.LEFT, fill='both', expand=True)`. – Mike - SMT Feb 06 '20 at 22:55
  • Hi Mike, thank you very much, that totally worked! – DoctorShe Feb 07 '20 at 00:17
  • Glad to help. Take some time to read up on pack() and grid(). Personal I prefer grid() for all my applications. – Mike - SMT Feb 07 '20 at 01:16
  • thank you mike! i prefer grid as well (i just copied that part from the original code that i found on stack overflow) and i did not know i could use grid in this occasion thats why i left pack(). thank you for the extra tip! really appreciate – DoctorShe Feb 07 '20 at 06:19

0 Answers0