Adding a scrollbar to canvas images for Python Tkinter
I think this trick shouldn't be shared to the masses for free. But i'm just gonna show it to you and you can thank me later.
Here's what I managed to do.
While I was writing a python code for one of my softwares, Banattend, I was forced to use a list of images in a canvas. But then, I didn't know how to scroll down. Until, I found out that I can embed my canvas in a frame and then add a frame in the canvas, technically the canvas can't be related to scrollbar, and that's why I use the second frame. Sounds simple, huh!?!
Most people may try to tell you to use the update() or idle_task functions, which from my experiences, don't really do anything important for your code. So follow this trick below.
First, create a root window and then add a frame inside the window. Then embed a canvas in that frame, and finally put another frame for that canvas. Then attach the scrollbar to the first frame.
In our case, we create myframe in root window and then canvas to be a part of myframe and we insert a frame by the name window, to enable us scroll down and add our objects there. You can basically add images in the space and scroll them down to see your list of pictures or objects.
Here's a sample code.
from Tkinter import *
root = Tk()
sizex = 450
sizey = 300
posx = 100
posy = 100
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
myframe=Frame(root,relief=GROOVE,width=50,height=50,bd=1,bg = "yellow")
myframe.place(x=10,y=30)
def myfunction(event):
canvas.configure(scrollregion=canvas.bbox("all"),width=200,height=150)
news = "hi you can replace this with an image"
canvas=Canvas(myframe,bg = "yellow")
window=Frame(canvas,bg = "yellow")
myscrollbar=Scrollbar(myframe,orient="vertical",command=canvas.yview)
canvas.configure(yscrollcommand=myscrollbar.set)
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
myscrollbar.pack(side="right",fill="y")
canvas.pack(side="left")
canvas.create_window((0,0),window=window,anchor='nw')
window.bind("<Configure>",myfunction)
root.mainloop()
Enjoy your coding experience.
Do not hesitate to comment below if you have any questions or improvements.
Thanks!
Here's what I managed to do.
While I was writing a python code for one of my softwares, Banattend, I was forced to use a list of images in a canvas. But then, I didn't know how to scroll down. Until, I found out that I can embed my canvas in a frame and then add a frame in the canvas, technically the canvas can't be related to scrollbar, and that's why I use the second frame. Sounds simple, huh!?!
Most people may try to tell you to use the update() or idle_task functions, which from my experiences, don't really do anything important for your code. So follow this trick below.
First, create a root window and then add a frame inside the window. Then embed a canvas in that frame, and finally put another frame for that canvas. Then attach the scrollbar to the first frame.
In our case, we create myframe in root window and then canvas to be a part of myframe and we insert a frame by the name window, to enable us scroll down and add our objects there. You can basically add images in the space and scroll them down to see your list of pictures or objects.
Here's a sample code.
from Tkinter import *
root = Tk()
sizex = 450
sizey = 300
posx = 100
posy = 100
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
myframe=Frame(root,relief=GROOVE,width=50,height=50,bd=1,bg = "yellow")
myframe.place(x=10,y=30)
def myfunction(event):
canvas.configure(scrollregion=canvas.bbox("all"),width=200,height=150)
news = "hi you can replace this with an image"
canvas=Canvas(myframe,bg = "yellow")
window=Frame(canvas,bg = "yellow")
myscrollbar=Scrollbar(myframe,orient="vertical",command=canvas.yview)
canvas.configure(yscrollcommand=myscrollbar.set)
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
label = Label(window, text = news).pack()
myscrollbar.pack(side="right",fill="y")
canvas.pack(side="left")
canvas.create_window((0,0),window=window,anchor='nw')
window.bind("<Configure>",myfunction)
root.mainloop()
Enjoy your coding experience.
Do not hesitate to comment below if you have any questions or improvements.
Thanks!
0 comments