I am having an issue with the tkinter scrollbar. I use a scrollable canvas which displays photos in a grid once loaded the pictures appear in order and the scroll works fine. In my actual app there is a button to scroll to the next set of pictures which all goes smoothly. However, the scroll bar always seems to remain the length of the previous frame. So for example I open a folder with 50 pictures a long scroll appears as it should lets say I tab to the next set of pictures which has 2 images in it the scroll bar remains the same and scrolls way past the images displaying part of the blank canvas. I have replicated the issue with the code below
import tkinter as tk
import os
from tkinter import *
from PIL import Image,ImageTk
from tkinter import messagebox,filedialog # only needed for pop up
# binds mouse middle button to window
def _on_mousewheel(event):
# convert 1.0 to 1 to remove errors
window_event_cords=int(-1 *(event.delta))
# scroll Window
canvas.yview_scroll(window_event_cords, "units")
# Opens the folder selected and downloads photos
def change(b3,b4,b5,b6):
b3.destroy()
b4.destroy()
b5.destroy()
b6.destroy()
# canvas.config(height=100)
def browse_button(input,postedpath,firstcall):
global new_image
img = (Image.open("1.jpg"))
MAX_SIZE = (600, 600)
img.thumbnail(MAX_SIZE)
new_image=(ImageTk.PhotoImage(img))
b3 = tk.Button(scrollable_frame)
b3.config(height=500,width=500,image=new_image,command=lambda: change(b3,b4,b5,b6))
b3.grid(column=2, row=1) # and arranges widgets accordingly
b4 = tk.Button(scrollable_frame)
b4.config(height=500,width=500,image=new_image,command=lambda: change(b3,b4,b5,b6))
b4.grid(column=1, row=1) # and arranges widgets accordingly
b5 = tk.Button(scrollable_frame)
b5.config(height=500,width=500,image=new_image,command=lambda: change(b3,b4,b5,b6))
b5.grid(column=1, row=2) # and arranges widgets accordingly
b6 = tk.Button(scrollable_frame)
b6.config(height=500,width=500,image=new_image,command=lambda: change(b3,b4,b5,b6))
b6.grid(column=2, row=2) # and arranges widgets accordingly
# make root
root = Tk()
root.call('tk', 'scaling', 1.5)
root.geometry("1920x1080")
container = tk.Frame(root)
canvas = tk.Canvas(container)
canvas.config(width=1500,height=550)
scrollbar = tk.Scrollbar(container, orient="vertical", command=canvas.yview)
scrollbar.bind_all("<MouseWheel>", _on_mousewheel)
scrollable_frame = tk.Frame(canvas)
scrollbar.pack(side="right", fill="y")
# bind scroller
scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
# Select box
button2 = tk.Button(root,text="Click Me",command=lambda p="select",first=1: browse_button(p,"None",first))
button2.grid(row=1, column=1)
# canvas
canvas.create_window((0, 0), window=scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
container.grid(row=5, column=1)
canvas.pack(side="left", fill="both", expand=True)
mainloop()
- load the app
- press click me
- 4 photos appear click any of the photos to destroy them all
- You will see the canvas is now blank but showing the scroll still
How would I update the scroll on the fly so if its a long folder a long scroll is added. If its a small folder no scroll is added etc
Thanks any help appreciated i've been trying to figure this out for 4 days constantly now.