[ltk-user] binding an event to a canvas element

cage cage at katamail.com
Fri Jun 22 20:36:26 UTC 2012


On Fri, Jun 22, 2012 at 12:27:59PM -0400, Jerry Vinokurov wrote:

> Right, I understand that you can bind events to the canvas. I don't
> have any trouble doing that. What I want is to bind them to the
> canvas elements; I want to have different pie slices and track which
> ones people click on. I tried using tag-bind but it throws an error:

Ok, i  think you  just used the  wrong function  (i make this  kind of
error often! :)), the correct one is "tagbind" not "tag-bind".

Please see the attached source for a simple example.

Anyway i suggest you use the  solution provided by Peter, a clever way
to accomplish the task, in my opinion.

Bye!
C.
-------------- next part --------------
;; tagbind example
;; Copyright (C) 2012  cage

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.


(require 'ltk)

(use-package 'ltk)


(defun bind-red (e)
  (declare (ignore e))
  (do-msg "click on red!"))

(defun bind-green (e)
  (declare (ignore e))
  (do-msg "click on green!"))


(with-ltk ()
  (let* ((size 500)
	 (canvas (make-canvas nil :width size :height size))
	 (arc1 (create-arc canvas 
			   (/ size 4) (/ size 4) 
			   (* size 3/4) (* size 3/4) :start 0 :extent 180))
	 (arc2 (create-arc canvas 
			   (/ size 4) (/ size 4) 
			   (* size 3/4) (* size 3/4) :start 180 :extent 180)))
    (itemconfigure canvas arc1 "fill" "#ff0000")
    (itemconfigure canvas arc2 "fill" "#00ff00")
    (itemconfigure canvas arc1 "tag" "red")
    (itemconfigure canvas arc2 "tag" "green")
    (tagbind canvas "red" "<ButtonPress-1>" #'bind-red)
    (tagbind canvas "green" "<ButtonPress-1>" #'bind-green)
    (pack canvas)))


More information about the ltk-user mailing list