Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 12 months ago.
The community reviewed whether to reopen this question 12 months ago and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(1)
这种情况——有一对相反的操作——需要上下文经理。您无需将容器小部件显式推入堆栈或从堆栈中弹出,而是将容器的子级包装在
with
块中。基于您在此处显示的代码,这可以实现为类似的内容(< 的文档代码>contextlib.contextmanager)。
您的
SetupGUI
方法将变为:如您所见,嵌套从缩进中清晰可见,无需手动推送和弹出。
This sort of situation -- where you have a pair of opposite operations -- calls for a context manager. Instead of explicitly pushing and popping container widgets onto/from a stack, you would wrap the children of the container in a
with
block. Building on top of the code you've shown here, this could be implemented as something like(Documentation for
contextlib.contextmanager
).Your
SetupGUI
method then becomes:As you can see, the nesting is clear from the indentation, and there's no need to manually push and pop.