[Go to BBS]
All articles in a thread
SubjectSAD problem with ploting
Article No348
Date: 2007/02/14(Wed) 14:39:35
ContributorWangWeizhen < >
I encounter a problem with ploting a figure in a new window, the code as follows:

FFS;
w = KBMainFrame["w",f,Title->"abc"];
b = Button[f,Text->"abc",Command:>(
w1 = Window[Title->"plot"];
c1 = Canvas[w1];
$DisplayFunction=CanvasDrawer;
Canvas$Widget=c1;
plot1 = Plot[Sin[x],{x,-Pi,Pi}]
)];
TkWait[];

The problem is: When I click the button for the first time, the figure shows; however, if I close the window and click the button again, there is no figure in the new window, and some error message appears.
My SAD is on linux with the version 1.0.10b2, and the version of Tcl/Tk is 8.4.14.

Thanks a lot~

SubjectRe: SAD problem with ploting
Article No349
Date: 2007/02/15(Thu) 14:39:52
ContributorK. Oide
One solution is something like

FFS;
w = KBMainFrame["w",f,Title->"abc"];
b = Button[f,Text->"abc",Command:>(
w1 = Window[Title->"plot"];

Bind[w1,"<Destroy>",
CanvasGraphics@CanvasObject[c1]=.;
c1=.;w1=.];

c1 = Canvas[w1];
Canvas$Widget=c1;
plot1 = Plot[Sin[x],{x,-Pi,Pi}]
)];
TkWait[];


to define a call back when w1 is destroyed, to clean up c1, w1, and CanvasGraphics@CanvasObject[c1]. The last one is a hidden object associated with CanvasDrawer.

Still it may consume some memories by making/killing the memory in this way. So I recommend to use a fixed window and canvas. Instead of destroying the window, you can show/hide it.