2

ユーザーにコンボボックスを表示する機能があります。

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

プログラムの残りの部分は、このコンボ ボックスからの選択に依存します。

ルビーがコンボボックスからの入力を待ってから、残りのコードを実行する方法を見つけたいと思います。

ユーザーの入力を待つ、askと呼ばれる同様の関数がshoesにあります。

interface =  ask("write your interface here")

Ruby/shoesでこの「変数に値が入るまで待つ」機能を実装するにはどうすればよいですか?

4

1 に答える 1

2

あなたの質問を理解するのに少し時間がかかりました:) GUIアプリケーションの理論全体について長い回答を書き始めました。しかし、あなたはすでに必要なものをすべて持っています。list_boxが取るブロックは、実際にはその change メソッドです。変更されたときに何をすべきかを伝えています。必要な値が得られたら、プログラムの残りの部分を実行するのを遅らせるだけです。

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 アプリケーションの背後にある基本的な考え方です。最初のアプリケーションをビルドしてから、応答するための新しい状態を作成する「イベント」を待ちます。たとえば、ruby-gnome2 では、アプリケーションの状態を変更するGtk::ComboBoxでコールバック関数/ブロックを使用します。このようなもの:

# 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 のObserver モジュールを使用して「無料」のイベント システムを取得できます。これが役に立ったことを願っています。

于 2008-12-21T21:21:38.423 に答える