Feedback messages to user using Tk

Hi all!

If anyone is very familiar with Tk I would be very grateful for a few
pointers.

Basically I’m looking to feedback my program’s output to the user in a
GUI (Tk Label) rather than printing to the console (which it currently
does). This information would simply be a string to let the user know
what’s going on and maybe even what percentage of the work has been
done. I don’t want a command/bound event to drive this - I simply want
to send a message to a method which will update the Tk Label
dynamically.

I envision the calling the class like this:

@output = GraphicalOutput.new
@output.message("Now moving files....")

to replace the current:
p “Now moving files…”

Is this possible with Tk? I’ve been looking for days now and haven’t
found much. I know how to use the TkLabel.configure(‘text’=>…) etc. to
update the text, however just calling this when the mainloop is running
does not seem to dynamically update the label. I’ve even got the
stopwatch example going from the O’Reilly cookbook.

Am I using the wrong approach and/or mentality for this problem?!

Again I will be very grateful for anyone’s help! Thanks in advance!!!

Gemma


This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.


Cameron, Gemma (UK) wrote:

If anyone is very familiar with Tk I would be very grateful for a few
pointers.

Basically I’m looking to feedback my program’s output to the user in a
GUI (Tk Label) rather than printing to the console (which it currently
does).

From someone definitely NOT “very familiar with Tk”…

Nathaniel T.'s Test::Unit is a unit testing framework for the Ruby
programming language that ships as part of the standard Ruby
distribution.

As well as a character-based console testrunner it has a variety of
gui-based testrunners, including one for Tk.

I wonder if comparing test/unit/ui/console/testrunner.rb and
test/unit/ui/tk/testrunner.rb might give you an example of what you
seek?

Cameron, Gemma (UK) wrote:

Basically I’m looking to feedback my program’s output to the user in a
GUI (Tk Label) rather than printing to the console (which it currently
does). This information would simply be a string to let the user know
what’s going on and maybe even what percentage of the work has been
done. I don’t want a command/bound event to drive this - I simply want
to send a message to a method which will update the Tk Label

I actually want to do this too.
Im not on the point yet where i have to implement it, therefore havent
came across anything that explains how to do it.
but please post here if you come up with something.
It would be very useful.
Ill do the same if I stumble across anything.

On Sep 27, 2006, at 4:26 AM, Cameron, Gemma (UK) wrote:

work has been done. I don’t want a command/bound event to drive

Is this possible with Tk? I’ve been looking for days now and
haven’t found much. I know how to use the TkLabel.configure
(‘text’=>…) etc. to update the text, however just calling this
when the mainloop is running does not seem to dynamically update
the label. I’ve even got the stopwatch example going from the
O’Reilly cookbook.

Am I using the wrong approach and/or mentality for this problem?!

Ruby/Tk makes it easy to do what you want. You don’t need to bother
with low-level methods like ‘configure’ – Ruby/Tk provides more
runbyish ways – in this case access methods for a label’s text. I
hope the following small example will help you.

#! /usr/bin/ruby -w

require ‘tk’

DEBUG = []

MESSAGES = [
“This is the first message”,
“This is another message”,
“This is the third message”,
“Are you getting bored?”
]

class TestWindow
# Each time the button is clicked on, the label shows a new message.
def btn_action
@indx = (@indx + 1) % MESSAGES.length
@lbl.text = MESSAGES[@indx]
end

def initialize
   begin
      @indx = 0

      # Set up the widgets.
      root = TkRoot.new { title 'Ruby Tk' }
      @lbl = TkLabel.new { text MESSAGES.first }
      @btn = TkButton.new { text "Next Message" }
      @btn.command = lambda { btn_action }
      @lbl.pack(:pady => 20)
      @btn.pack(:pady => 20)

      # Set initial window geometry; i.e., size and placement.
      win_w, win_h = 300, 135
      # root.minsize(win_w, win_h)
      win_x = (root.winfo_screenwidth - win_w) / 2
      root.geometry("#{win_w}x#{win_h}+#{win_x}+50")

      # Set resize permissions.
      root.resizable(false, false)

      # Make Cmnd+Q work as expected (running on OS X).
      root.bind('Command-q') {Tk.root.destroy}

      Tk.mainloop
   ensure
      puts DEBUG unless DEBUG.empty?
   end
end

end

TestWindow.new

Regards, Morton