Calling class's method when button is pressed

S

Sujeet Kumar

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
 
C

Charles Steinman

Sujeet said:
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
 
S

Sujeet Kumar

Hi
Thanks.It works now
sujeet
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top