Calling class's method when button is pressed

Discussion in 'Ruby' started by Sujeet Kumar, May 29, 2005.

  1. Sujeet Kumar

    Sujeet Kumar Guest

    Hi
    How to call the method of a class when button(TkButton) is pressed.
    For example why the following code generate runtime error
    bgerror failed to handle background error.
    Original error: NoMethodError: undefined method `bpressed' for
    Button:Class
    Error in bgerror: invoked "break" outside of a loop
    when TkButton is pressed.The code is
    require 'tk'
    class Button
    def bpressed
    puts "bpressed"
    end

    root = TkRoot.new(){ title "Buttton"}
    button =TkButton.new(root){
    text "Button1"
    command {Button::bpressed}

    }.pack
    end
    Tk.mainloop

    Anyone can explain the error or any other method to call class's function
    when TkButton is pressed
    Thanks
    sujeet
     
    Sujeet Kumar, May 29, 2005
    #1
    1. Advertising

  2. Sujeet Kumar wrote:
    > Original error: NoMethodError: undefined method `bpressed' for
    > Button:Class
    > Error in bgerror: invoked "break" outside of a loop
    > when TkButton is pressed.The code is
    > require 'tk'
    > class Button
    > def bpressed
    > puts "bpressed"
    > end
    >
    > root = TkRoot.new(){ title "Buttton"}
    > button =TkButton.new(root){
    > text "Button1"
    > command {Button::bpressed}
    >
    > }.pack
    > end
    > Tk.mainloop


    You are defining bpressed as an instance method, not a class method, of
    Button. What you want is this:

    class Button
    def self.bpressed
    puts "bpressed"
    end
    end
     
    Charles Steinman, May 29, 2005
    #2
    1. Advertising

  3. Sujeet Kumar

    Sujeet Kumar Guest

    Thanks for suggestion

    Hi
    Thanks.It works now
    sujeet

    > Sujeet Kumar wrote:
    >> Original error: NoMethodError: undefined method `bpressed' for
    >> Button:Class
    >> Error in bgerror: invoked "break" outside of a loop
    >> when TkButton is pressed.The code is
    >> require 'tk'
    >> class Button
    >> def bpressed
    >> puts "bpressed"
    >> end
    >>
    >> root = TkRoot.new(){ title "Buttton"}
    >> button =TkButton.new(root){
    >> text "Button1"
    >> command {Button::bpressed}
    >>
    >> }.pack
    >> end
    >> Tk.mainloop

    >
    > You are defining bpressed as an instance method, not a class method, of
    > Button. What you want is this:
    >
    > class Button
    > def self.bpressed
    > puts "bpressed"
    > end
    > end
    >
    >
     
    Sujeet Kumar, May 29, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Earl Teigrob
    Replies:
    0
    Views:
    568
    Earl Teigrob
    Dec 15, 2003
  2. cgia
    Replies:
    2
    Views:
    413
    Steve C. Orr [MVP, MCSD]
    Apr 2, 2004
  3. Rune
    Replies:
    6
    Views:
    1,265
    Richie Hindle
    Jan 29, 2004
  4. Chuck
    Replies:
    0
    Views:
    267
    Chuck
    Nov 10, 2003
  5. Replies:
    1
    Views:
    159
Loading...

Share This Page