GUIによる項目の選択(ラジオボタン)

ラジオボタン

いくつかある項目の中から1つだけ選びたい時はラジオボタンを使用する。

radioButton(ラジオボタン)
複数のボタンのうち一つだけがオンになるもの。
複数のラジオボタンを一つのグループとしてまとめるにはradioCollectionコマンドを使用する。
radioButtonコマンドとradioCollectionコマンドはcheckboxコマンドと同じように名前を自分で決めて作ることもできるし、MELが決めた名前を変数に代入して利用することもできる。
どのラジオボタンが選ばれているかは以下のコマンドで得ることができる。
radioButton -q -select (radioCollection名);
または
radioButton -q -select (radioCollection名の入っている変数名);
これらによってオンになっているradioButtonの名前が返される。
具体的な使用例は以下の "ラジオボタンの使用例"を参照すること。
radioButtonGrp
複数のラジオボタンをまとめたもの。

ラジオボタンの使用例

ラジオボタンによってプリミティブ(球、キューブのうちのどちらか)を作るGUIを作ってみよう。
今回はラジオボタンに自分で適当な名前(radioButton1など)をつけてゆく方法で作成している。

  1. 以下のMELスクリプトをradio1.melという名前で作る。
    global proc makePrimitive()
    {
    	global string $radioCollection1;
    	string $selected;
    
    	$selected = `radioCollection -q -select $radioCollection1`;
    	if($selected == "radioButton1")
    	{
    		sphere;
    	}
    	else if($selected == "radioButton2")
    	{
    		nurbsCube;
    	}
    }
    
    string $windowName = `window -title "radio1"`;
    columnLayout;
    text -label "make Primitive";
    string $radioCollection1 = `radioCollection -global false`;
    	radioButton -label "Sphere" -select radioButton1;
    	radioButton -label "Cube" radioButton2;
    button -label "OK" -command "makePrimitive()";
    button -label "Close" -command "deleteUI $windowName";
    showWindow;
    
  2. Script Editorの File → Source Script によってradio1.melを読み込む。
  3. 以下のようなウインドウが表示される。
    [radio1.melの実行結果]
  4. Cubeボタンにチェックを入れてOKボタンを押すとCubeが作られる。
    [ラジオボタンを押した結果]
    [OKボタンを押した結果]

スクリプトの解説

global proc makePrimitive()
このプロシージャがOKボタンによって実行される。
以下はmakePrimitiveの中身。
global string $radioCollection1;
外部変数の宣言。
このプロシージャ(makePrimitive)の外側で宣言された(される)変数をプロシージャ内で使用するときはglobalをつける。
string $selected;
選ばれたラジオボタンの名前を代入するための変数の宣言。
$selected = `radioCollection -q -select $radioCollection1`;
$radioCollection1という変数に代入されている名前のラジオコレクションの中で選ばれているラジオボタンを調べて、その名前を$selectedという変数に代入する。
if($selected == "radioButton1")
{
	sphere;
}
radioButton1(1つ目のラジオボタンの名前)が選ばれていれば、sphereコマンドを実行する。
else if($selected == "radioButton2")
{
	nurbsCube;
}
radioButton2(2つ目のラジオボタンの名前)が選ばれていれば、nurbsCubeコマンドを実行する。
string $windowName = `window -title "radio1"`;
radio1というタイトルのついたウインドウを作る。
作られたウインドウの名前は$windowNameという変数に代入される。
columnLayout;
部品を縦1列に並べるレイアウトコマンド。
text -label "make Primitive";
make Primitiveというテキストを表示。
string $radioCollection1 = `radioCollection`;
ラジオコレクションを作り、その名前を$radioCollection1という変数に代入する。
これ以後に並べるラジオボタンが一つのグループになる。 (このグループから1つのボタンが選ばれる)
radioButton -label "Sphere" -select radioButton1;
ラジオボタンを作り、そのラジオボタンの名前をradioButton1にする。
-selectフラグがついていると最初にこのボタンが選ばれた状態で表示される。
radioButton -label "Cube" radioButton2;
ラジオボタンを作り、そのラジオボタンの名前をradioButton2にする。
button -label "OK" -command "makePrimitive()";
OKボタンを作る。
ボタンを押すとmakePrimitive()が実行される。
button -label "Close" -command "deleteUI $windowName";
Closeボタンを作る。
ボタンを押すと、deleteUIコマンドによって$windowNameに入っている名前のウインドウが消去される。
showWindow;
ウインドウを表示する。

練習

まとめ

参考


Prev
Home | Contents
abe@injapan.net