By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,018 Members | 1,204 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,018 IT Pros & Developers. It's quick & easy.

How to get ScrollRegion to adjust w/ window-size?

P: n/a
Below is a simple code snippet showing a Tkinter Window bearing a
canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine.
When you shrink/resize the window the scrollbars adjust accordingly.

However, what I really want to happen is that the area of the canvas
that the scrollbars show (the Scrollregion) should expand as the window
grows. It doesn't currently do this. although, if the window shrinks
smaller than the original canvas-size, then the scrollregion adjusts
properly.

How can I make it such that the Scrollregion fills the entire space
avaialable to it. I tried all permutations of setting
expand=Tkinter.YES and fill=Tkinter.BOTH in the pack command??

-Saqib

----------------------------------------------------

import Tkinter

class testApp2:

def _setupCanvas(self):
self._canvasFrame = Tkinter.Frame(self._overallFrame, bd=1,
relief=Tkinter.SUNKEN)
self._canvasFrame.pack(expand=Tkinter.YES, fill=Tkinter.BOTH)

self._canvas = Tkinter.Canvas(self._canvasFrame,
background="white", width=self._canvasWidth,
height=self._canvasHeight,)

# Scroll Bars
vScrollbar = Tkinter.Scrollbar(self._canvasFrame)
hScrollbar = Tkinter.Scrollbar(self._canvasFrame)

# Scroll Bars
vScrollbar = Tkinter.Scrollbar(self._canvasFrame)
vScrollbar.pack(side=Tkinter.LEFT, expand=Tkinter.NO,
fill=Tkinter.NONE)

hScrollbar = Tkinter.Scrollbar(self._canvasFrame)
hScrollbar.pack(side=Tkinter.TOP, expand=Tkinter.NO,
fill=Tkinter.NONE)

# Configure
self._parent.rowconfigure(0, weight=1)
self._parent.columnconfigure(0, weight=1)
# self._scrollX0 = self._scrollY0 = 0
# self._scrollX1 = self._canvasWidth
# self._scrollY1 = self._canvasHeight

print "self._canvasWidth = %s" % self._canvasWidth
print "self._canvasHeight = %s" % self._canvasHeight
# print "self._scrollX1 = %s" % self._scrollX1
# print "self._scrollY1 = %s" % self._scrollY1
self._canvas.config(
width=self._canvasWidth,
height=self._canvasHeight,
scrollregion=(0,0, self._canvasWidth, self._canvasHeight),
yscrollcommand=vScrollbar.set,
xscrollcommand=hScrollbar.set,
)

vScrollbar.config(orient=Tkinter.VERTICAL,
command=self._canvas.yview)
hScrollbar.config(orient=Tkinter.HORIZONTAL,
command=self._canvas.xview)

self._canvasFrame.pack()
self._canvas.pack(expand=Tkinter.YES, fill=Tkinter.BOTH)
vScrollbar.pack(side=Tkinter.RIGHT, expand=Tkinter.YES,
fill=Tkinter.Y)
hScrollbar.pack(side=Tkinter.BOTTOM, expand=Tkinter.YES,
fill=Tkinter.X)

def __init__(self, parent):
self._parent = parent

self._overallFrame = Tkinter.Frame(self._parent, bd=1,
relief=Tkinter.SUNKEN)
self._overallFrame.pack(expand=Tkinter.YES, fill=Tkinter.BOTH)

self._canvasWidth = 300
self._canvasHeight = 250
self._setupCanvas()
self._setCallBacks()

def _setCallBacks(self):
# Function Bindings
self._canvas.bind("<Button-1>", self._b1PressEvt)

def _b1PressEvt(self, event):
print self._canvas.config('scrollregion')
print self._canvas.config('width')
print self._canvas.config('height')
print "=" * 50
print "\n"

root = Tkinter.Tk()
app = testApp2(root)
root.mainloop()

Aug 5 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
The ScrollRegion of the canvas gives the area that should be "available"
for scrolling. If this is larger than the visible area of the canvas,
then associated scrollbars will allow scrolling. If this is smaller
than the visible area of the canvas, then the scrollbar will fill with
the "thumb" and no scrolling is possible.

Here is an application that creates a 5000x5000 canvas and allows
scrolling. The scrollbars react appropriately when the window is
resized.
from Tkinter import *

t = Tk()

c = Canvas(t)
hsb = Scrollbar(t, orient="h", command=c.xview)
vsb = Scrollbar(t, orient="v", command=c.yview)
c.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)

c.grid(row=0, column=0, sticky="nsew")
hsb.grid(row=1, column=0, stick="ew")
vsb.grid(row=0, column=1, sticky="ns")

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

c.configure(scrollregion = (0, 0, 5000, 5000))

for x in range(100, 5000, 100):
for y in range(100, 5000, 100):
c.create_text((x,y), anchor=CENTER, text="%d,%d" % (x,y))

t.mainloop()

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)

iD8DBQFC9KY1Jd01MZaTXX0RAntrAJoD3rZ5Se/OdyhC+xkU1nJkMpTITwCfeOEF
I2rrOgXzrusVp9rMYRBXcBU=
=AWaw
-----END PGP SIGNATURE-----

Aug 6 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.