如何从Gtk :: Entry小部件Ruby获取文本

时间:2016-11-21 04:01:26

标签: ruby methods gtk gtk2 ruby-gnome2

我已经开始在Ruby学习Gtk的绳索了,我已经构建了这个非常简单的程序:

require 'gtk2'
include Gtk

Gtk.init

window = Window.new
window.show
window.set_title "// Edit //"
window.signal_connect "destroy" do 
    Gtk.main_quit
end

box = VBox.new 10 

text = Entry.new
text.set_text "Enter your name"
box.pack_start text

button = Button.new "Go!"
button.signal_connect "clicked" do 
    puts "Hello "+text.get_text()+"!"
end
box.pack_start button

window.add box
window.show_all

Gtk.main

但是当我按下button时,程序输出一个错误说明:

test.rb:21:in `block in <main>': undefined method `get_text' for # <Gtk::Entry:0x20be2d8 ptr=0x13831d0>
Did you mean?  set_text
     from test.rb:28:in `main'
     from test.rb:28:in `<main>'

在这种情况下,如何从GTK中的Entry小部件中检索文本?提前谢谢。

1 个答案:

答案 0 :(得分:2)

您需要的方法名称为text。例如,

my_entry = Entry.new
puts my_entry.text