问题是你在写other_frame.destroy()之前删除了框架other_frame。这使得程序的其余部分无法访问它,因为您销毁了它。
所以会有两种解决方案:一种是,正如 BryanOakley 所提到的,使用.pack()。另一种是使用.place()。
.pack() 的代码:
from tkinter import *
root = Tk()
my_height = 600
my_width = 350
root.geometry(f"{my_height}x{my_width}")
# root.maxsize(my_height, my_width)
def main_screen():
other_frame.pack_forget()
main_frame.pack()
def second_screen():
main_frame.pack_forget()
other_frame.pack()
# FRAMES
main_frame = Frame(root, bg="Red", borderwidth=30)
other_frame = Frame(root, bg="Yellow", borderwidth=30)
Label(other_frame, text="Second Window").pack()
Button(other_frame, text="BACK", command=main_screen).pack()
Label(main_frame, text="First Window").pack()
Button(main_frame, text="GO", command=second_screen).pack()
# first window
main_screen()
root.mainloop()
.place() 的代码:
from tkinter import *
root = Tk()
my_height = 600
my_width = 350
root.geometry(f"{my_height}x{my_width}")
# root.maxsize(my_height, my_width)
# first window
def main_screen():
main_frame.place(relx = 0.5, rely = 0.1, anchor = "center")
other_frame.place(relx = 1.5, rely = 0.1, anchor = "center")
main_frame.update()
other_frame.update()
def second_screen():
main_frame.place(relx = -0.5, rely = 0.1, anchor = "center")
main_frame.update()
other_frame.place(relx = 0.5, rely = 0.1, anchor = "center")
other_frame.update()
# FRAMES
main_frame = Frame(root, bg="Red", borderwidth=30)
other_frame = Frame(root, bg="Yellow", borderwidth=30)
main_frame.place(relx = 0.5, rely = 0.1, anchor = "center")
other_frame.place(relx = 1.5, rely = 0.1, anchor = "center")
Label(main_frame, text="First Window").pack()
Button(main_frame, text="GO", command=second_screen).pack()
Label(other_frame, text="Second Window").pack()
Button(other_frame, text="BACK", command=main_screen).pack()
main_screen()
root.mainloop()
使用.place() 的优点是您可以使用 for 循环来来回动画帧。
下面是代码:
from tkinter import *
root = Tk()
my_height = 600
my_width = 350
root.geometry(f"{my_height}x{my_width}")
# root.maxsize(my_height, my_width)
# first window
def main_screen():
global x1, y1, x2, y2
for i in range(1000):
x1 += 0.001
x2 += 0.001
main_frame.place(relx = x1, rely = y1, anchor = "center")
other_frame.place(relx = x2, rely = y2, anchor = "center")
main_frame.update()
other_frame.update()
def second_screen():
global x1, y1, x2, y2
for i in range(1000):
x1 -= 0.001
x2 -= 0.001
main_frame.place(relx = x1, rely = y1, anchor = "center")
other_frame.place(relx = x2, rely = y2, anchor = "center")
main_frame.update()
other_frame.update()
# FRAMES
x1, y1 = 0.5, 0.1
x2, y2 = 1.5, y1
main_frame = Frame(root, bg="Red", borderwidth=30)
other_frame = Frame(root, bg="Yellow", borderwidth=30)
main_frame.place(relx = 0.5, rely = 0.1, anchor = "center")
other_frame.place(relx = 1.5, rely = 0.1, anchor = "center")
Label(main_frame, text="First Window").pack()
Button(main_frame, text="GO", command=second_screen).pack()
Label(other_frame, text="Second Window").pack()
Button(other_frame, text="BACK", command=main_screen).pack()
root.mainloop()
希望这会有所帮助!