Python Forum
[Tkinter] Scrollbars on canvas - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] Scrollbars on canvas (/thread-21094.html)



Scrollbars on canvas - Christina - Sep-13-2019

Hello,
I'm trying to create a image viewer in tkinter. I have problem with scrollbars in my app. I want horizontal and vertical scrollbars next to image when the image is zoomed. I use following code to do this (part of my code):
image = Image.open(filename)
photo = ImageTk.PhotoImage(image)
...
if zoom > 1:
        canvas = Canvas(root, width = w*zoom, height = h*zoom, bg=bgcolor)
        canvas.pack(side="top", expand=1, fill=BOTH)
        scroll_bar_x = Scrollbar(canvas, orient = tkinter.HORIZONTAL)
        scroll_bar_x.pack(side="bottom", fill = X, expand= 0)
        scroll_bar_y = Scrollbar(canvas, orient = tkinter.VERTICAL)
        scroll_bar_y.pack(side="right", fill = Y, expand= 0)
        scroll_bar_x["command"] = canvas.xview
        scroll_bar_y["command"] = canvas.yview
        canvas["xscrollcommand"] = scroll_bar_x.set
        canvas["yscrollcommand"] = scroll_bar_y.set
        canvas.create_image(w*zoom/2, h*zoom/2, image=photo)
I see scrollbars next to image but they don't work. I can't use them.
In what part of the code do I have a mistake?

Thank you, Christina


RE: Scrollbars on canvas - woooee - Sep-14-2019

Use the ScrolledFrame from Pmw http://pmw.sourceforge.net/doc/ScrolledFrame.html or PyPi https://pypi.org/project/tkScrolledFrame/


RE: Scrollbars on canvas - Christina - Sep-14-2019

woooee thank you for your advice. tkScrolledFrame works in my app.