我已经在上面构建了我的前几个脚本,上面有一个漂亮的小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?
发布评论
评论(4)
一种方法是将框架彼此堆叠在一起,然后您可以在堆叠顺序中简单地将一个置于另一个上方。顶部将是可见的。如果所有框架的尺寸相同,则最有效,但是有了一些工作,您可以将其与任何大小的框架一起使用。
注释:为了使某个页面的所有小部件都必须具有该页面(即:
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's 在替换之前,请回答以销毁旧框架。作为额外的奖励,这消除了对
容器
对象的需求,并允许您使用任何通用frame> frame
类。说明
switch_frame()
通过接受任何实现_frame
如果存在,则将其替换为新帧。.pack()
(例如梅纳布尔)的帧将不受影响。tkinter.frame
的类一起使用。版本历史记录
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
如果您使用
pack_forget
方法,也许更直观的解决方案是使用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.