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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • syed_saqib_ali@yahoo.com

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

    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.BO TH in the pack command??

    -Saqib

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

    import Tkinter

    class testApp2:

    def _setupCanvas(se lf):
    self._canvasFra me = Tkinter.Frame(s elf._overallFra me, bd=1,
    relief=Tkinter. SUNKEN)
    self._canvasFra me.pack(expand= Tkinter.YES, fill=Tkinter.BO TH)

    self._canvas = Tkinter.Canvas( self._canvasFra me,
    background="whi te", width=self._can vasWidth,
    height=self._ca nvasHeight,)

    # Scroll Bars
    vScrollbar = Tkinter.Scrollb ar(self._canvas Frame)
    hScrollbar = Tkinter.Scrollb ar(self._canvas Frame)

    # Scroll Bars
    vScrollbar = Tkinter.Scrollb ar(self._canvas Frame)
    vScrollbar.pack (side=Tkinter.L EFT, expand=Tkinter. NO,
    fill=Tkinter.NO NE)

    hScrollbar = Tkinter.Scrollb ar(self._canvas Frame)
    hScrollbar.pack (side=Tkinter.T OP, expand=Tkinter. NO,
    fill=Tkinter.NO NE)

    # Configure
    self._parent.ro wconfigure(0, weight=1)
    self._parent.co lumnconfigure(0 , weight=1)
    # self._scrollX0 = self._scrollY0 = 0
    # self._scrollX1 = self._canvasWid th
    # self._scrollY1 = self._canvasHei ght

    print "self._canvasWi dth = %s" % self._canvasWid th
    print "self._canvasHe ight = %s" % self._canvasHei ght
    # print "self._scro llX1 = %s" % self._scrollX1
    # print "self._scro llY1 = %s" % self._scrollY1
    self._canvas.co nfig(
    width=self._can vasWidth,
    height=self._ca nvasHeight,
    scrollregion=(0 ,0, self._canvasWid th, self._canvasHei ght),
    yscrollcommand= vScrollbar.set,
    xscrollcommand= hScrollbar.set,
    )

    vScrollbar.conf ig(orient=Tkint er.VERTICAL,
    command=self._c anvas.yview)
    hScrollbar.conf ig(orient=Tkint er.HORIZONTAL,
    command=self._c anvas.xview)

    self._canvasFra me.pack()
    self._canvas.pa ck(expand=Tkint er.YES, fill=Tkinter.BO TH)
    vScrollbar.pack (side=Tkinter.R IGHT, expand=Tkinter. YES,
    fill=Tkinter.Y)
    hScrollbar.pack (side=Tkinter.B OTTOM, expand=Tkinter. YES,
    fill=Tkinter.X)

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

    self._overallFr ame = Tkinter.Frame(s elf._parent, bd=1,
    relief=Tkinter. SUNKEN)
    self._overallFr ame.pack(expand =Tkinter.YES, fill=Tkinter.BO TH)

    self._canvasWid th = 300
    self._canvasHei ght = 250
    self._setupCanv as()
    self._setCallBa cks()

    def _setCallBacks(s elf):
    # Function Bindings
    self._canvas.bi nd("<Button-1>", self._b1PressEv t)

    def _b1PressEvt(sel f, event):
    print self._canvas.co nfig('scrollreg ion')
    print self._canvas.co nfig('width')
    print self._canvas.co nfig('height')
    print "=" * 50
    print "\n"

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

  • jepler@unpythonic.net

    #2
    Re: How to get ScrollRegion to adjust w/ window-size?

    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(ysc rollcommand=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_rowconfi gure(0, weight=1)
    t.grid_columnco nfigure(0, weight=1)

    c.configure(scr ollregion = (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)

    iD8DBQFC9KY1Jd0 1MZaTXX0RAntrAJ oD3rZ5Se/OdyhC+xkU1nJkMp TITwCfeOEF
    I2rrOgXzrusVp9r MYRBXcBU=
    =AWaw
    -----END PGP SIGNATURE-----

    Comment

    Working...