# Adding widgets to glade generated interfaces via code

**URL:** <https://www.ruby-forum.com/t/adding-widgets-to-glade-generated-interfaces-via-code/131008>\
**Category:** Ruby-Gnome 2\
**Created:** [March 5, 2008, 5:01am UTC](https://www.ruby-forum.com/t/adding-widgets-to-glade-generated-interfaces-via-code/131008 "2008-03-05T05:01:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [March 5, 2008, 5:01am UTC](https://www.ruby-forum.com/t/adding-widgets-to-glade-generated-interfaces-via-code/131008/1 "2008-03-05T05:01:00Z")

</div>

I have an interface that I created in Glade3. I’ve used  
ruby-glade-create-template to create my initial code file. In the glade  
template I made a horizontal box with one cell which I named gl\_box.

I my code I’ve since created a Gtk::DrawingArea object which I would  
like to pack into the gl\_box. I’ve attached the code. The attempt I made  
is at line 24.

* * *

#!/usr/bin/env ruby

# 

# This file is gererated by ruby-glade-create-template 1.1.4.

# 

require ‘libglade2’  
require ‘drawing\_area’

class Interface02Glade \< Gtk::Window  
include GetText

attr :glade

def initialize(path\_or\_data, root = nil, domain = nil, localedir =  
nil, flag = GladeXML::FILE)  
super(“DRCAM”)  
bindtextdomain(domain, localedir, nil, “UTF-8”)  
@glade = GladeXML.new(path\_or\_data, root, domain, localedir, flag)  
{|handler| method(handler)}  
glconfig = create\_glconfig

```
set_reallocate_redraws(true)
signal_connect("delete_event"){ Gtk.main_quit}

@cnc_drawing_area = CNCDrawingArea.new(glconfig)
@cnc_drawing_area.set_size_request(640,480)
#gl_box.pack_start(@cnc_drawing_area, true, true, 0)

```

end

def create\_glconfig # Gdk::GLConfig.new([screen,]  
attrib\_list/mode)–Returns an OpenGL frame buffer configuration that  
matches the specified configuration.  
# Try double-buffered visual  
glconfig = Gdk::GLConfig.new(Gdk::GLConfig::MODE\_RGB|  
Gdk::GLConfig::MODE\_DEPTH|  
Gdk::GLConfig::MODE\_DOUBLE)

```
unless glconfig
  puts "*** Cannot find the double-buffered visual."
  puts "*** Trying single-buffered visual."

  # Try single-buffered visual
  glconfig = Gdk::GLConfig.new(Gdk::GL::MODE_RGB|
                               Gdk::GL::MODE_DEPTH)
  raise "No appropriate OpenGL-capable visual found." unless

```

glconfig  
end  
glconfig  
end

end

# Main program

if **FILE** == $0

# Set values as your own application.

PROG\_PATH = “interface\_layouts/interface-02.glade”  
PROG\_NAME = “YOUR\_APPLICATION\_NAME”  
Interface02Glade.new(PROG\_PATH, nil, PROG\_NAME)  
Gtk.main  
end

* * *

What is the correct way to reference the widgets made via Glade3? Thanks  
in advance for any help.

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [March 5, 2008, 11:34am UTC](https://www.ruby-forum.com/t/adding-widgets-to-glade-generated-interfaces-via-code/131008/2 "2008-03-05T11:34:11Z")

</div>

Mark Thompson wrote:

> What is the correct way to reference the widgets made via Glade3? Thanks  
> in advance for any help.

Try @glade.get\_widget(“yourwidgetname”)

This will return the GTk widget. Use \<\<, add, pack\_start or pack\_end  
then to add widgets to your container.

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [March 5, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/adding-widgets-to-glade-generated-interfaces-via-code/131008/3 "2008-03-05T16:16:45Z")

</div>

Joachim G. wrote:

> Try @glade.get\_widget(“yourwidgetname”)

## Joachim, thank you for your quick reply. That worked as far as I can tell, but I still can’t see my opengl window. Perhaps I need to set some properties of the window? Here’s how I implemented your suggestion.

```
@cnc_drawing_area = CNCDrawingArea.new(glconfig)
@cnc_drawing_area.set_size_request(640,480)
gl_box = @glade.get_widget("gl_box")
gl_box.pack_start(@cnc_drawing_area, true, true, 0)

```

* * *

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [March 5, 2008, 4:37pm UTC](https://www.ruby-forum.com/t/adding-widgets-to-glade-generated-interfaces-via-code/131008/4 "2008-03-05T16:37:22Z")

</div>

Mark Thompson wrote:

> Joachim G. wrote:
> 
> > Try @glade.get\_widget(“yourwidgetname”)
> 
> ## Joachim, thank you for your quick reply. That worked as far as I can tell, but I still can’t see my opengl window. Perhaps I need to set some properties of the window? Here’s how I implemented your suggestion.
> 
> ```
> @cnc_drawing_area = CNCDrawingArea.new(glconfig)
> @cnc_drawing_area.set_size_request(640,480)
> gl_box = @glade.get_widget("gl_box")
> gl_box.pack_start(@cnc_drawing_area, true, true, 0)
> 
> ```
> 
> * * *

## That was it! .set\_visible(true)

```
@cnc_drawing_area = CNCDrawingArea.new(glconfig)
@cnc_drawing_area.set_size_request(640,480)
@cnc_drawing_area.set_visible(true)
gl_box = @glade.get_widget("gl_box")
gl_box.pack_start(@cnc_drawing_area, true, true, 0)

```

* * *

Many thanks again Joachim!

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [March 5, 2008, 5:48pm UTC](https://www.ruby-forum.com/t/adding-widgets-to-glade-generated-interfaces-via-code/131008/5 "2008-03-05T17:48:13Z")

</div>

I’m dying to know where you got this information, as it could be a great  
resource for me as well…

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [March 5, 2008, 4:59pm UTC](https://www.ruby-forum.com/t/adding-widgets-to-glade-generated-interfaces-via-code/131008/6 "2008-03-05T16:59:33Z")

</div>

Mark Thompson wrote:

> That was it! .set\_visible(true)

Or try show\_all on your top level window

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [March 5, 2008, 5:49pm UTC](https://www.ruby-forum.com/t/adding-widgets-to-glade-generated-interfaces-via-code/131008/7 "2008-03-05T17:49:27Z")

</div>

Mark Thompson wrote:

> I’m dying to know where you got this information, as it could be a great  
> resource for me as well…

Meaning the “@glade.get\_widget(“yourwidgetname”)” thing.

---

<div class="post-metadata">

**Author:** ![markthomas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@markthomas](https://www.ruby-forum.com/u/markthomas)\
**Post date:** [March 5, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/adding-widgets-to-glade-generated-interfaces-via-code/131008/8 "2008-03-05T18:07:09Z")

</div>

Hi,

it’s from the ruby-libglade docs at:

[http://ruby-gnome2.sourceforge.jp/hiki.cgi?GladeXML#get\_widget](http://ruby-gnome2.sourceforge.jp/hiki.cgi?GladeXML#get_widget)

Cheers, detlef

Am Mittwoch, den 05.03.2008, 17:49 +0100 schrieb Mark Thompson:
