[英]I want to hide my main window and then open new window(tkinter)
Could you help me?你可以帮帮我吗? I'm trying to close my main window and then create a new window.
我正在尝试关闭我的主要 window,然后创建一个新的 window。 I'm using withdraw() instead of destroy() since I'm planning to use that widget later.
我正在使用withdraw() 而不是destroy(),因为我打算稍后使用该小部件。
Here is my code, but I just get: tkinter.TclError: image "pyimage10" doesn't exist
这是我的代码,但我刚刚得到:
tkinter.TclError: image "pyimage10" doesn't exist
I separated the codes of the main window and a new window into two python file, which are "Page1" and "Page2".我将主 window 和新 window 的代码分成两个 python 文件,分别是“Page1”和“Page2”。
Sorry for my rough English.对不起我粗鲁的英语。 Any help to fix this would be much appreciated:)
任何解决此问题的帮助将不胜感激:)
tkinter.TclError: image "pyimage10" doesn't exist
seems to occur at image_2 = canvas.create_image(
tkinter.TclError: image "pyimage10" doesn't exist
似乎出现在image_2 = canvas.create_image(
Page1第1页
from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage
OUTPUT_PATH = Path(__file__).parent
ASSETS_PATH = OUTPUT_PATH / Path("./assets")
def relative_to_assets(path: str) -> Path:
return ASSETS_PATH / Path(path)
window = Tk()
window.geometry("703x981")
window.configure(bg = "#FFFFFF")
button_image_6 = PhotoImage(
file=relative_to_assets("button_6.png"))
button_6 = Button(
image=button_image_6,
borderwidth=0,
highlightthickness=0,
command=lambda: ("WM_DELETE_WINDOW", nextPage()),
relief="flat"
)
button_6.place(
x=415.0,
y=793.0,
width=86.0,
height=78.0
)
def nextPage():
window.withdraw()
import Page2
Page2第2页
from tkinter import Tk, Canvas, Entry, Text, Button, PhotoImage
window = Tk()
def relative_to_assets(path: str) -> Path:
return ASSETS_PATH / Path(path)
window.geometry("545x470")
window.configure(bg = "#FFFFFF")
window.title('PythonGuides')
canvas = Canvas(
window,
bg = "#FFFFFF",
height = 470,
width = 545,
bd = 0,
highlightthickness = 0,
relief = "ridge"
)
canvas.place(x = 0, y = 0)
image_image_2 = PhotoImage(
file=relative_to_assets("image_2.png"))
image_2 = canvas.create_image(
272.0,
175.0,
image=image_image_2
)
Maybe it's happening because the image you are trying to add on the button is not present where you are trying to access it from.可能是因为您尝试在按钮上添加的图像不存在于您尝试访问它的位置。
And seems you have missed this:似乎你错过了这个:
You didn't pack the button你没有打包按钮
You didn't run the.mainloop() ie window.mainloop()您没有运行 the.mainloop() 即 window.mainloop()
and what does the:什么是:
[enter the image description here][1] [在此处输入图片描述][1]
is meant to do?是什么意思?
I didn't use this and the code ran perfectly fine.我没有使用它,代码运行得很好。
This error is caused by having multiple instances of Tk
.此错误是由
Tk
的多个实例引起的。 Having multiple instances of Tk
can cause errors.拥有多个
Tk
实例可能会导致错误。 Use Toplevel
instead for additional windows.使用
Toplevel
代替额外的 windows。
Change window = Tk()
to window = Toplevel()
in Page2
.在
Page2
window = Tk()
更改为window = Toplevel()
。 You will also need to add Toplevel
to your import list.您还需要将
Toplevel
添加到您的导入列表中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.