小编Dau*_* Zi的帖子

如何在Python中安全地删除多个已定义和未定义的变量?

我目前正在使用jupyter笔记本,我想删除仅在单元格内使用的变量,这样我就不会意外地在其他单元格中滥用这些变量。

例如,我想删除以下代码中的变量myvar和循环变量:i

start = 1
stop = 2
for i in range(start, stop):
    pass
myvar = "A"
# other statements
# ...
del i, myvar # Runs ok, and variables i and myvar are deleted and I won't accidentally use i or myvar in another jupyter notebook cell
Run Code Online (Sandbox Code Playgroud)

这工作得很好,但在某些情况下,某些变量实际上没有定义。在下面的示例中,这会引发错误,因为i未定义,因为循环从未运行。定义的变量myvar不会被删除。

start = 1
stop = 1 # Now we have stop equal to 1
for i in range(start, stop):
    pass
myvar = "A"
# …
Run Code Online (Sandbox Code Playgroud)

python

5
推荐指数
2
解决办法
2万
查看次数

我们如何在 FXML 中定义上下文菜单?

虽然我可以在没有 FXML 的情况下定义 ContextMenu,但我没有找到在 FXML 中定义上下文菜单的好方法:

在源文件中:

ContextMenu contextMenu = new ContextMenu();
contextMenu.getItems().addAll(someMenuItems);
// This runs perfectly
Run Code Online (Sandbox Code Playgroud)

在 FXML 中:

<ContextMenu fx:id="contextMenu">
</ContextMenu>
// This is incorrect in fxml. The Exception of "Unable to coerce javafx.scene.control.ContextMenu to class javafx.scene.Node" is thrown.

<MenuBar fx:id="menuBar">
    //... Some Menu and items could be defined here
</MenuBar>
// This is correct in fxml
Run Code Online (Sandbox Code Playgroud)

我知道 MenuBar 可以工作,因为它扩展了 javafx.scene.control.Control,它是 javafx.scene.Node 的子类,但 ContextMenu 没有。

那么有没有办法在 FXML 中类似地定义 ContextMenu 的属性呢?

java javafx fxml

2
推荐指数
2
解决办法
2933
查看次数

标签 统计

fxml ×1

java ×1

javafx ×1

python ×1