last day (15 days later) » 

7:40 PM
1
A: How to update Ruby GTK window label in real time and communicate with other threads

cedlemoNo It is a local variable: myglobal = "toto" class Myclass def initialize @myvar = myglobal end def print puts @myvar end end an_instance = Myclass.new an_instance.print throw this error: global_and_class.rb:5:in `initialize': undefined local variable or method `myglobal' f...

 
Thanks cedlemo for the answer. I admit it is a fine idea to pass an reference to a Queue object to the class. Yet doing that I ran into another problem: (( ruby main.rb main.rb:14:in `initialize': not a Gtk::Window::Type: #<Thread::Queue:0x9ea3c84> (TypeError) )) Does this mean the class only accepts the specific type of arguments? How can I pass another type then?
 
For you error try to add parenthesis after super -> super(). Look here :stackoverflow.com/questions/5146974/…
 
Thanks, it worked! Still, how can I update the text automatically (without the button pressing)?
 
Are talking about the super problem or the code/edit that I have added in my first answer?
 
No, your idea of adding paranthesis to the super method did work. The program works as expected - whenever the button is pressed, the label auto updates itself with the text in the queue object. Now in addition I want to implement a method which would automate the process - like autoupdate or something so that the user would not need to refresh the label by clicking the button.
 
7:40 PM
Maybe try to simplify like in the code I have added. Don't use Queu and update directly the label within the timer. Look carefully my example.
 
But how can I make it update it itself every click? I have tried using while loop, but it freezes the GUI. Take a look at the code below - Gtk.main has to be executed, otherwise the GUI does not show up. It is impossible to implement any type of looping in the window class.
Hello, cedlemo!
 
Hello! Try to create a TimerWindow which take a label object as argument instead of a Queu object
 
hmm, is it possible? I mean, the label object is probably available only within the Gtk::Window object
do you think there is a way to implement a method inside the TimerWindow which could autoupdate label? Like eventhandler or something, just every click?
 
No you can create a label object without a window look carefully the code I have added it is what I have done.
 
Sweet, haven't noticed it, trying to implement it :))
thank you!
 
7:48 PM
I have added more code. I haven't tested it but it should works.
 
7:58 PM
q1 = Queue.new
label = Gtk::Label.new("Tomatono")

app = Thread.new {
timer = Timer.new(q1)
timer.launch
}




labelUpdate = Thread.new {
loop {
puts "Update"
label.text = q1.pop.to_s;
sleep(1)
}
}

gui = Thread.new {
win = Gtk::Window.new
win.set_default_size 100, 30
win.add(label)

win.show_all
win.signal_connect("destroy") { labelUpdate.kill;Gtk.main_quit; }
Gtk.main
}

app.join
labelUpdate.join
gui.join
thank you, sir!
I have rewritten the code this way
It definately works as expected!
one question - how does loop know to sleep for 1s even if I remove the sleep line?
 
8:31 PM
I think it is just a coincidence, you could try while 1 instead.
 

last day (15 days later) »