Forum: Ruby how to integrate ruby tk/gtk event loop with another kind ev

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Talha O. (Guest)
on 2006-04-24 12:41
(Received via mailing list)
Hello,

I needed to write a test/monitoring program for an application that is
written in C++ which sends and receives messages on sockets. Normally I
would write such a test application in most likely C++, java because I
need
gui. I have been tying to learn ruby for some time, and it is very much
easy
to write all other stuff except the gui part in ruby. I am trying to use
tk
as well as gtk for learning purposes.

I am using ruby 1.8.15 (one click installation for windows) about the tk
library that comes with it. I have downloaded and installed
Ruby-GNOME2-0.14.1.for
windows.
<http://ruby-gnome2.sourceforge.jp/#Ruby-GNOME2-0.1...
I have perl tk book, and trying to translate it to ruby and use the
features
that are explained there. I have noticed that very crucial notebook
widget
is not available. I have also noticed that some of the other features
such
as binding external events and geometry managers except pack manager
that
are available in perl tk are not available in ruby binding. Even such a
small application, this makes tk almost unusable as gui toolkit. Besides
I
could not find a way to integrate an external event loop or process to
Tk
eventloop. There are some explanations about filevent and tk send
interface
which iseemed not available in windows and in ruby.

I find gtk much more convenient especially with the use of glade ide and
libglade ruby binding. Even if know almost nothing about gtk, by using
glade
interface designer and libglade binding, I was able to create quite
sophisticated well looking window with necessary events handled without
writing a line of ruby code. On the other hand, I have not been able to
figure out how to integrate a socket server to Gtk event loop.

To make to long story short, I need help about integration of a socket
server process/thread to Gtk and Tk event loop. How can I hook a
callback to
these event loops so that when a new data is available the callback is
called automatically.

I expect a high volume of network traffic so I need to decouple the gui
event loop and the server process.

Any help much appreciated and thanks in advance.

TaO
Hidetoshi NAGAI (Guest)
on 2006-04-24 14:02
(Received via mailing list)
Maybe, the followings are not answers which you want.

Tcl/Tk libraries with One-Click Ruby Installer 182-15 is
only standard libraries of Tcl/Tk.
Perl/Tk uses the 'ptk' library which includes some mega widgets
from some Tcl/Tk extensions.
Ruby/Tk supports such extensions by libraries on "tkextlib/*".
But, to use such extionstions, those are available on Tcl/Tk.

