Tk question and tk solution


(MENON Jean-Francois) #1

hello,

  • the question:
    I don’t remember how to change (or hide) the mouse cursor with tk. (and
    what other cursors I can use)

  • the solution:

in a recent question about tix balloons, I had an answer that works
well, if there is tk-ext installed on my host.(thx Mr Nagai)
But I realise that I could do the same with pure tk code, wich is more
portable.
So I send you an example code in the hope it could be useful for someone
:->

require ‘tk’

def help(ev)

if $top.nil?
$top=TkToplevel.new {geometry “+#{ev.x_root}+#{ev.y_root}“
overrideredirect true}
l= TkLabel.new($top,‘text’=>‘coucou’,‘background’=>‘yellow’).pack
else
$top.geometry(”+#{ev.x_root}+#{ev.y_root}”)
end

$top.deiconify
end

def destroy_help
$top.withdraw
end

$root= TkRoot.new
frame= TkFrame.new.pack
b= TkButton.new($frame,‘text’=>‘texte’).pack
b.bind(‘Enter’,proc { | ev| help(ev)})
b.bind(‘Leave’,proc { destroy_help})

Tk.mainloop

···


/ do you play Go? \

http://jeanfrancois.menon.free.fr/rubygo |
\ /


    \   ^__^
     \  (oo)\_______
        (__)\       )\/\
            >>----w |
            >>     >>

(Hidetoshi NAGAI) #2

Hi,

···

From: MENON Jean-Francois Jean-Francois.MENON@meteo.fr
Subject: tk question and tk solution
Date: Thu, 20 Jun 2002 00:33:33 +0900
Message-ID: 3D10A43F.B9A3325B@meteo.fr

  • the question:
    I don’t remember how to change (or hide) the mouse cursor with tk. (and
    what other cursors I can use)

  • the solution:

in a recent question about tix balloons, I had an answer that works
well, if there is tk-ext installed on my host.(thx Mr Nagai)
But I realise that I could do the same with pure tk code, wich is more
portable.
So I send you an example code in the hope it could be useful for someone

How about the following BalloonHelp class?
It’s a part of a sample script which I wrote for a book
"Ruby Application Programming".
(http://www.amazon.co.jp/exec/obidos/ASIN/4274064611/qid=1024543390/sr=1-1/ref=sr_1_2_1/250-8255844-6093833)
A test script of BalloonHelp class is

===================================================
require 'tkballoonhelp’
b = TkButton.new(nil, ‘text’=>‘QUIT’, ‘command’=>proc{exit}).pack
h = BalloonHelp.new(b, ‘text’=>‘exit this script’, ‘interval’=>250)
Tk.mainloop

Move the mouse cursor to the ‘QUIT’ button,
and wait 250ms without moving the mouse.
Then the mouse cursor will be changed to ‘crosshair’ and
the balloonhelp message ‘exit this script’ will be displayed.
It is a subclass of TkLabel.
So, a BalloonHelp object has the same properties as a TkLabel object
(‘foreground’, ‘background’, and so on).

Here is tkballoonhelp.rb.
The original script has some Japanese comments.
But I remove them for this ML. Sorry.

You can get this and some other sample scripts

from the suppliment CD-ROM of “Ruby Application Programming”. :slight_smile:

===================================================

tkballoonhelp.rb :

2000/05/01 Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)

require ‘tk’

class BalloonHelp<TkLabel
def _balloon_binding(interval)
@timer = TkAfter.new(interval, 1, proc{show})
@bindtag = TkBindTag.new
@bindtag.bind(‘Enter’, proc{@timer.start})
@bindtag.bind(‘Motion’, proc{@timer.restart; erase})
@bindtag.bind(‘Any-ButtonPress’, proc{@timer.restart; erase})
@bindtag.bind(‘Leave’, proc{@timer.stop; erase})
tags = @parent.bindtags
idx = tags.index(@parent)
tags[idx,0] = @bindtag
@parent.bindtags(tags)
end
private :_balloon_binding

def initialize(parent=nil, keys=nil)
@parent = parent

@frame = TkToplevel.new(@parent)
@frame.withdraw
@frame.overrideredirect(true)
@frame.transient(TkWinfo.toplevel(@parent))
@epath = @frame.path 

@message = keys['text']

interval = keys.delete('interval'){2000}
_balloon_binding(interval)

@label = TkLabel.new(@frame, 'background'=>'yellow').pack
@label.configure(keys) if keys
@path = @label

end

def epath
@epath
end

def message
@message
end

def show
x = TkWinfo.pointerx(@parent)
y = TkWinfo.pointery(@parent)
@label.text(message)
@frame.geometry("+#{x+1}+#{y+1}")
@frame.deiconify
@frame.raise

@org_cursor = @parent['cursor']
@parent.cursor('crosshair') 

end

def erase
@parent.cursor(@org_cursor)
@frame.withdraw
end

def destroy
@frame.destroy
end
end


Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)