468,241 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,241 developers. It's quick & easy.

Tough Scrolling question with Tkinter

Please take a look at and run the code snippet shown below.

It creates a canvas with vertical & Horizontal scroll-bars.
If you shrink the window to smaller than the area of the canvas, the
scroll-bars work as advertised. That's great.

However, if you click the Left Mouse button, it calls code which
expands the width of the canvas by 100 pixels. The area being viewed
expands correspondingly..... BUT I DON'T WANT IT TO!!

I want to know how to expand the size of a canvas without changing the
area/size of what is currently shown by the scroll bars. I would like
to find code that expands the width of the canvas and simply adjusts
the H-Scrollbar without changing what is shown on in the area of the
canvas being displayed.

I have tried seemingly every combination of messing with the
canvas.config and scrollregion parameters to no avail. Can someone out
there show me how its done??

-Saqib

-----------------------------------------
import Tkinter

def _b1PressEvt(event):
print "B1"
_canvas.config(width=300)

tkRoot = Tkinter.Tk()
_canvas = Tkinter.Canvas(tkRoot, background="white", width=200,
height=200,)

# Scroll Bars
vScrollbar = Tkinter.Scrollbar(tkRoot)
vScrollbar.pack(side=Tkinter.RIGHT, expand=True, fill=Tkinter.Y)

hScrollbar = Tkinter.Scrollbar(tkRoot)
hScrollbar.pack(side=Tkinter.BOTTOM, expand=True, fill=Tkinter.X)

_canvas.config(
width=200,
height=200,
scrollregion=(0,0,100,100),
yscrollcommand=vScrollbar.set,
xscrollcommand=hScrollbar.set,
)

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

#tkRoot.pack()
_canvas.pack(expand=Tkinter.NO)
vScrollbar.pack(side=Tkinter.RIGHT, expand=True, fill=Tkinter.Y)
hScrollbar.pack(side=Tkinter.BOTTOM, expand=True, fill=Tkinter.X)

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

tkRoot.mainloop()

Jul 19 '05 #1
1 2548
Hi Saqib,

AFAIU, you want to change the scrollregion, not the size. So try:

def _b1PressEvt(event):
x0,y0,x1,y1 = canvas.config('scrollregion')[4]
print x0,y0,x1,y1
canvas.config(scrollregion=(int(x0),int(y0),int(x1 )+100,int(y1)))

First click will show nothing, because first change keeps the scrollregion
entirely in the window.

Eugene

<sy************@yahoo.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
Please take a look at and run the code snippet shown below.

It creates a canvas with vertical & Horizontal scroll-bars.
If you shrink the window to smaller than the area of the canvas, the
scroll-bars work as advertised. That's great.

However, if you click the Left Mouse button, it calls code which
expands the width of the canvas by 100 pixels. The area being viewed
expands correspondingly..... BUT I DON'T WANT IT TO!!

I want to know how to expand the size of a canvas without changing the
area/size of what is currently shown by the scroll bars. I would like
to find code that expands the width of the canvas and simply adjusts
the H-Scrollbar without changing what is shown on in the area of the
canvas being displayed.

I have tried seemingly every combination of messing with the
canvas.config and scrollregion parameters to no avail. Can someone out
there show me how its done??

-Saqib

-----------------------------------------
import Tkinter

def _b1PressEvt(event):
print "B1"
_canvas.config(width=300)

tkRoot = Tkinter.Tk()
_canvas = Tkinter.Canvas(tkRoot, background="white", width=200,
height=200,)

# Scroll Bars
vScrollbar = Tkinter.Scrollbar(tkRoot)
vScrollbar.pack(side=Tkinter.RIGHT, expand=True, fill=Tkinter.Y)

hScrollbar = Tkinter.Scrollbar(tkRoot)
hScrollbar.pack(side=Tkinter.BOTTOM, expand=True, fill=Tkinter.X)

_canvas.config(
width=200,
height=200,
scrollregion=(0,0,100,100),
yscrollcommand=vScrollbar.set,
xscrollcommand=hScrollbar.set,
)

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

#tkRoot.pack()
_canvas.pack(expand=Tkinter.NO)
vScrollbar.pack(side=Tkinter.RIGHT, expand=True, fill=Tkinter.Y)
hScrollbar.pack(side=Tkinter.BOTTOM, expand=True, fill=Tkinter.X)

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

tkRoot.mainloop()

Jul 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Toph | last post: by
1 post views Thread by Toph | last post: by
3 posts views Thread by mksql | last post: by
3 posts views Thread by Bob Greschke | last post: by
reply views Thread by syed_saqib_ali | last post: by
1 post views Thread by Michael Yanowitz | last post: by
4 posts views Thread by D | last post: by
1 post views Thread by blackielawless | last post: by
4 posts views Thread by BartlebyScrivener | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.