widget modifs after calls to Tk.mainloop

N

Nicolas Decoster

Hi.

I'm new to ruby and used to Tcl/Tk.

Is it possible using Ruby/Tk to create widgets, then view and
interactivly interact with them, then change their behavior and view and
interact again with them. For example in Tcl/Tk shell (wish):

% button .b -command {puts $globVar}
..b
% pack .b
% set globVar 1
1

Click on button displays 1

% set globVar 2
2

Click on button displays 2

% .b configure -command {puts $globVar$globVar}

Click on button displays 22



Is there a similar command sequence for Ruby/Tk.

I guess this is not possible, since for Tcl/Tk the event loop is started
when the shell is started while for irb it is started only after _full_
widgets settings.


Nicolas.
 
H

Hidetoshi NAGAI

Hi,

From: Nicolas Decoster <[email protected]>
Subject: widget modifs after calls to Tk.mainloop
Date: Wed, 1 Sep 2004 00:00:56 +0900
Message-ID: said:
Is it possible using Ruby/Tk to create widgets, then view and
interactivly interact with them, then change their behavior and view and
interact again with them. For example in Tcl/Tk shell (wish): (snip)
Is there a similar command sequence for Ruby/Tk.

For example,

$ /usr/local/bin/irb
irb(main):001:0> require 'tk'
irb(main):002:0> Thread.new{Tk.mainloop}
irb(main):003:0> v = 1
irb(main):004:0> b = TkButton.new:)command=>proc{puts v}).pack
irb(main):005:0> v = 2
irb(main):006:0> b.command{puts "#{v}#{v}"}
 
P

Phlip

Nicolas said:
I guess this is not possible, since for Tcl/Tk the event loop is started
when the shell is started while for irb it is started only after _full_
widgets settings.

Following up HN's answer...

The most majorly awesome way to do this is out of a unit test. If you write
GUI code thru unit tests, you can display a window in a tested state, by
temporarily operating mainloop().

class TestGrapher < Test::Unit::TestCase

def setup()
@canvas = TkCanvas.new()
@canvas.configure('width', 600)
@canvas.configure('height', 600)
@canvas.grid()
return @canvas
end

def teardown()
@canvas.destroy() if @canvas
end

def maybeMainloop(reveal = false)
if reveal then
Tk.mainloop()
Tk.restart()
@canvas = nil
end
end
....
def test_twoNodes()
svg = generateSvg(
'digraph aGraph { a [label=""]; b [label=""]; }')

putSvgIntoCanvas(svg, @canvas)
all = @canvas.find_all()
assert_equal(all.size(), 2)
assert_equal(all[0].class.name(), 'TkcOval')
assert_equal(all[1].class.name(), 'TkcOval')
maybeMainloop # true
end
....
end

That's not complete - it illustrates a test that creates a canvas,
optionally displays it, exits mainloop, restarts, and optionally destroys
the canvas.

Decomment maybeMainloop # true to see a canvas. (Also take out the stuff
that doesn't work - it's too complex to copy in here.)

Using this technique, you can put one of HN's or your experiments into a
test case.

Writing unit tests to write features is just like running IRB, except much
better. All your features leave a trail of tests, and running them over and
over again keeps your bug rate low.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top