[Tkinter-discuss] realtime resizing

Stewart Midwinter stewart.midwinter at gmail.com
Wed Apr 20 18:26:51 CEST 2005


Marco:

I see one problem with what you are trying to accomplish. You want to
resize the widget when the mouse passes over the widget and the user
clicks.  Normally you might want to do several things when clicking on
an object, not just resize it.   Perhaps you need to consider a
different mouse event, such as detecting click and drag (mouse-down,
mouse-move) instead.

As to how to specifically accomplish that, my suggestion is to search
around for Tkinter drawing programs.  I've seen a couple of examples
that, IIRC, allow resizing or movement of widgets on the canvas.  Matt
Conway wrote a bunch of canvas demos.  The one attached shows how to
move a widget by dragging it; others may show sizing. Find them all
at:
http://unununium.org/darcs/prebuilt-python/Demo/tkinter/

I hope this helps.


#----------------------
#canvas-moving-with-mouse.py

from Tkinter import *

# this file demonstrates the movement of a single canvas item under
mouse control

class Test(Frame):
    ###################################################################
    ###### Event callbacks for THE CANVAS (not the stuff drawn on it)
    ###################################################################
    def mouseDown(self, event):
	# remember where the mouse went down
	self.lastx = event.x
	self.lasty = event.y

    def mouseMove(self, event):
	# whatever the mouse is over gets tagged as CURRENT for free by tk.
	self.draw.move(CURRENT, event.x - self.lastx, event.y - self.lasty)
	self.lastx = event.x
	self.lasty = event.y

    ###################################################################
    ###### Event callbacks for canvas ITEMS (stuff drawn on the canvas)
    ###################################################################
    def mouseEnter(self, event):
        # the CURRENT tag is applied to the object the cursor is over.
	# this happens automatically.
	self.draw.itemconfig(CURRENT, fill="red")
	
    def mouseLeave(self, event):
	# the CURRENT tag is applied to the object the cursor is over.
	# this happens automatically.
	self.draw.itemconfig(CURRENT, fill="blue")

    def createWidgets(self):
	self.QUIT = Button(self, text='QUIT', foreground='red', 
			   command=self.quit)
	self.QUIT.pack(side=LEFT, fill=BOTH)
	self.draw = Canvas(self, width="5i", height="5i")
	self.draw.pack(side=LEFT)

	fred = self.draw.create_oval(0, 0, 20, 20,
				     fill="green", tags="selected")

	self.draw.tag_bind(fred, "<Any-Enter>", self.mouseEnter)
	self.draw.tag_bind(fred, "<Any-Leave>", self.mouseLeave)

	Widget.bind(self.draw, "<1>", self.mouseDown)
	Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)

    def __init__(self, master=None):
	Frame.__init__(self, master)
	Pack.config(self)
	self.createWidgets()

test = Test()
test.mainloop()




On 4/20/05, Marco Aschwanden <PPNTWIMBXFFC at spammotel.com> wrote:
> Hi
> 
> I am laying out widgets on a canvas. I would like to be able to resize
> this widget on runtime as follows:
> 
> 1. The cursor changes close to the edges of the widget (or when exiting)
> into an appropiate "resizing"-cursor.
> 2. When the user clicks now on the widget he may resize the widget
> (<B1-Motion>).
> 
> How would I do that?
> 
> Here are my thoughts:
> 
> The only way I can think of cheating around:
> 
> 1. Place the the widget XYZ into a frame.
> 2. Pad widget XYZ with 1-2 pixels within the frame.
> 3. When entering the surrounding frame, the "resizing"-cursor is set
> depending on the exit/entry point.
> 4. When the user clicks now <B1-Motion> it will resize the widget XYZ.
> 
> The extra frame and the extra padding space disturbs me. I would like to
> define all "events" on the widget.
> 
> Any suggestions? I found none so far.
> Thanks for any hints,
> Cheers,
> Marco
> 
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
> 


-- 
Stewart Midwinter
stewart at midwinter.ca
stewart.midwinter at gmail.com
Skype: midtoad


More information about the Tkinter-discuss mailing list