在 tkinter 中的两个框架之间切换?
我已经构建了我的前几个脚本,上面有一个漂亮的小 GUI,正如教程所示,但它们都没有解决如何处理更复杂的程序。
如果您有一个带有“开始菜单”的东西,用于您的打开屏幕,并且根据用户选择,您移动到程序的不同部分并适当地重绘屏幕,那么执行此操作的优雅方法是什么?
是否只是.destroy()
“开始菜单”框架,然后创建一个新的框架,其中填充了另一部分的小部件?当他们按下后退按钮时反转这个过程?
I have built my first few scripts with a nice little GUI on them, as the tutorials have shown me, but none of them address what to do for a more complex program.
If you have something with a 'start menu', for your opening screen, and upon user selection you move to a different section of the program and redraw the screen appropriately, what is the elegant way of doing this?
Does one just .destroy()
the 'start menu' frame and then create a new one filled with the widgets for another part? And reverse this process when they press the back button?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用
pack
几何管理器,也许更直观的解决方案是使用pack_forget
方法隐藏/取消隐藏框架。这是一个简单的例子。
Perhaps a more intuitive solution would be to hide/unhide frames using the
pack_forget
method if you are using thepack
geometry manager.Here's a simple example.
一种方法是将框架堆叠在一起,然后您只需按照堆叠顺序将一个框架提高到另一个之上即可。顶部的那个将是可见的。如果所有框架的尺寸相同,则效果最佳,但只需做一些工作,您就可以使其适用于任何尺寸的框架。
注意:要实现此功能,页面的所有小部件都必须具有该页面(即:
self
)或后代作为父级(或主控,具体取决于页面)您喜欢的术语)。这里有一个人为的示例来向您展示一般概念:
如果您发现在类中创建实例的概念令人困惑,或者如果不同的页面在构造过程中需要不同的参数,你可以分别显式调用每个类。该循环主要用于说明每个类都是相同的。
例如,要单独创建类,您可以删除循环(
for F in (StartPage, ...)
):随着时间的推移,人们使用此代码(或在线教程)提出了其他问题复制此代码)作为起点,您可能想阅读这些问题的答案:
One way is to stack the frames on top of each other, then you can simply raise one above the other in the stacking order. The one on top will be the one that is visible. This works best if all the frames are the same size, but with a little work you can get it to work with any sized frames.
Note: for this to work, all of the widgets for a page must have that page (ie:
self
) or a descendant as a parent (or master, depending on the terminology you prefer).Here's a bit of a contrived example to show you the general concept:
If you find the concept of creating instance in a class confusing, or if different pages need different arguments during construction, you can explicitly call each class separately. The loop serves mainly to illustrate the point that each class is identical.
For example, to create the classes individually you can remove the loop (
for F in (StartPage, ...)
with this:Over time people have asked other questions using this code (or an online tutorial that copied this code) as a starting point. You might want to read the answers to these questions:
这是另一个简单的答案,但不使用类。
Here is another simple answer, but without using classes.
在 tkinter 中切换帧的一种方法是销毁旧帧,然后用新帧替换它。
我修改了Bryan Oakley的答案,在更换旧框架之前销毁旧框架。作为额外的好处,这消除了对
container
对象的需要,并允许您使用任何通用的Frame
类。说明
switch_frame()
通过接受任何实现框架
。然后该函数创建一个新框架来替换旧框架。_frame
(如果存在),然后用新框架替换它。.pack()
添加的其他框架(例如菜单栏)将不受影响。版本历史记录
One way to switch frames in
tkinter
is to destroy the old frame then replace it with your new frame.I have modified Bryan Oakley's answer to destroy the old frame before replacing it. As an added bonus, this eliminates the need for a
container
object and allows you to use any genericFrame
class.Explanation
switch_frame()
works by accepting any Class object that implementsFrame
. The function then creates a new frame to replace the old one._frame
if it exists, then replaces it with the new frame..pack()
, such as menubars, will be unaffected.tkinter.Frame
.Version History