On Mar 6, 2008, at 4:44 PM, Isaac Toothyxdip wrote:

> words = ['-', 'cm', 'mm', 'ml']
> @var = TkVariable.new()
> minuteOptionMenu = TkOptionMenubutton.new(
>     detailViewFrm, @var, *words) {
>       width  1
>     }.grid('column'=>1, 'row'=>0, 'sticky'=>'w', 'padx'=>5)
>
> I was wondering something...what is the way that you can check what  
> is chosen from the menu

I have two suggestions.

1. Google on 'tkoptionmenubutton'. You will find many interesting and  
useful URLs.

2. Read and, better yet, run and experiment with the following example.

<code>
#! /usr/bin/ruby -w

require 'tk'

URAND_ARG_ERR = "Arguments not valid for urand"

# Return a pseudo-random number in the range 0.0...1.0, 0...m, or m..n.
def urand(m=0, n=nil)
    case m
    when Range
       m, n = m.begin, m.end
    when Integer
       return rand(m) if n.nil?
    else
       raise ArgumentError, URAND_ARG_ERR
    end
    raise ArgumentError, URAND_ARG_ERR if n < m
    m + rand(n - m + 1)
end

root = Tk.root
root.title('Ruby/Tk Canvas Demo')

opt_choices = ["Square", "Circle", "Scribble", "Geometry"]
opt_chosen = TkVariable.new
opt_btn = TkOptionMenubutton.new(root, opt_chosen, *opt_choices)
opt_btn.pack('pady'=>10)

canvas = TkCanvas.new(root) {
    relief 'solid'
    borderwidth 1
}
canvas.instance_variable_set(:@shape, opt_chosen)
canvas.pack('fill'=>'both', 'expand'=>true, 'padx'=>20, 'pady'=>10)

def canvas.draw(e)
    # Argument e is the Button-1 click event.
    focus
    case @shape.value
    when "Square"
       ds = 50
       top_lf = e.x - ds, e.y - ds
       btm_rt = e.x + ds, e.y + ds
       TkcRectangle.new(self, top_lf, btm_rt) { width 2 }
    when "Circle"
       r = 50
       top_lf = e.x - r, e.y - r
       btm_rt = e.x + r, e.y + r
       TkcOval.new(self, top_lf, btm_rt) { width 2 }
    when "Scribble"
       # Closed curve statistically centered at the cursor.
       dx, dy = 100, 100
       pts = []
       5.times { pts << [e.x + urand(-dx, dx), e.y + urand(-dy, dy)] }
       pts << pts.first
       TkcLine.new(self, pts) {
          width 2
          smooth true
       }
    when "Geometry"
       # Report the geometry of the window.
       # Text is drawn centered at the cursor.
       TkcText.new(self, e.x, e.y) { text "#{Tk.root.geometry}" }
    end
end

def canvas.clear
    delete("all")
end

# Draw a shape by clicking the left mouse button in the canvas.
canvas.bind('Button-1') { |e| canvas.draw(e) }
# Canvas can be cleared by pressing forward or backward delete key.
ve = TkVirtualEvent.new
ve.add('Key-Delete', 'Key-BackSpace')
canvas.bind(ve) { canvas.clear }
root.bind('Command-q') { root.destroy }

min_w, min_h = 300, 300
root.minsize(min_w, min_h)
root_x = (root.winfo_screenwidth - min_w) / 2
root.geometry("#{min_w}x#{min_h}+#{root_x}+50")

Tk.mainloop
</code>

Regards, Morton