From: Morton Goldberg Date: 2008-03-11T10:13:30+09:00 Subject: Re: Reading pick from option menu 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: > > > 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 > 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. 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 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. 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 Regards, Morton