簡體   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