Pmw.Counter and Pmw.ScrolledCanvas questions

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Thu Jul 11 05:21:18 EDT 2002


On Thursday 11 Jul 2002 5:41 am, John McMonagle wrote:
> My first question is to do with Pmw.Counter.  I am creating a
> Pmw.Counter like so (see below code sample):
>
>
> Because the label spans two text lines, the entryfield is also
> expanded to this height.
>
> Is it possible to set the height of the entry field so as to only
> appear as a single line ?  Another way to say it is can the frame
> component height and position be changed within the hull component ?
>
>
> My second question is to do with Pmw.ScrolledCanvas.  In my
> application, I have a zoom drag feature.  The user can drag an area in
> the canvas window using button-3-press-motion-release to define an
> area to zoom.  I am able to do the zooming ok but how do you set where
> the horizontal and vertical scrollbars are postioned.  I wish the user
> to see at the top left of the scrolled canvas window the point at
> which they started the drag.  I guess it's just for convenience sake
> so the user does not have to scroll through to their desired point
> after the zoom.


John,

I couldn't find a way round the Counter problem you may have to create you own
multi-line labeled entry widget.

As for the ScrolledCanvas are you using the .scale() canvas method.  If so 
the call looks somthing like

mycanvas.scale("ALL", xorigin, yorigin, xscale, yscale)

and setting xorigin and yorigin to the top left points from you 'zoom box' 
should do what you want, to get those I would bbox the rectangle I guess you 
are drawing when they do the button-3-press-move-release dance.  Oh and don;t 
forget to call resizescrollregion() after zooming. 

This is an example that seems to work (for me!)


import Pmw
from Tkinter import *


class ZoomCanvas(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.canvas=Pmw.ScrolledCanvas(self)
        self.canvas.pack(fill='both', expand='yes')
        ## canvas bindings
        self.canvas.component('canvas').bind('<1>', 
            self.button1down)
        self.canvas.component('canvas').bind('<Button1-Motion>',
            self.button1motion)
        self.canvas.component('canvas').bind('<ButtonRelease-1>',
            self.button1released)
        ## now create some junk on the canvas to zoom!
        self.canvas.create_oval(10, 10, 40, 80)
        self.canvas.create_oval(15, 20, 40, 40)
        self.canvas.create_oval(15, 30, 50, 40)
        self.canvas.create_oval(15, 40, 40, 60)
        
    def button1down(self, event):
        ## print 'Button 1 Down'
        self.startx=self.canvas.canvasx(event.x)
        self.starty=self.canvas.canvasy(event.x)
        
    def button1released(self, event):
        ## print 'Button 1 Released'
        x1, y1, x2, y2 =  self.canvas.bbox("rectangle")
        self.canvas.delete("rectangle")
        ## should work out correct scale factors just using 2 for now!
        self.canvas.scale("all", x1, y1, 2, 2)
        self.canvas.resizescrollregion()
        
    def button1motion(self, event):
        ## print 'Button 1 Move'
        x=self.canvas.canvasx(event.x)
        y=self.canvas.canvasy(event.y)
        if x!=self.startx and y != self.starty:
            self.canvas.delete("rectangle")
            self.canvas.create_rectangle(self.startx, self.starty, x, y,   
                tags='rectangle')
        
        
root=Tk()
zc=ZoomVanvas(root)
zc.pack(fill='both', expand='yes')

root.mainloop()




Regards
Martin








More information about the Python-list mailing list