I am trying to understand building an app using Glade and Ruby. I have
a
test app that has a top-level window with a fixed container. The
container
has three buttons on it. I would like to know how to reference those
buttons in Ruby code to do simple things like disable or enable them
using
the sensitive attribute.
During the app startup I am keeping a reference to the window object via
a
builder using get_object
window = builder.get_object("my_window")
# the following code works and disables all of the buttons
window.children.first.children.each do |obj|
obj.sensitive = false
end
However, I was looking for a way to reference the button object
directly.
window.fixed_container.button_quit.sensitive = false
or at least something along those lines. Do I have to use the builder
instance to get a reference to one of the buttons? I'm looking for the
preferred way to accomplish this task.
Thanks!!
on 2012-03-05 21:24
on 2012-03-05 21:32
Hi, There are many open-source Ruby Gtk+ apps for you to browse the source code. http://launchpad.net/studly (flashcard trainer) http://launchpad.net/screenruler (on-screen ruler) http://launchpad.net/luz (motion graphics editor) > However, I was looking for a way to reference the button object directly. > > window.fixed_container.button_quit.sensitive = false You can achieve this by mixing Ruby's attr_accessor feature with the array of widgets symbols in any of those above apps. See glade_window.rb in Studly for example. Though I recommend you don't do this, and instead do window.set_some_state and let that method configure the window. -Ian
on 2012-03-06 00:01
Hi:
Ian's code is a great resource. He's helped me learn many things in
ruby.
I'll give you a few basics:
> window = builder.get_object("my_window")
This can be abbreviated to this:
window = builder["my_window"]
and to set the title, for example, you could just use:
builder["my_window"].title = "Title appears at top of window"
or
builder["button1"].sensitive = false
Normally, you'd want "builder" to be an instance variable @builder
instead, so you can use it everywhere in your class. I've written an
IDE for ruby that uses that exact variable name, @builder to do exactly
what you're trying to do. You can see simple examples at
visualruby.net.
Good luck,
Eric
on 2012-03-06 00:32
Thanks guys!! I picked up some very good ideas... On Mon, Mar 5, 2012 at 5:01 PM, Eric C.
on 2012-03-06 02:38
hi W.W. - > ... I was looking for a way to reference the button object > directly. > > window.fixed_container.button_quit.sensitive = false i don't use glade, and the builder suggestions above are great - but couldn't you just use an instance variable to reference the buttons? i often do something like this: require 'gtk2' class Nothing def initialize win = Gtk::Window.new win.signal_connect("destroy"){Gtk.main_quit} box = Gtk::HBox.new(true, 2) button1 = Gtk::Button.new("click me and see") button1.signal_connect("clicked"){button1_click_event} @button2 = Gtk::Button.new("I'm just a pawn in this game") @button2.signal_connect("clicked"){p "I'm button2, and I'm feeling sensitive"} box.pack_start(button1, true, true, 2) box.pack_start(@button2, true, true, 2) win.add(box) win.show_all @switch_button2 = false end def button1_click_event p "I'm the master of this game!" @button2.sensitive = @switch_button2 @switch_button2 = !@switch_button2 end end n = Nothing.new Gtk.main - j
on 2012-03-06 06:10
Hi Jake:
I'd encourage you to take a look at a program that I just made
available,
visualruby. It can really streamline your code. For example all the
code that you wrote above could be reduced to this:
class Nothing
def initialize
@button1 = "click me and see"
@button2 = "I'm just a pawn in this game"
load_glade(__FILE__)
set_glade_variables
@switch_button2 = false
show_window()
end
def button2__clicked
p "I'm button2, and I'm feeling sensitive"
end
def button1__clicked
p "I'm the master of this game!"
@builder["button2"].sensitive = !@switch_button2
@switch_button2 = !@switch_button2
end
end
And if you set the values of button1 and button2 in glade, and fixed
button1_clicked:
class Nothing
include GLadeGUI
def initialize
load_glade(__FILE__)
show_window()
end
def button2__clicked
p "I'm button2, and I'm feeling sensitive"
end
def button1__clicked
p "I'm the master of this game!"
@builder["button2"].sensitive = !@builder["button2"].sensitive
end
end
I know that your code works, but using visualruby and glade will
unclutter it.
take a look at visualruby.net
Best,
Eric
on 2012-03-06 12:16
hi Eric, > > take a look at visualruby.net > i will, looks interesting - definitely less clutter in the code... - j
Please log in before posting. Registration is free and takes only a minute.
Existing account
(Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
Log in with Google account | Log in with Yahoo account
No account? Register here.