From: J J Date: 2001-05-04T08:11:23+09:00 Subject: [ruby-talk:14599] OOP-ier Tk? Using Tk with Ruby (or any other language) has always seemed a little convoluted. Ruby's scoping for blocks makes this even more so, since the blocks passed to command are scoped when the command is set, not when it's running. This leads to: p = proc { showPig } ... command p Of course you have no idea what a p is, so you have to search back through the source to find it. I've been toying with classes that allow me to do the following: require 'FlatButton' require 'CheckButton' class OOPTkTest def initialize packOpt = { 'padx' => 10, 'pady' => 10 } @root = TkRoot.new { title "OOP Tk Example" } @frame = TkFrame.new(@root) { relief "groove"; borderwidth "2" pack('fill'=>'both', 'side'=>'top') } @label = TkLabel.new(@frame) { text "Click the button to exit" pack packOpt } @button = FlatButton.new(@frame) { pack packOpt } @button.onClick{ if @checkBtn.isChecked exit end } @checkLbl = TkLabel.new(@frame) { text "check" } @checkLbl.pack packOpt @checkBtn = CheckButton.new(@frame) { text "Exit on Button Click" pack packOpt } @checkBtn.onCheck { @checkLbl.configure('text'=>'will exit') } @checkBtn.onUncheck { @checkLbl.configure('text'=>"won't exit") } end end OOPTkTest.new Tk.mainloop As you can see, I have defined methods like onClick for Buttons, onCheck and onUncheck for CheckButtons, etc. Not demonstrated here are methods such as onEnter for FlatButton which allow you to do something when the mouse pointer enters the button. I think it's a whole heck of a lot more intuitive. Notice the @checkBtn.onUncheck for instance. The block it executes is defined where it is used. I plan to pursue this further, what do you folks think? Regards, JJ -- Be Kind, Be Careful, Be Yourself -- Solid, reliable software at reasonable prices http://www.johnjohnsonsoftware.com