[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Reading pick from option menu

Isaac

3/6/2008 9:44:00 PM

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
ive tried checking what words was and i also checked what @var was but i
dont think i did it the right way

there were a few other ways i tried like setting commands but i dont
know where it is i think it is on my usb key at my friends house so i
will try that later if know one knows how.

Thanks in advance!
--
Posted via http://www.ruby-....

9 Answers

Isaac

3/7/2008 2:27:00 AM

0

Well i thought im going to do this on my own because ill never learn if
i dont try it on my own (more then 30min)

but i just want to ask something simple that im having a problem with.

when i make a frame and i make lets say a button TkButton.new(frame) {}
when i try to center the text horizontally using justify (i tried
anchor) center it wont work. I tried grid, pack, putting it in the {} it
just refuses to center. So to kinda fix it i used column to get close to
the center but that doesnt work to well. I even tried to put center as
the column but i didnt expect that work.

I mean its fine until i get to the finishing steps of the program but i
just was wondering
--
Posted via http://www.ruby-....

Isaac

3/7/2008 3:19:00 AM

0

Yeah i figured it out i have to set words as @words and then set
@words = @var
--
Posted via http://www.ruby-....

Morton Goldberg

3/7/2008 5:00:00 AM

0

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



Isaac

3/10/2008 1:28:00 AM

0

Wow this is VERY helpful and im going look over and run it (it looks
like it has a menu button and then a box where you can draw a square,
oval, a random scribble, and text.) But i have a question why can you
just do a when for the opt_chosen variable to check it. I tried this and
it didnt work.

Also is the instance_variable_set saying that @shape is the variable and
that opt_chosen is the value? Can instance_variable_set be used on
pretty much any tkclass?
and for me doing this would i have to do the same method or something
like with a input box or just simply a TkVariable?

--
Posted via http://www.ruby-....

Morton Goldberg

3/10/2008 4:28:00 AM

0

On Mar 9, 2008, at 9:28 PM, Isaac Toothyxdip wrote:

> Wow this is VERY helpful and im going look over and run it (it looks
> like it has a menu button and then a box where you can draw a square,
> oval, a random scribble, and text.) But i have a question why can you
> just do a when for the opt_chosen variable to check it. I tried
> this and
> it didnt work.

The opt_chosen local variable isn't in the right scope. That's why I
do the instance_variable_set. It makes the TkVariable object referred
to by opt_chosen available in a scope visible to the canvas.draw
singleton method.

> Also is the instance_variable_set saying that @shape is the
> variable and
> that opt_chosen is the value?

The instance variable @shape gets bound to the object which was
originally bound to the local variable opt_chosen. After the call to
instance_variable_set, both variables refer to the same TkVariable
object. In Ruby it is important to be aware of the difference between
variables and the objects they refer to.

> Can instance_variable_set be used on pretty much any tkclass?

The instance_variable_set method is a method of Object and is,
therefore, inherited by all classes. But be careful with this method
-- thoughtless use of it can cause real problems with your code. In
my canvas example the use is safe because the instance variable is
only added to one particular instance of TkCanvas so it can used in a
method that is only available to that one particular instance.

> and for me doing this would i have to do the same method or something
> like with a input box or just simply a TkVariable?

I'm not sure I understand this question, but if you're asking is this
the 'right' way to make objects visible inside methods defined for
Ruby/Tk widgets, the answer is: There are many ways to solve scope
and visibility problems. This is only one of them.

Regards, Morton

Isaac

3/10/2008 4:31:00 PM

0

Thanks.

My question was are you able to do this with a entry box or even just a
TkVariable instead of a canvas?
I've been trying to intertwine your method with what im trying to do and
have had no luck. Im going to keep trying and do some more searching.

(What im doing is i have 2 entry boxes, 1 button, and 1 optionmenubutton
and in the top entry you type a number and then you pick an option from
the menu then you press the button and it multiplies the number you
inputed by a variable and the variable changes with the selection of the
menu)

--
Posted via http://www.ruby-....

Isaac

3/10/2008 4:35:00 PM

0

Isaac Toothyxdip wrote:
> Thanks.
>
> My question was are you able to do this with a entry box or even just a
> TkVariable instead of a canvas?
> I've been trying to intertwine your method with what im trying to do and
> have had no luck. Im going to keep trying and do some more searching.
>
> (What im doing is i have 2 entry boxes, 1 button, and 1 optionmenubutton
> and in the top entry you type a number and then you pick an option from
> the menu then you press the button and it multiplies the number you
> inputed by a variable and the variable changes with the selection of the
> menu)

WOO got it to work i just completely started over and this is what my
code is now:

<code>
require 'tk'

root = Tk.root
root.title('Hello')
@entry = TkVariable.new
@menu_chosen = TkVariable.new

menu_choices = ["Yes", "No"]
menu_chosen = TkVariable.new
menu = TkOptionMenubutton.new(root, menu_chosen, *menu_choices)
menu.pack('pady'=>10)
@number = menu_chosen

entry = TkEntry.new(root)
entry.pack('pady'=>10)
entry.textvariable = @entry

def multiply
case @number.value
when "Yes"
@menu_chosen = 50
when "No"
@menu_chosen = 30
end
@entry.numeric *= @menu_chosen
end

button = TkButton.new(root)
button.text = "Hello"
button.pack('pady'=>10)
button.command { multiply }

#geometry
min_w, min_h = 200, 200
root.resizable(flase,false)
root_x = (root.winfo_screenwidth - min_w) / 2
root.geometry("#{min_w}x#{min_h}+#{root_x}")

Tk.mainloop
</code>

I tried something like this before but i dont think i had .value so it
didnt work.

Thanks for your help.
--
Posted via http://www.ruby-....

Morton Goldberg

3/11/2008 1:14:00 AM

0

On Mar 10, 2008, at 12:34 PM, Isaac Toothyxdip wrote:

> WOO got it to work i just completely started over and this is what my
> code is now:
>
> <code>
> require 'tk'
>
> root = Tk.root
> root.title('Hello')
> @entry = TkVariable.new
> @menu_chosen = TkVariable.new
>
> menu_choices = ["Yes", "No"]
> menu_chosen = TkVariable.new
> menu = TkOptionMenubutton.new(root, menu_chosen, *menu_choices)
> menu.pack('pady'=>10)
> @number = menu_chosen
>
> entry = TkEntry.new(root)
> entry.pack('pady'=>10)
> entry.textvariable = @entry
>
> def multiply
> case @number.value
> when "Yes"
> @menu_chosen = 50
> when "No"
> @menu_chosen = 30
> end
> @entry.numeric *= @menu_chosen
> end
>
> button = TkButton.new(root)
> button.text = "Hello"
> button.pack('pady'=>10)
> button.command { multiply }
>
> #geometry
> min_w, min_h = 200, 200
> root.resizable(flase,false)
> root_x = (root.winfo_screenwidth - min_w) / 2
> root.geometry("#{min_w}x#{min_h}+#{root_x}")
>
> Tk.mainloop
> </code>

Your clearly making progress, but it's clear from this code that you
don't yet fully understand Ruby variables because you are defining
unnecessary ones. Compare the following simplified code to what you
posted.

<code>
require 'tk'

root = Tk.root
root.title('Hello')

menu_choices = ["Yes", "No"]
@menu_chosen = TkVariable.new
menu = TkOptionMenubutton.new(root, @menu_chosen, *menu_choices)
menu.pack('pady'=>10)

entry = TkEntry.new(root)
entry.pack('pady'=>10)
@entry = TkVariable.new
entry.textvariable = @entry

def multiply
case @menu_chosen.value
when "Yes"
num = 50
when "No"
num = 30
end
@entry.numeric *= num
end

button = TkButton.new(root)
button.text = "Hello"
button.pack('pady'=>10)
button.command { multiply }

min_w, min_h = 200, 150
root.resizable(false, false)
root_x = (root.winfo_screenwidth - min_w) / 2
root.geometry("#{min_w}x#{min_h}+#{root_x}+50")

Tk.mainloop
</code>

You had to invent @number because your multiply method broke the
binding between @menu_chosen and the TkVariable it was originally
bound to. But there was no reason to do that. You should have used a
local variable in multiply as I did.

Also, I am puzzled by use of "Yes" and "No" to represent the numbers
50 and 30, respectively, in your option menu. It's your program and
you're entitled to do as you please, but it could be made simpler if
you just used numbers. As in the following.

<code>
require 'tk'

root = Tk.root
root.title('Hello')

menu_choices = [50, 30]
@menu_chosen = TkVariable.new
menu = TkOptionMenubutton.new(root, @menu_chosen, *menu_choices)
menu.pack('pady'=>10)

entry = TkEntry.new(root)
entry.pack('pady'=>10)
@entry = TkVariable.new
entry.textvariable = @entry

def multiply
@entry.numeric *= @menu_chosen.numeric
end

button = TkButton.new(root)
button.text = "Hello"
button.pack('pady'=>10)
button.command { multiply }

min_w, min_h = 200, 150
root.resizable(false, false)
root_x = (root.winfo_screenwidth - min_w) / 2
root.geometry("#{min_w}x#{min_h}+#{root_x}+50")

Tk.mainloop
</code>

Regards, Morton



Isaac

3/15/2008 4:56:00 AM

0

Morton Goldberg wrote:
> Also, I am puzzled by use of "Yes" and "No" to represent the numbers

Well there isnt really a reason i just put to random words. could have
been bobo and jojo but i just put Yes and No. The reason being i didnt
use just simply numbers in the way that you did is because in my main
program it would text.

Thanks for pointing out the unnecessary variables ill study up on those.


--
Posted via http://www.ruby-....