簡體   English   中英

如何從Gtk :: Entry小部件Ruby獲取文本

[英]How to Get Text from a Gtk::Entry Widget Ruby

我已經開始學習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小部件檢索文本? 提前致謝。

您需要的方法名稱是text 例如,

my_entry = Entry.new
puts my_entry.text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM