Defining a Method

D

Dave Mccormick

Hi,
I am new to Ruby from basic. I am having a hard time getting my head
around the "def".

I have an Entry widget for a number, a label widget for the answer,
button widget to multiply entry by 2 and place in Label.

I know this should/is simple, but cannot seem to get started.

Here is the messed up code that I have, would someone tell me what to
do?

Thank you,
Dave


#!/usr/bin/env ruby
require 'gtk2'

window =Gtk::Window.new
window.signal_connect("delete_event") do
Gtk.main_quit
false
end


box1 = Gtk::HBox.new(false, 0)
window.add(box1)

buttonE = Gtk::Button.new("Exit")
buttonE.signal_connect("clicked")do
Gtk.main_quit
false
end

box1.pack_start(buttonE, false, false, 10)


buttonC = Gtk::Button.new("Click")
buttonC.signal_connect("clicked") {
label.text=(entry.text.to_f * 2).to_s
}


box1.pack_start(buttonC, true, true, 10)

label = Gtk::Label.new("LABEL")
box1.pack_start(label, true, true, 10)

entry = Gtk::Entry.new
entry.set_max_length( 50 )
box1.pack_start(entry, true, true, 10)



window.border_width = 100
window.show_all
Gtk.main
 
R

Rick DeNatale

Hi,
I am new to Ruby from basic. I am having a hard time getting my head
around the "def".

I have an Entry widget for a number, a label widget for the answer,
button widget to multiply entry by 2 and place in Label.

I know this should/is simple, but cannot seem to get started.

Here is the messed up code that I have, would someone tell me what to
do?

Thank you,
Dave


#!/usr/bin/env ruby
require 'gtk2'

window =Gtk::Window.new
window.signal_connect("delete_event") do
Gtk.main_quit
false
end


box1 = Gtk::HBox.new(false, 0)
window.add(box1)

buttonE = Gtk::Button.new("Exit")
buttonE.signal_connect("clicked")do
Gtk.main_quit
false
end

box1.pack_start(buttonE, false, false, 10)


buttonC = Gtk::Button.new("Click")
buttonC.signal_connect("clicked") {
label.text=(entry.text.to_f * 2).to_s
}


box1.pack_start(buttonC, true, true, 10)

label = Gtk::Label.new("LABEL")
box1.pack_start(label, true, true, 10)

entry = Gtk::Entry.new
entry.set_max_length( 50 )
box1.pack_start(entry, true, true, 10)



window.border_width = 100
window.show_all
Gtk.main

I suspect that the first problem is that a the point you invoke:

buttonC.signal_connect("clicked") {
label.text=(entry.text.to_f * 2).to_s
}

the variable label has not yet been defined in the outer scope.

Try moving this until someplace after you initialize the label variable
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,276
Latest member
Sawatmakal

Latest Threads

Top