From: Hidetoshi NAGAI Date: 2010-12-28T10:34:25+09:00 Subject: Re: Problems w/ Tk::Tile::Label.new scope? From: "Joseph D." Subject: Problems w/ Tk::Tile::Label.new scope? Date: Tue, 28 Dec 2010 04:49:32 +0900 Message-ID: <0e3c6a7e26bda6d69a56d6ed8fa0a366@ruby-forum.com> > non-working: > @button = Tk::Tile::Label.new($content){image @image}.grid(:column => > @position, :row => 1, :sticky =>'we') A block given to "new" method of TkWindow class (and sub-class of TkWindow) is evaluated by "instance_eval". So, at internal of the block, "self" denotes the created widget object. Your "{image @image}" means "{self.image(@image)}" and the "self" is the button widget (@button). @image is not a instance variable of the button widget. It is the reason of why your script doesn't work. Your trouble depends on the scope of variables. So, -------------------------------------------------------------- Ans 1: give a hash of widget options to "new" method. @button = Tk::Tile::Label.new($content, :image => @image, ...).grid(:column => @position, :row => 1, :sticky =>'we') Ans 2: use local variables (not equal to names of wiget options) img = @image @button = Tk::Tile::Label.new($content){image img; ...}.grid(:column => @position, :row => 1, :sticky =>'we') -------------------------------------------------------------- > working method 1: > @button = Tk::Tile::Label.new($content).grid(:column => @position, :row > => 1, :sticky =>'we') > @button['image'] = @image or "@button.image @image" or "@button.image = @image" or "@button.configure(:image => @image, ... )". Methods for widget options aren't shown by Object#methods method. "method_missing" method is used to implement those method. If you want to get the list of supported widget options, Please use "@button.current_configinfo.keys" or "@button.configinfo.collect{|optinfo| optinfo[0]}". > working method 2: > @button = Tk::Tile::Label.new($content){image @@image}.grid(:column => > @position, :row => 1, :sticky =>'we') (snip) > Using a class variable of the class, method 2, worked, but seemed like a > poor coding practice to me. (I am trying to learn good ways not bad > ones) In this case, you use a class variable as a global variable. It is a wrong solution. -- Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp) Department of Artificial Intelligence, Kyushu Institute of Technology