我收到一个错误:当我按下以下代码中的按钮时,".!frame"错误的窗口路径名?



我正在尝试制作两个不同的屏幕,并使用每个屏幕上的按钮将它们相互链接,这些按钮为它们分配了受人尊敬的功能作为命令。

请帮助我知道为什么我收到以下错误:

错误的窗口路径名".!框架">

当我按下以下代码中的按钮时。

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)
# FRAMES
main_frame = Frame(root, bg="Red", borderwidth=30)
other_frame = Frame(root, bg="Yellow", borderwidth=30)

# first window
def main_screen():
other_frame.destroy()
Label(main_frame, text="First Window").pack()
Button(main_frame, text="GO", command=second_screen).pack()
main_frame.pack()

def second_screen():
main_frame.destroy()
Label(other_frame, text="Second Window").pack()
Button(other_frame, text="BACK", command=main_screen).pack()
other_frame.pack()

main_screen()
root.mainloop()

你在main_screen做的第一件事就是摧毁other_frame。当您调用second_screen时,它会尝试使用该框架作为新小部件的父帧,但它已被销毁。这就是为什么你会得到错误bad window path name ".!frame"- 它很糟糕,因为它已被破坏并且不能再使用。

一个简单的解决方案是简单地不调用other_frame.destroy()。相反,您可以使用other_frame.pack_forget()将其从视图中删除。

重要的是要知道,pack的工作原理是将对象放置在可用空间中,并按照调用pack的顺序。如果在应用程序启动后删除并添加微件,则可能会影响微件的显示顺序。

在这种特定情况下,由于main_screenother_screen是唯一直接在root中的小部件,因此没关系,并且不会影响您获得错误的原因。

问题是您在编写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()

希望这有帮助!

最新更新