我正在尝试使用 TK 为 Mad Libs 程序在 Ruby 中编写 GUI。本质上,它需要从 Entry 小部件中获取用户的输入,并用该信息替换一行文本中的某些单词。以下是相关代码($content为父窗口):
$name=TkVariable.new()
name=Tk::Tile::Entry.new($content) {width 7; textvariable $name}.grid(:column =>1, :row =>0, :sticky => 'we')
# Mad Lib base text
$text=TkVariable.new("My name is #{name.get}.")
# Displays end result
def showResult
display=Tk::Tile::Label.new($content) {textvariable $text}.grid(:column =>0, :row => 6, :sticky => 'we')
end
# Button to replace input in base text.
# Clicking this button adds a new Label widget containing the text. The variables,
#however, retain whatever value they were initialized to (in this case, nothing)
Tk::Tile::Button.new($content) {text 'Submit'; command 'showResult'}.grid(:column => 1, :row => 5, :sticky => 'we')
据我了解,name.get 应该给我在 Entry 小部件字段中输入的任何内容。然而 name 和 $name 都没有更新。