繁体   English   中英

问题第二次加载Tkinter窗口

[英]Problem Loading The Tkinter window for second time

所以我想为我开发的Tkinter应用程序制作主菜单,但是当我单击按钮上的一个打开另一个文件(另一个窗口也可以)时,这就是问题所在,但是当我关闭该窗口并尝试通过单击主菜单中的相同按钮再次将其打开,我得到此错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "c:\users\alireza\anaconda3\Lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "menu.py", line 32, in open_main
    root.destroy()
  File "c:\users\alireza\anaconda3\Lib\tkinter\__init__.py", line 2059, in destroy
    self.tk.call('destroy', self._w)
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed

这是我的主菜单文件(menu.py):

from tkinter import *
import tkinter.messagebox


class Application:
    def __init__(self, master, *args, **kwargs):


        self.mainframe = Frame(master, width = 300, height = 400, bg = 'slategray1')
        self.mainframe.pack()

        self.main_button = Button (self.mainframe, width = 25, height = 2,bg = "SteelBlue2" , text = "Customer Service", command = self.open_main)
        self.main_button.place(x= 55, y =50 )

        self.add_to_db_button = Button (self.mainframe, width = 25, height = 2,bg = "SteelBlue2" , text = "Add Item To Inventory", command = self.open_add_to_db)
        self.add_to_db_button.place(x= 55, y =100 )

        self.update_button = Button (self.mainframe, width = 25, height = 2,bg = "SteelBlue2" , text = "Update Inventory Items", command = self.open_update)
        self.update_button.place(x= 55, y =150 )

        self.about_button = Button (self.mainframe, width = 25, height = 2,bg = "sea green" , text = "Credits", command = self.about)
        self.about_button.place(x= 55, y =300 )

        self.close_button = Button (self.mainframe, width = 25, height = 2,bg = "navajo white" , text = "Quit", command = self.close_window)
        self.close_button.place(x= 55, y =350 )

    def close_window(self, *args, **kwargs):
        root.destroy()

    def open_main(self, *args, **kwargs):
        import main
        root.destroy()

    def open_add_to_db(self, *args, **kwargs):
        import add_to_db
        root.destroy()

    def open_update(self, *args, **kwargs):
        from update import AppUpdate
        root.destroy()

    def about(self, *args, **kwargs):
        tkinter.messagebox.showinfo("About Me", "Programmed and designed by Alireza Bagheri.")


root = Tk()
root.title("Main Menu")
root.resizable(False, False)
b = Application(root)
root.geometry("301x401+0+0")
root.mainloop()

我不完全知道问题出在哪里,所以将我指向正确的方向非常重要

正如Idlehands所说,我不认为您应该依赖import main来构建窗口,而是在import main中定义一个函数(也许是build_window() ?),然后可以调用它,例如main.build_window() 这将确保您知道应该执行什么代码,并且可以帮助避免错误。 我尝试运行您的代码,但无法复制该错误,但遇到了以下错误:

  1. ImportError: No module named 'main'
  2. ImportError: No module named 'update'
  3. ImportError: No module named 'add_to_db'

因此,这不是您面临的错误的最低限度,可验证的完整示例代码。 我们能为您提供的最大帮助就是推测解决方案。 我的猜测是,如果您想“隐藏”一个以后要返回的窗口,则应使用.iconify()它将其最小化到任务栏。 如果在Tkinter窗口小部件上调用.destroy() ,该窗口小部件将从内存中删除,并且如果您希望另一个窗口小部件,则必须对其进行定义,然后将.pack() .place().grid()重新定义到屏幕! 我建议您阅读https://stackoverflow.com/help/mcve,以了解最小,完整和可验证的示例是什么。 解决您的问题可以使社区更轻松地为您提供帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM