Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollbars for a .jpg image on a Tkinter Canvas in Python

I'm trying to make a jpeg on a canvas scrollable but I can't seem to get my scrollbars to work. Here's some example code:

from Tkinter import *
import Image, ImageTk

root = Tk()

frame = Frame(root, bd=2, relief=SUNKEN)

frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)

xscrollbar = Scrollbar(frame, orient=HORIZONTAL)
xscrollbar.grid(row=1, column=0, sticky=E+W)

yscrollbar = Scrollbar(frame)
yscrollbar.grid(row=0, column=1, sticky=N+S)

canvas = Canvas(frame, bd=0, xscrollcommand=xscrollbar.set, yscrollcommand=yscrollbar.set)
canvas.grid(row=0, column=0, sticky=N+S+E+W)

File = "jpg filepath here"
img = ImageTk.PhotoImage(Image.open(File))
canvas.create_image(0,0,image=img, anchor="nw")

xscrollbar.config(command=canvas.xview)
yscrollbar.config(command=canvas.yview)

frame.pack()
root.mainloop()
like image 497
Symon Avatar asked Mar 21 '11 18:03

Symon


People also ask

How do I make tkinter scrollable?

The a value gives the position of the left or top edge of the slider, for horizontal and vertical scrollbars respectively; the b value gives the position of the right or bottom edge. To connect a scrollbar to another widget w, set w's xscrollcommand or yscrollcommand to the scrollbar's set() method.

How do I make an image scrollable in a div?

Set the overflow-x:hidden; and overflow-y:auto; that will automatically hide the horizontal scroll bar and present only vertical scrollbar. Here the scroll div will be vertically scrollable. Example 1: html.


1 Answers

You need to tell the canvas what part of the drawing space to scroll. Use something like:

canvas.config(scrollregion=canvas.bbox(ALL))

More information can be found here: http://effbot.org/tkinterbook/canvas.htm#coordinate-systems

like image 55
Bryan Oakley Avatar answered Oct 30 '22 18:10

Bryan Oakley