class LoginWindow: def __init__(self, master): self.master = master self.master.title("Login Window") self.master.geometry("300x250") self.master.resizable(0, 0) self.username = tk.StringVar() self.password = tk.StringVar() tk.Label(self.master, text="賬號").grid(row=0, column=0, padx=5, pady=5) tk.Entry(self.master, textvariable=self.username).grid(row=0, column=1, padx=5, pady=5) tk.Label(self.master, text="密碼").grid(row=1, column=0, padx=5, pady=5) tk.Entry(self.master, textvariable=self.password, show="*").grid(row=1, column=1, padx=5, pady=5) tk.Button(self.master, text="確認", command=self.login).grid(row=2, column=0, columnspan=2, padx=5, pady=5) def login(self): if self.username.get() == "1" and self.password.get() == "1": self.master.destroy() root = tk.Tk() app = ExcelApp(root) root.mainloop() else: messagebox.showwarning("Invalid Credentials", "Username or password is incorrect.")class ExcelApp: def __init__(self, master): self.master = master master.title("Excel App") # 获取屏幕的宽度和高度 screen_width = master.winfo_screenwidth() screen_height = master.winfo_screenheight() # 将窗口的大小设置为屏幕的大小 master.geometry("%dx%d" % (screen_width, screen_height)) master.state('zoomed')#窗口最大化# 查询窗口 class QueryWindow: def __init__(self, master): self.master = master self.master.title("查詢窗口") # 获取屏幕的宽度和高度 screen_width = self.master.winfo_screenwidth() screen_height = self.master.winfo_screenheight() # 将窗口的大小设置为屏幕的大小 self.master.geometry("%dx%d" % (screen_width, screen_height)) self.master.state('zoomed') # 窗口最大化if __name__ == '__main__': root = tk.Tk() app = LoginWindow(root) root.mainloop()這個代碼中我有三個窗口,第一個銷毀后第三個就報return self.tk.call('wm', 'title', self._w, string) _tkinter.TclError: can't invoke "wm" command: application has been destroyed怎麽修改
时间: 2023-12-12 16:05:13 浏览: 135
LoginWindow.class
这个错误是因为第一个窗口销毁后,第三个窗口的父级窗口不存在了,所以无法进行最大化操作。解决方法是在第一个窗口销毁后,再创建第三个窗口。可以在`LoginWindow`类的`login`方法中加入创建`QueryWindow`窗口的代码,如下所示:
```
def login(self):
if self.username.get() == "1" and self.password.get() == "1":
self.master.destroy()
root = tk.Tk()
app = ExcelApp(root)
root.withdraw() # 隐藏ExcelApp窗口
query_window = tk.Toplevel() # 创建QueryWindow窗口
QueryWindow(query_window)
else:
messagebox.showwarning("Invalid Credentials", "Username or password is incorrect.")
```
这样,第一个窗口销毁后会先隐藏`ExcelApp`窗口,然后再创建`QueryWindow`窗口,避免了报错。
阅读全文