Why doesn't running ClearAll[“Global`*”] from a custom Palette update local variables colors?

微笑、不失礼 提交于 2019-12-09 13:20:52

问题


I created a custom palette, with an ActionMenu that executes ClearAll["Global`*"]. It performs the desired action, but the frontend doesn't instantly update the colors from black to blue (standard), as it happens when ClearAll["Global`*"] is executed directly from a notebook cell (it only updates after something else has been executed). Is there a way to initiate a refresh of the frontend display status?

EDIT: Apparently, the same happens when I load a package from a palette (blue doesn’t immediately update to black).


回答1:


Not exactly elegant, but it works (the main loop has to be called once after ClearAll):

ActionMenu["Clear", 
   {"Clear global variables" :> (ClearAll["Global`*"]; 
         Block[{nb}, nb = CreateDocument[ExpressionCell[42, "Input"], 
                Visible -> False]; SelectionMove[nb, All, Notebook]; 
            SelectionEvaluate[nb]; NotebookDelete[nb];])}]

Of course, one should use the new Mathematica 8 NotebookEvaluate function, but it seems not to work here (i.e.,

ActionMenu["Clear", 
   {"Clear global variables" :> (ClearAll["Global`*"]; 
         Block[{nb}, nb = CreateDocument[ExpressionCell[42, "Input"], 
                Visible -> False]; NotebookEvaluate[nb]; 
     NotebookDelete[nb];])}]

gives an error message

Could not process unknown packet "1".


来源:https://stackoverflow.com/questions/7022538/why-doesnt-running-clearallglobal-from-a-custom-palette-update-local-vari

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!