[英]Python - How to switch frames in a Tkinter window
I want to switch frames in a Tkinter window but the previous frame always stays visible in the background. 我想在Tkinter窗口中切换帧,但是上一帧始终在后台保持可见状态。
This is the code I have: 这是我的代码:
from tkinter import *
class frame(Frame):
def __init__(self,display):
Frame.__init__(self,display)
l = Label(self,text="frame1")
l.pack()
class frame2(Frame):
def __init__(self,display):
Frame.__init__(self,display)
l = Label(self,text="frame2")
l.pack()
class test(Tk):
def __init__(self):
Tk.__init__(self)
f = frame(self)
f.grid(row=0)
f2 = frame2(self)
f2.grid(row=0)
f.tkraise()
t = test()
t.mainloop()
This works if the layout of the two frames is the same but if I add another label to the second frame, it will still be visible in the Background. 如果两个框架的布局相同,但是如果我在第二个框架中添加另一个标签,则该标签仍将在背景中可见,这将起作用。 Is there a way to switch frames so that only elements from the raised frame are visible?
有没有一种方法可以切换框架,以便仅显示凸起框架中的元素?
As requested, this is what I used to fix my problem: 根据要求,这是我用来解决问题的方法:
from tkinter import *
class frame(Frame):
def __init__(self,display):
Frame.__init__(self,display)
l = Label(self,text="frame1")
l.pack()
class frame2(Frame):
def __init__(self,display):
Frame.__init__(self,display)
l = Label(self,text="frame2")
l.pack()
class test(Tk):
def __init__(self):
Tk.__init__(self)
f2 = frame2(self)
f2.grid(row=0)
#To raise the first frame, I used the following
frame2.grid_remove()
f = frame(self)
f.grid(row=0)
t = test()
t.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.