总的来说,IDL是先创立一个基本窗口,里面没有东西,代码如下。 PRO experiment_event, ev print,'draw event',ev.x,ev.y,ev.press,ev.release ; 分别打印出点下鼠标的坐标和松开鼠标的坐标,此句会有两行输出 END
PRO experiment main = WIDGET_BASE() ; 此处是建立一个基本窗口 draw = WIDGET_draw(main, /button_events); 在基本窗口中建立一个画图窗口,而且起到按钮的功能(对鼠标点击有反应) WIDGET_CONTROL, main, /REALIZE ; 此句将窗口画出 XMANAGER, 'experiment', main ; 在窗口中进行的操作应写在此句之前,
; 单引号中的单词应该第一个自过程中_event前的单词相同 END
有了基本窗口之后,就可以在此窗口内再创建窗口或者按钮。
PRO experiment_event, ev widget_control, ev.id, get_uvalue=uvalue
CASE uvalue of ; 由于定义了两个控件(窗口):一个按钮、一个画图窗口,所以分情况对待 'go' : print,'GO button' 'draw' : print,'draw event',ev.x,ev.y,ev.press,ev.release END END
PRO experiment2 main = WIDGET_BASE(title='OTF experiments',/row) btn = widget_button(main,uvalue='go',value='GO') draw = WIDGET_draw(main,uvalue='draw', /button) WIDGET_CONTROL, main, /REALIZE XMANAGER, 'experiment', main,/no_block END
窗口的大小可以通过xsize和ysize控制。
PRO experiment_event, ev widget_control, ev.id, get_uvalue=uvalue
CASE uvalue of 'go' : print,'GO button' 'draw' : print,'draw event',ev.x,ev.y,ev.press,ev.release END END
PRO experiment3 main = WIDGET_BASE(title='OTF experiments',/row,/frame,xsize=500,ysize=500) cntl = widget_base(main,/column,/frame,xsize=100,ysize=100) btn = widget_button(cntl,uvalue='go',value='GO') draw = WIDGET_draw(main,uvalue='draw', /button) WIDGET_CONTROL, main, /REALIZE XMANAGER, 'experiment', main,/no_block END
按钮的功能可以定义为关闭窗口,还可以在画图窗口画一个图。
PRO experiment_event, ev widget_control, ev.id, get_uvalue=uvalue widget_control, ev.top, /destroy ; 按QUIT按钮的时候关闭窗口 CASE uvalue of 'quit' : print,'QUIT button' 'draw' : print,'draw event',ev.x,ev.y,ev.press,ev.release END END
PRO experiment4 main = WIDGET_BASE(title='OTF experiments',/row,/frame,xsize=500,ysize=500) cntl = widget_base(main,/column,/frame,xsize=100,ysize=100); 定义一个子窗口 btn = widget_button(cntl,uvalue='quit',value='QUIT',xsize=100,ysize=100);在子窗口中放一个按钮 draw = WIDGET_draw(main,uvalue='draw', /button,xsize=300,ysize=300) WIDGET_CONTROL, main, /REALIZE a=indgen(100,100) tv,a ; 在画图窗口画一个图 XMANAGER, 'experiment', main,/no_block ; note this name in '' must be the same ; as that before _event in last PRO!!! END
原则上可以加很多按钮,实现不同功能,画图窗口也可以调整。
PRO experiment_event, ev widget_control, ev.id, get_uvalue=uvalue ;widget_control, ev.top, /destroy widget_control, ev.top, get_uvalue=textwid CASE uvalue of 'one' : widget_control, textwid, set_value='Button 1 Pressed' 'two' : widget_control, textwid, set_value='Button 2 Pressed' 'quit' : widget_control, ev.top, /destroy ; 'draw' : print,'draw event',ev.x,ev.y,ev.press,ev.release ENDCASE END
PRO experiment5 main = WIDGET_BASE(title='OTF experiments',/row,/frame,xsize=600,ysize=500) ;cntl = widget_base(main,/row,/frame,xsize=100,ysize=100) cntl = widget_base(main,/column,/frame,xsize=100,ysize=100) ;cntl = main btn1 = widget_button(cntl,uvalue='one',value='ONE',xsize=100,ysize=62) btn2 = widget_button(cntl,uvalue='two',value='two',xsize=100,ysize=62) btn = widget_button(cntl,uvalue='quit',value='QUIT',xsize=100,ysize=62) text = widget_text(main,xsize=20) draw = WIDGET_draw(main,uvalue='draw', /button,xsize=300,ysize=300) widget_control, main, set_uvalue=text WIDGET_CONTROL, main, /REALIZE a=indgen(300,300) tv,a XMANAGER, 'experiment', main,/no_block ; note this name in '' must be the same ; as that before _event in last PRO!!! END