我有一个函数为用户呈现一个组合框。
def select_interface(interfaces)
list_box :items => interfaces do |list|
interface = list.text
end
### ideally should wait until interface has a value then: ###
return interface
end程序的其余部分取决于从这个组合框中选择的内容。
我想找到一种方法,让ruby等待来自组合框的输入,然后执行其余的代码。
鞋子中有一个类似的函数,称为问,它将等待用户的输入。
interface = ask("write your interface here")我如何在Ruby/shoes中实现这个“等待变量有值”函数?
发布于 2008-12-21 21:21:38
我花了一段时间才理解你的问题:)我开始写一个关于GUI应用程序的整个理论的长篇答案。但你已经拥有了你所需要的一切。盒所使用的块实际上是它的更改方法。你在告诉它换衣服的时候该怎么做。当您获得所需的值时,只需将程序的其余部分推迟运行即可。
Shoes.app do
interfaces = ["blah", "blah1", "blah2"]
# proc is also called lambda
@run_rest_of_application = proc do
if @interface == "blah"
do_blah
# etc
end
@list_box = list_box(:items => interfaces) do |list|
@interface = list.text
@run_rest_of_application.call
@list_box.hide # Maybe you only wanted this one time?
end
end这是所有GUI应用程序背后的基本思想:构建初始应用程序,然后等待"events",这将为您响应创建新的状态。例如,在rubygnome2中,您将使用一个回调函数/块和一个Gtk::组合框来更改应用程序的状态。就像这样:
# Let's say you're in a method in a class
@interface = nil
@combobox.signal_connect("changed") do |widget|
@interface = widget.selection.selected
rebuild_using_interface
end即使在工具包之外,您也可以使用Ruby的观测器模块获得“免费”事件系统。希望这能帮上忙。
https://stackoverflow.com/questions/381480
复制相似问题