目前尚不清楚您的目标是什么。在某处显示@mobility?仅仅为@mobility 变量赋值就没有任何作用。
Shoes.app do
@choice_map = {
"1- Ad lib" => 11,
"2- Assist of SB" => 22,
"3- Assist of 1" => 33,
"4- Assist of 2" => 44
}
para "Choose item:"
@my_listbox = list_box(:items => @choice_map.keys)
button 'Compute' do
@mobility = @choice_map[@my_listbox.text]
end
end
要证明 @mobility 包含正确的值,您需要在某处显示该值:
Shoes.app do
@choice_map = {
"1- Ad lib" => 11,
"2- Assist of SB" => 22,
"3- Assist of 1" => 33,
"4- Assist of 2" => 44
}
@my_listbox = list_box(items: @choice_map.keys)
@my_para = para 'show computed results here'
button 'Compute' do
@mobility = @choice_map[@my_listbox.text]
@my_para.text = @mobility
end
end
您的代码实际上只需稍作改动即可正常工作:
Shoes.app :title => 'Procedural', :width => 300, :height => 200 do
@list_box = list_box :items => ["1- Ad lib", "2- Assist of SB", "3- Assist of 1", "4- Assist of 2"]
@my_para = para 'show computed results here'
button 'Compute' do
@acuity = @list_box.text #<---CHANGE HERE
if @acuity == "1- Ad lib"
@mobility = 11
elsif @acuity == "2- Assist of SB"
@mobility = 22
elsif @acuity == "3- Assist of 1"
@mobility = 33
elsif @acuity == "4- Assist of 2"
@mobility = 44
end
@my_para.text = @mobility
end
如果您不需要该按钮:
Shoes.app do
@choice_map = {
"1- Ad lib" => 11,
"2- Assist of SB" => 22,
"3- Assist of 1" => 33,
"4- Assist of 2" => 44
}
para "Choose item:"
@default = "2- Assist of SB"
@mobility = @choice_map[@default]
@my_listbox = list_box(items: @choice_map.keys, choose: @default) do |list|
@mobility = @choice_map[list.text]
end
end
提供给 list_box() 的块仅在更改时执行,因此您必须为 @mobility 设置一个初始值。
您会认为类似以下的内容能够检索列表框中的初始值:
Shoes.app do
@choice_map = {
"1- Ad lib" => 11,
"2- Assist of SB" => 22,
"3- Assist of 1" => 33,
"4- Assist of 2" => 44
}
para "Choose item:"
@my_listbox = list_box(items: @choice_map.keys) do |list|
@mobility = @choice_map[list.text]
@my_para.text = @mobility
end
@mobility = @choice_map[@my_listbox.text]
@my_para = para @mobility
end
但显然列表框直到应用程序块完成执行后才存在,因此您无法在应用程序块内检索@my_listbox.text。