1

I'm working on a tkinter personal project which involves displaying a scrollable gallery of images. All of the images are of the same resolution(1920x1080). Images are constantly reshuffled to accommodate for changing frame/window sizes. If all of the images can't be displayed in one view, I want to add a vertical scrollbar to view the images that can't fit in one view. For testing purposes I have a list of 12 image objects which I iterate over and add to the frame. In order to make the frame scrollable, I've taken the advice of others and I've embedded the frame within the canvas, making the canvas widget scrollable. However, if I comment out the line that my comment says to comment out, and uncomment the line that my comment says to uncomment, the scrollbar doesn't properly function, but the images are reshuffled. On the flip side, if I just leave everything as is, the images aren't reshuffled, but the scrollbar works properly. What needs to be done to get the scrolling functionality and reshuffling functionality operating at once?

from tkinter import *
from PIL import Image, ImageTk
image_objs  = [Image.open('testimage1.png'),Image.open('testimage2.png'),Image.open('testimage3.png'),
           Image.open('testimage4.png'),Image.open('testimage5.png'),Image.open('testimage6.png'),
           Image.open('testimage7.png'),Image.open('testimage8.png'),Image.open('testimage9.png'),
           Image.open('testimage10.png'),Image.open('testimage11.png'),Image.open('testimage12.png')]
root = Tk()
canvas = Canvas(root, borderwidth =0, bg = 'Gold')

f = Frame(canvas,  height = 500,width = 500, bg = 'Gold')
widget_list = list()
#f.pack(fill = BOTH, expand = YES) #uncomment this
vsb = Scrollbar(root, orient= 'vertical', command = canvas.yview)
canvas.configure(yscrollcommand = vsb.set)
vsb.pack(side = "right", fill = "y")
canvas.pack(fill = BOTH, expand = YES)
canvas.create_window((0,0), window = f, anchor = 'nw') #comment this out

def configure(event):
    global widget_list
    canvas.configure(scrollregion=canvas.bbox("all"))
    horizontal_ammount = f.winfo_width()//(1920//6) #Image w[![enter image description here][1]][1]idth/ window     width
    image_obj_iter = iter(image_objs)
    for x in widget_list:
        x.grid_forget()
    widget_list = list()
    try:
        if horizontal_ammount > 0:
            for x in range(12//horizontal_ammount):
                for y in range(horizontal_ammount):
                    img = next(image_obj_iter)
                    img = img.resize((img.size[0]//6, img.size[1]//6))
                    img = ImageTk.PhotoImage(img)
                    label = Label(master= f,image = img)
                    label.image = img
                    widget_list.append(label)
                    label.grid(row = x, column = y)
    except StopIteration:
        return

f.bind("<Configure>", configure)
root.mainloop()

-----Reshuffling version without working scrollbar------- enter image description here

-----Non-reshuffling version with working scrollbar----- enter image description here

Mrcitrusboots
  • 317
  • 3
  • 14

0 Answers0