I am creating a gui for my ML project but I am unable to successfully create a vertical scrollbar that actually works. Here's my code
import tkinter as tk
import tkinter.filedialog
from PIL import ImageTk, Image
class App:
def __init__(self, master):
frame = tk.Frame(master,width=500,height=500)
frame.pack()
self.canvas=tk.Canvas(frame,width=500, height=500,borderwidth=0,highlightthickness=0,background='white',scrollregion=(0,0,500,800))
self.canvas.pack()
vbar=tk.Scrollbar(self.canvas,orient="vertical")
vbar.pack(side="right",fill="y")
vbar.config(command=self.canvas.yview)
self.canvas.config(yscrollcommand=vbar.set)
self.canvas.bind("<Configure>",lambda e:self.canvas.configure(scrollregion=self.canvas.bbox('all')))
root.minsize(500,500)
self.button = tk.Button(self.canvas,
text="QUIT", fg="red",
command=frame.quit)
self.button.pack()
self.button2 = tk.Button(self.canvas,
text="Display image",
command=self.Display_image)
self.button2.pack()
def Display_image(self):
f = tkinter.filedialog.askopenfilename(
parent=root, initialdir='D:/Test',
title='Choose file',
filetypes=[('bmp images', '.bmp')]
)
image = ImageTk.PhotoImage(Image.open(f))
l1 = tkinter.Label(self.canvas, image=image)
l1.image = image
l1.pack()
root = tk.Tk()
app = App(root)
root.mainloop()
The code does create a scrollbar but is not functional. However when I include this line of code self.canvas.create_rectangle((0,0,500,800))
in the code the rectangle scrolls.