如何运行调色板的初始化代码?
有时,最好有一些调色板(按钮)的初始化代码。例如,它可以定义调色板按钮使用的一些功能。
定义/运行调色板初始化代码的最简单和最好的方法是什么?
- 初始化可以在加载调色板时或第一次按下任何按钮时运行(可能的问题:如果在加载调色板后重新启动内核会怎样?)
- 定义应该以某种方式本地化(即在它们自己的上下文中 - 独特的笔记本上下文在这里有帮助吗?)
- 如果可能,我希望付出最小的努力解决方案(即在尽可能少的地方添加额外代码,自包含调色板文件,没有额外的包文件,使用调色板菜单或
CreatePalette
等现有的便利工具创建调色板 - (您可以假设初始化代码运行得很快,例如它仅由定义组成)
Occasionally it would be preferable to have some initialization code for palettes (of buttons). For example, it could define some functions that are used by palette buttons.
What is the easiest and preferable way to define/run initialization code for a palette?
- The initialization can run either when the palette is loaded or when any button is pressed for the first time (possible issue: what if the kernel is restarted after the palette was loaded?)
- The definitions should be somehow localized (i.e. in their own context -- do unique notebook contexts help here?)
- If possible, I'd prefer a minimal effort solution (i.e. extra code at the fewest possible places, self contained palette file with no extra package files, palette creation using the existing convenience tools of palettes menu or
CreatePalette
, etc.) - (You can assume that the initialization code runs fast, e.g. it consists of definitions only)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对动态可见性问题的担忧是正确的。无论任何单个单元格的可见性如何,绝对保证对
Dynamic
表达式进行求值的方法是使用NotebookDynamicExpression
。下面的示例说明了NotebookDynamicExpression
在Dynamic
因隐藏在封闭单元格组中而失败时仍能正常工作:当您对此进行评估时,请注意
Dynamic
NotebookDynamicExpression
中的值立即计算。在您打开单元格组之前,DynamicWrapper
永远不会计算,您可以通过单击“单击打开”文本来完成此操作。顺便请注意,在此示例中,我用
Refresh
包装了NotebookDynamicExpression
。函数Refresh[#, None]&
将确保代码仅计算一次 - 当笔记本首次打开时。否则,代码将遵循Dynamic
的标准属性,并在任何依赖项发生变化时进行评估。NotebookDynamicExpression
自 v6 以来就已存在,但仅在 v8 中进行了记录。还记录了其相关的表兄弟CellDynamicExpression
和FrontEndDynamicExpression
。You're right to be concerned about the visibility of the
Dynamic
being an issue. The way to absolutely guarantee aDynamic
expression to be evaluated regardless of the visibility of any of the individual cells is to useNotebookDynamicExpression
. Here's an example that illustratesNotebookDynamicExpression
working while aDynamic
fails because it's hidden within a closed cell group:When you evaluate this, note that the
Dynamic
inNotebookDynamicExpression
evaluates immediately. TheDynamicWrapper
never evaluates until you open the cell group, which you can do by clicking on the "Click to open" text.In this example, incidentally, notice that I wrapped the
NotebookDynamicExpression
withRefresh
. The functionRefresh[#, None]&
will make sure that the code evaluates only once -- when the notebook is first opened. Otherwise, the code would obey the standard properties ofDynamic
and evaluate whenever any of the dependencies change.NotebookDynamicExpression
has been around since v6, but was only documented in v8. Also documented are its related cousins,CellDynamicExpression
andFrontEndDynamicExpression
.带有
初始化
的DynamicBox
能够实现基本功能。您可以调整调色板的大小,使该对象不可见,但它仍然可以运行。这是一个小示例调色板的代码。它为
var
设置一个值。活动代码用空格偏移。A
DynamicBox
withInitialization
is capable of the basic function. You can size the palette such that the object is not visible, and it will still operate.Here is code for a small sample palette. It sets a value for
var
. The active code is offset with whitespace.