PDA

View Full Version : GUI question


sheb
12-28-2007, 01:20 AM
Hello there,

I want to read a string from a textfield and when I click a button pass it to a function. This should be easy enough .. I thought!

It works when I create the window on a global level but not when I do it in a function! Here is my code:


proc test()
{
if (`window -exists "window1"`)deleteUI -window "window1";

string $wnd = `window -title "Test Window" "window1"`;
columnLayout ColumnLayout;

$txt = `textField -text "Ready" -editable true -width 278 StatusLine`;
$btn = `button -label "Click me"`;
button -edit -command "print (`textField -query -text $txt`+\"\\n\")" $btn;
showWindow $wnd;

}
test();


I guess the problem is that the textfield variable $txt doesnt exist anymore since it was created inside the function. I'm fairly new to mel, could anybody tell me how to do this properly?


Thanks alot in advance!

seb

sheb
12-28-2007, 02:20 AM
By properly browsing here I found the solution here :

http://forums.cgsociety.org/showthread.php?f=89&t=303660&highlight=gui

:D

ashishdantu
12-28-2007, 02:25 AM
hi seb,

i see that you choose to use "StatusLine" as the name for the textfield. so the following shoud work :
proc test()
{
if (`window -exists "window1"`)deleteUI -window "window1";
string $wnd = `window -title "Test Window" "window1"`;
columnLayout ColumnLayout;
textField -text "Ready" -editable true -width 278 StatusLine;
$btn = `button -label "Click me"`;
button -edit -command "print (`textField -query -text StatusLine`+\"\\n\")" $btn;
showWindow $wnd;
}
test();

on a general note, i prefer naming all the UI controllers uniquely so we know what they are... when we need to query their values.

and your point that "the textfield variable $txt doesnt exist anymore since it was created inside the function." is right.. since the variables have been declared inside the function, their scope is limited with in it. You can use global variables to hold the names of the controls but thats a v.bad idea to store control names in global vars.

CGTalk Moderation
12-28-2007, 02:25 AM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.