I recommend you to get ActiveTcl binary package from ActiveState
site (http://www.activestate.com/), and use One-Click Ruby
Installer 184-16 rc1.
ActiveTcl binary package inlcudes much extensions than 'ptk'.
Of course, it includes some extensions with a notebook widget.

# Current latest release of Tcl/Tk is 8.4.13.
# Although I don't test it yet, probably, it will work with Ruby/Tk.
# In the near future, I'll check the changes between 8.4.12 and
# 8.4.13, and new features included in ActiveTcl8.5.0.0 Beta5.
# Before commiting supports for such changes, you may need
# Tk.tk_call() method to use new features.

By the way, I cannot understand the following part of your mail.

From: "Talha O." <removed_email_address@domain.invalid>
Subject: how to integrate ruby tk/gtk event loop with another kind event
loop such as socket server?
Date: Mon, 24 Apr 2006 17:40:02 +0900
Message-ID:
<removed_email_address@domain.invalid>
>                   I have also noticed that some of the other features such
> as binding external events and geometry managers except pack manager that
> are available in perl tk are not available in ruby binding.

I think those are supported on Ruby/Tk.
Please tell me the case which you have troubles.
Did you see the Ruby/Tk examples on Ruby source tree?
Those are also included in One-Click Ruby Installer.

>                                                                  Besides I
> could not find a way to integrate an external event loop or process to Tk
> eventloop. There are some explanations about filevent and tk send interface
> which iseemed not available in windows and in ruby.

Old Ruby/Tk had some troubles when running an eventloop on non-main
thread. But current Ruby/Tk is safe for Ruby's threads.
There is no problem, even if an eventloop works on non-main thread
or the other threads controll the Tk interpreter.
So, it is better to create a thread for I/O operations.
However, on Windows, I heard that there are some I/O operations
blocks thread-switching.

# That is the reason of why the sample 'irbtk.rb' ('wish'-like(?) IRB)
# doesn't work properly on Windows.

Do you say about such case?
Chris A. (Guest)
on 2006-04-24 19:46
(Received via mailing list)
> I expect a high volume of network traffic so I need to decouple the gui
> event loop and the server process.
>
> Any help much appreciated and thanks in advance.

The following is probably insufficient for your high performance
situation, but for more forgiving situations:

Tk.after_idle {...}

is an easy way to have your own code run during the event loop.  If Tk
has nothing to do then it runs your block.  It only does this once,
but:

p = proc {print "Hi!\n";Tk.after_idle(&p)}
Tk.after_idle(p)

--

All the geometry managers from Tk are available in Ruby/Tk.  What
problems are you running into?

obj.pack
obj.place
obj.grid

as a backend.  Similar to how Tk originally used X11 as a backend.

Good luck,
c.
Chris A. (Guest)
on 2006-04-24 19:49
(Received via mailing list)
> as a backend.  Similar to how Tk originally used X11 as a backend.

Woops, sorry about this.  It slipped in from an earlier draft.  Please
ignore.
Hidetoshi NAGAI (Guest)
on 2006-04-24 21:48
(Received via mailing list)
From: "Chris A." <removed_email_address@domain.invalid>
Subject: Re: how to integrate ruby tk/gtk event loop with another kind
event loop such as socket server?
Date: Tue, 25 Apr 2006 00:45:46 +0900
Message-ID: <removed_email_address@domain.invalid>
> p = proc {print "Hi!\n";Tk.after_idle(&p)}
> Tk.after_idle(p)

Or, TkTimer.new(:idle, -1, proc{print "Hi!\n"}).start
Talha O. (Guest)
on 2006-04-25 13:06
(Received via mailing list)
Hi,

Thank you all for all yours posts. I really appreciate. I was wrong
about
geometry managers. I guess the form geometry manager is not in standard
Tk
distribution.

However, I have stopped my parallel development on Tk, and I will try
information you provided some other time for learning purposes. I
continue
my development using Gtk binding. So far, I am quite pleased with the
documentation, tutorials and available GTK tools existing. However my
question is still pending about integration to Gtk main loop.
I had following observations in windows platform about using threads.
In windows platform, threads are not being scheduled against gtk main
loop(
this is also valid for tk main loop as well). When I open a socket and
bind
it in the thread, it sits there. Gtk main loop does not leave control to
the
thread. As fork is not available in windows platforms I have downloaded
the
win32-utils process module and tying to use that but could not be
successful
so far.  Mostly due to my lack of understanding the parallel programming
using processes I guess. I am much accustomed to using threads.

Again thank you all.
Masao M. (Guest)
on 2006-04-25 22:25
(Received via mailing list)
Hi,

On Tue, 25 Apr 2006 18:03:35 +0900
"Talha O." <removed_email_address@domain.invalid> wrote:

> question is still pending about integration to Gtk main loop.
> I had following observations in windows platform about using threads.
> In windows platform, threads are not being scheduled against gtk main loop(
> this is also valid for tk main loop as well). When I open a socket and bind
> it in the thread, it sits there. Gtk main loop does not leave control to the
> thread. As fork is not available in windows platforms I have downloaded the
> win32-utils process module and tying to use that but could not be successful
> so far.  Mostly due to my lack of understanding the parallel programming
> using processes I guess. I am much accustomed to using threads.
>
> Again thank you all.

How about Gtk.idle_add ?

require 'gtk2'

Gtk.init
w = Gtk::Window.new("Test")
w.signal_connect("destroy"){Gtk.main_quit}
w.show_all
Gtk.idle_add do
  p Time.now
  true
end
Gtk.main
This topic is locked and can not be replied to.