PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > Ruby/Tk - binding to canvas items
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Ruby/Tk - binding to canvas items

Réponse
 
LinkBack Outils de la discussion
Vieux 08/01/2008, 21h38   #1
Witold Rugowski
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Ruby/Tk - binding to canvas items

Hi!
I have canvas items (TkcLine to be exact) and I need to bind some code
executed on click. I use code similar to this:

line = TkcLine.new($root, arg, :fill => '#000', :width => 2)
line.bind("Button-1") {|e|
#some actions
}

It works, but I dont know how to get which item was clicked.

I have bunch of lines in one canvas. Event passed to code does refers
only to widget which was clicked, but this is canvas itself, not
particular canvas item. Is possible to know which line was clicked?

Best regards,
--
Witold Rugowski
http://nhw.pl/wp/ (EN blog)
http://FriendsFeedMe.com
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 09/01/2008, 02h21   #2
Joel VanderWerf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby/Tk - binding to canvas items

Witold Rugowski wrote:
> Hi!
> I have canvas items (TkcLine to be exact) and I need to bind some code
> executed on click. I use code similar to this:
>
> line = TkcLine.new($root, arg, :fill => '#000', :width => 2)
> line.bind("Button-1") {|e|
> #some actions
> }
>
> It works, but I dont know how to get which item was clicked.


I just tested that this works:

line.bind("Button-1") {|e|
p line
}

It's a closure, so you can refer to local scope.

There's one thing to watch out for:

line = TkcLine.new...
line.bind("Button-1") {|e|
p line
}

line = TkcLine.new...
line.bind("Button-1") {|e|
p line
}

This creates two lines, but the scopes share the same binding for the
local var "line", so the two blocks will both operate on the second
line. Instead, use different var names, or use an #each loop.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  Réponse avec citation
Vieux 09/01/2008, 11h15   #3
Witold Rugowski
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby/Tk - binding to canvas items

Joel VanderWerf wrote:
> This creates two lines, but the scopes share the same binding for the
> local var "line", so the two blocks will both operate on the second
> line. Instead, use different var names, or use an #each loop.


Yeah, use of #each was required, since I was iterating through array and
needed index of elements inside of handler.

Thank You.

--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 09/01/2008, 15h13   #4
Hidetoshi NAGAI
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Ruby/Tk - binding to canvas items

From: Witold Rugowski <rugowski@nhw.pl>
Subject: Ruby/Tk - binding to canvas items
Date: Wed, 9 Jan 2008 06:38:33 +0900
Message-ID: <31383eb79838802c2314f99d24282680@ruby-forum.com>
> I have bunch of lines in one canvas. Event passed to code does refers
> only to widget which was clicked, but this is canvas itself, not
> particular canvas item. Is possible to know which line was clicked?


On Tcl/Tk's canvas manual,
--------------------------------------------------------------------
The tag current is managed automatically by Tk; it applies
to the current item, which is the topmost item whose drawn
area covers the position of the mouse cursor. If the
mouse is not in the canvas widget or is not over an item,
then no item has the current tag.
--------------------------------------------------------------------
So, you should use 'current' to denote the target item.
Or use a TkcTagCurrent object.

For example,
--------------------------------------------------------------------
require 'tk'
c = TkCanvas.new.pack
current = TkcTagCurrent.new(c)
callback = proc{|w|
p c
p w
p w.itemconfiginfo('current')
p current.configinfo
p w.find_withtag('current')
}

[100, 120, 140, 160, 180, 200].each{|y|
TkcLine.new(c, [50, y], [200, y]).bind('1', callback, '%W')
# or .bind('1', '%W', &callback)
}
Tk.mainloop
--------------------------------------------------------------------
--
Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 00h20.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,36563 seconds with 12 queries