From: Hidetoshi NAGAI Date: 2004-11-23T08:28:03+09:00 Subject: Re: Tk widget fixed width Hi, From: "R. Mark Volkmann" Subject: Tk widget fixed width Date: Tue, 23 Nov 2004 01:51:32 +0900 Message-ID: <1101140800.41a21340eaea5@mail.ociweb.com> > How can I request that a Tk widget have a fixed width? Please give the width to the TkOptionMenubutton object. Maybe you want to give the anchor option to it too. For example, ------------------------------------------------------------------- require 'tk' mb = TkOptionMenubutton.new(TkVariable.new, 1, 123, 12345, 1234567, 123456789) mb.width 5 mb.anchor :w mb.pack TkButton.new(:text=>'show', :command=>proc{p mb.value}).pack(:fill=>:x) Tk.mainloop ------------------------------------------------------------------- If you patch tk/menu.rb (see the tail of this mail; I committed it to CVS), you can give the width option when creating the widget. For example, ------------------------------------------------------------------- require 'tk' mb = TkOptionMenubutton.new(1, 123, 12345, 1234567, 123456789, :width=>5, :anchor=>:w) # Or, # mb = TkOptionMenubutton.new(:values=>[1, 123, 12345, 1234567, 123456789], # :width=>5, :anchor=>:w) mb.pack TkButton.new(:text=>'show', :command=>proc{p mb.value}).pack(:fill=>:x) TkButton.new(:text=>'set 123', :command=>proc{mb.value = 123}).pack(:fill=>:x) TkButton.new(:text=>'set 999', :command=>proc{mb.value = 999}).pack(:fill=>:x) Tk.mainloop ------------------------------------------------------------------- -- Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp) --- menu.rb.orig 2004-10-11 10:45:47.000000000 +0900 +++ menu.rb 2004-11-23 07:57:22.000000000 +0900 @@ -402,33 +402,56 @@ end end - def initialize(parent=nil, var=nil, firstval=nil, *vals) - if parent.kind_of? Hash - keys = _symbolkey2str(parent) - parent = keys['parent'] - var = keys['variable'] if keys['variable'] - firstval, *vals = keys['values'] + def initialize(*args) + # args :: [parent,] [var,] [value[, ...],] [keys] + # parent --> TkWindow or nil + # var --> TkVariable or nil + # keys --> Hash + # keys[:parent] or keys['parent'] --> parent + # keys[:variable] or keys['variable'] --> var + # keys[:values] or keys['values'] --> value, ... + # other Hash keys are menubutton options + keys = {} + keys = args.pop if args[-1].kind_of?(Hash) + keys = _symbolkey2str(keys) + + parent = nil + if args[0].kind_of?(TkWindow) || args[0] == nil + parent = args.shift + else + parent = keys.delete('parent') + end + + @variable = nil + if args[0].kind_of?(TkVariable) || args[0] == nil + @variable = args.shift + else + @variable = keys.delete('variable') end - if parent.kind_of? TkVariable - vals.unshift(firstval) if firstval - firstval = var - var = parent - parent = nil + @variable = TkVariable.new unless @variable + + (args = keys.delete('values') || []) if args.empty? + if args.empty? + args << @variable.value + else + @variable.value = args[0] end - var = TkVariable.new unless var - fail 'variable option must be TkVariable' unless var.kind_of? TkVariable - @variable = var - firstval = @variable.value unless firstval - @variable.value = firstval + install_win(if parent then parent.path end) - @menu = OptionMenu.new(tk_call('tk_optionMenu', @path, @variable.id, - firstval, *vals)) + @menu = OptionMenu.new(tk_call('tk_optionMenu', + @path, @variable.id, *args)) + + configure(keys) if keys end def value @variable.value end + def value=(val) + @variable.value = val + end + def activate(index) @menu.activate(index) self