简体   繁体   English

我想隐藏我的主要 window 然后打开新窗口(tkinter)

[英]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:似乎你错过了这个:

  1. You didn't pack the button你没有打包按钮

  2. You didn't run the.mainloop() ie window.mainloop()您没有运行 the.mainloop() 即 window.mainloop()

  3. 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.

相关问题 我想要一个按钮来执行双重命令一个命令将终止我当前的 window 另一个命令将在 tkinter 中打开一个新的 window - I want a single button to do double command one command will terminate my present window and another command will open a new window in tkinter 我想在tkinter中创建一个新窗口 - i want to make a new window in tkinter Tkinter如何使用按钮隐藏当前窗口并打开新窗口 - Tkinter how to use a button to hide current window and open new one tkinter python:想要关闭初始 window 并打开新的 - tkinter python: Want to close initial window and open new one 使用另一个“顶层”窗口登录时,如何隐藏tkinter程序主窗口? - How can I hide the main tkinter program window whilst logging in using another Toplevel window? 当主 Window 不可见时隐藏或关闭 Tkinter 顶层 Window - Hide or Close Tkinter Top Level Window When Main Window Is Not Visible 如何在 TKinter 中打开一个新的 window,然后向其添加按钮? - How do I open a new window in TKinter, then add buttons to it? 如何通过单击带有 tkinter 的按钮打开新的 window? - How do i open a new window on the click of a button with tkinter? 当我打开 window 的新实例时,Tkinter 小部件丢失了信息 - Tkinter widgets lost information when I open a new instance of their window 如果我的登录用户名和密码在我的登录 window 中正确,如何在 Tkinter 中打开一个新的 window? - How to open a new window in Tkinter if my login username and password are correct in my login window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM