# Get active selection from tk iwidgets radiobox

**URL:** <https://www.ruby-forum.com/t/get-active-selection-from-tk-iwidgets-radiobox/128905>\
**Category:** Ruby\
**Created:** [February 12, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/get-active-selection-from-tk-iwidgets-radiobox/128905 "2008-02-12T15:40:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![unwen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b487fb/32.png) [@unwen](https://www.ruby-forum.com/u/unwen)\
**Post date:** [February 12, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/get-active-selection-from-tk-iwidgets-radiobox/128905/1 "2008-02-12T15:40:37Z")

</div>

Hi there,  
I have created a radiobox with various entries and want to read the  
currently selected entry.  
For some reason I can’t get it to work.  
From what I see “get” would be the appropriate method but it expects  
an argument, which seems odd to me as it should return the active  
entry.

Any help?  
Thanks

Stefan

---

<div class="post-metadata">

**Author:** ![unwen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b487fb/32.png) [@unwen](https://www.ruby-forum.com/u/unwen)\
**Post date:** [February 12, 2008, 6:53pm UTC](https://www.ruby-forum.com/t/get-active-selection-from-tk-iwidgets-radiobox/128905/2 "2008-02-12T18:53:16Z")

</div>

Unwen wrote:

> Hi there,  
> I have created a radiobox with various entries and want to read the  
> currently selected entry.  
> For some reason I can’t get it to work.  
> From what I see “get” would be the appropriate method but it expects  
> an argument, which seems odd to me as it should return the active  
> entry.
> 
> Any help?  
> Thanks
> 
> Stefan

I can’t test this because my tk installation no longer works, but you  
need to:

1. Create a TkVariable, e.g.

val = TkVariable.new

1. Create your radio button with the ‘variable’ option set to the  
TkVariable, e.g.

variable val

1. …and set the ‘value’ option of each radio button to a unique value,  
e.g.

value 1

Thereafter, when a radio button is checked, ruby will automatically  
assign the value attribute of the radio button to the TkVariable. You  
can determine which radio button is checked by checking the value of the  
TKVariable. If you want to programmatically set a radio button to  
checked, then assign the value of the radio button to the TkVariable.

---

<div class="post-metadata">

**Author:** ![unwen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b487fb/32.png) [@unwen](https://www.ruby-forum.com/u/unwen)\
**Post date:** [February 13, 2008, 1:27pm UTC](https://www.ruby-forum.com/t/get-active-selection-from-tk-iwidgets-radiobox/128905/3 "2008-02-13T13:27:01Z")

</div>

> 1. Create a TkVariable, e.g.
> 
> val = TkVariable.new

I found an example using this approach but it only applies to the use of  
individual radiobuttons not the radiobox.  
In tcl the way to get the active button is “pathname get”.  
But the Ruby API expects an argument for “get” which seems to be exactly  
that information I’d like to read.

Does someone have an example of the usage of get with the Iwidgets  
radiobox in Ruby?

Stefan

---

<div class="post-metadata">

**Author:** ![unwen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b487fb/32.png) [@unwen](https://www.ruby-forum.com/u/unwen)\
**Post date:** [February 14, 2008, 3:41am UTC](https://www.ruby-forum.com/t/get-active-selection-from-tk-iwidgets-radiobox/128905/4 "2008-02-14T03:41:48Z")

</div>

From: Unwen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: get active selection from tk iwidgets radiobox  
Date: Tue, 12 Feb 2008 23:39:56 +0900  
Message-ID:  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> I have created a radiobox with various entries and want to read the  
> currently selected entry.  
> For some reason I can’t get it to work.  
> From what I see “get” would be the appropriate method but it expects  
> an argument, which seems odd to me as it should return the active  
> entry.

I’m very sorry. That is a bug.  
Please wait for the patch. I’ll send it in a few days.

# Why does nobody report such serious bug? (;\_😉

# I need bug reports and feature requests to improve Ruby/Tk.

---

<div class="post-metadata">

**Author:** ![unwen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b487fb/32.png) [@unwen](https://www.ruby-forum.com/u/unwen)\
**Post date:** [February 14, 2008, 4:42am UTC](https://www.ruby-forum.com/t/get-active-selection-from-tk-iwidgets-radiobox/128905/5 "2008-02-14T04:42:46Z")

</div>

On Feb 13, 2008, at 9:40 PM, Hidetoshi NAGAI wrote:

> # Why does nobody report such serious bug? (;\_😉
> 
> # I need bug reports and feature requests to improve Ruby/Tk.

I am a regular user of Ruby/Tk, so perhaps I may be permitted to  
comment on this.

Ruby/Tk is a huge, complex library with very little English  
documentation. Neither ri nor the Pickaxe Book, which I rely on when  
I run into problems with Ruby, are of much help with Ruby/Tk beyond  
the most elementary level. That means I must rely on your examples  
posted on [ruby-lang.org](http://ruby-lang.org) and your Ruby ML posts whenever I venture  
into new Ruby/Tk territory. These are very helpful, but by following  
closely in your footsteps, I never go where the bugs live. I only go  
where you have already been and where you have chased the bugs away.  
Therefore, I’m not likely to encounter bugs. But there is another  
story. Suppose I do try something not covered by your examples and it  
doesn’t work: I don’t think I found a bug. I merely think I don’t  
understand Ruby/Tk or the underlying Tk/Tcl (another huge and complex  
library) well enough.

As for new features, there are already so many features I have yet to  
master that it would seem the utmost arrogance for me to request  
anything beyond what is already there.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![unwen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b487fb/32.png) [@unwen](https://www.ruby-forum.com/u/unwen)\
**Post date:** [February 14, 2008, 10:46am UTC](https://www.ruby-forum.com/t/get-active-selection-from-tk-iwidgets-radiobox/128905/6 "2008-02-14T10:46:51Z")

</div>

> I’m very sorry. That is a bug.  
> Please wait for the patch. I’ll send it in a few days.
> 
> # Why does nobody report such serious bug? (;\_😉
> 
> # I need bug reports and feature requests to improve Ruby/Tk.

Thanks for the info!

Wow, a bug was the last thing I was thinking about 🙂  
I’m a newbie when it comes to Ruby but I have some experience with  
tcl/tk which is one reason why I want to use Ruby Tk for GUI stuff.

With regard to the feature request question I can only confirm what  
Morton said, first I need to wade through the features that are already  
there. Maybe there will be some input when I increase my usage of Ruby  
Tk.

Stefan

---

<div class="post-metadata">

**Author:** ![unwen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b487fb/32.png) [@unwen](https://www.ruby-forum.com/u/unwen)\
**Post date:** [February 18, 2008, 4:40am UTC](https://www.ruby-forum.com/t/get-active-selection-from-tk-iwidgets-radiobox/128905/7 "2008-02-18T04:40:41Z")

</div>

From: Hidetoshi NAGAI [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: get active selection from tk iwidgets radiobox  
Date: Thu, 14 Feb 2008 11:40:38 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> I’m very sorry. That is a bug.  
> Please wait for the patch. I’ll send it in a few days.

Sorry. I came down with a cold the last weekend,  
and my physical strength has not recovered yet.  
Please give me more two or three days.

---

<div class="post-metadata">

**Author:** ![unwen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b487fb/32.png) [@unwen](https://www.ruby-forum.com/u/unwen)\
**Post date:** [February 21, 2008, 3:20pm UTC](https://www.ruby-forum.com/t/get-active-selection-from-tk-iwidgets-radiobox/128905/8 "2008-02-21T15:20:07Z")

</div>

Hi,  
Patch works like a charm.  
Thanks!

Cheers,  
Stefan

---

<div class="post-metadata">

**Author:** ![unwen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/b487fb/32.png) [@unwen](https://www.ruby-forum.com/u/unwen)\
**Post date:** [February 20, 2008, 7:20pm UTC](https://www.ruby-forum.com/t/get-active-selection-from-tk-iwidgets-radiobox/128905/9 "2008-02-20T19:20:22Z")

</div>

From: Hidetoshi NAGAI [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: Re: get active selection from tk iwidgets radiobox  
Date: Mon, 18 Feb 2008 12:39:53 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Please give me more two or three days.

Sorry, too late. The following is the patch.

‘Tk::Iwidgets::Radiobox#get\_tag’ (and 'Radiobox#get) returns  
the tag name of the selected button.  
‘Tk::Iwidgets::Radiobox#get\_obj’ returns the component object  
which denotes the selected button.  
You can get the tag name of the button component object  
by ‘name’ method of the object.

The patch includes bug fix of Tk::Iwidgets::Checkbox too.  
‘Tk::Iwidgets::Checkbox#get\_tags’ returns tag name list of  
selected buttons, and ‘Tk::Iwidgets::Checkbox#get\_objs’  
returns component list of selected buttons.  
‘Tk::Iwidgets::Checkbox#get(idx)’ returns status (boolean  
value) of the checkbutton ‘idx’.  
And ‘Tk::Iwidgets::Checkbox#get’ (with no argument) returns  
the same result of ‘Tk::Iwidgets::Checkbox#get\_tags’  
(it follows the feature of the Tcl/Tk function).

# Index: ext/tk/lib/tkextlib/iwidgets/radiobox.rb

— ext/tk/lib/tkextlib/iwidgets/radiobox.rb (revision 15558)  
+++ ext/tk/lib/tkextlib/iwidgets/radiobox.rb (working copy)  
@@ -85,12 +85,15 @@  
self  
end

- def get(idx)
- simplelist(tk\_call(@path, ‘get’, index(idx))).collect{|id|
- 

```
 Tk::Itk::Component.id2obj(self, id)

```

- }

- def get\_tag

- ((tag = tk\_call\_without\_enc(@path, ‘get’)).empty?)? nil: tag  
end

- alias get get\_tag

- def get\_obj

- (tag = get\_tag)? Tk::Itk::Component.id2obj(self, tag): nil

- end

- def index(idx)  
number(tk\_call(@path, ‘index’, tagid(idx)))  
end  
Index: ext/tk/lib/tkextlib/iwidgets/checkbox.rb  
===================================================================  
— ext/tk/lib/tkextlib/iwidgets/checkbox.rb (revision 15558)  
+++ ext/tk/lib/tkextlib/iwidgets/checkbox.rb (working copy)  
@@ -85,12 +85,24 @@  
self  
end

- def get(idx)
- simplelist(tk\_call(@path, ‘get’, index(idx))).collect{|id|

- def get\_tags

- simplelist(tk\_call\_without\_enc(@path, ‘get’))

- end

- 
- def get\_objs

- simplelist(tk\_call\_without\_enc(@path, ‘get’)).collect{|id|  
Tk::Itk::Component.id2obj(self, id)  
}  
end

- def get(idx=nil)

- if idx

- 

```
 bool(tk_call_without_enc(@path, 'get', index(idx)))

```

- else

- 

```
 get_tags

```

- end

- end

- def index(idx)  
number(tk\_call(@path, ‘index’, tagid(idx)))  
end  
Index: ext/tk/lib/tkextlib/itk/incr\_tk.rb  
===================================================================  
— ext/tk/lib/tkextlib/itk/incr\_tk.rb (revision 15558)  
+++ ext/tk/lib/tkextlib/itk/incr\_tk.rb (working copy)  
@@ -156,7 +156,7 @@  
master = master.to\_s  
end  
return id unless ComponentID\_TBL.key?(master)

- 

```
   (ComponentID_TBL.key?(id))? ComponentID_TBL[master][id]: id

```

- 

```
   (ComponentID_TBL[master].key?(id))? 

```

ComponentID\_TBL[master][id]: id  
end

```
   def self.new(master, component=nil)

```
