前提・実現したいこと
python tkinterを利用してメイン画面⇒子画面⇒孫画面と遷移しております。
孫画面に閉じるボタンを作成し孫画面と子画面を同時に閉じたい。
ご教授の程よろしくお願い致します。
発生している問題・エラーメッセージ
孫画面については Toplevel.destroy(self.master) で閉じることは出来ているのですが 子画面は閉じることが出来ません。 print(self) print(self.master) を実施したところ .!toplevel2.!correctwindow .!toplevel と表示されました。
該当のソースコード
python
1#親ウィンドウ生成(メイン画面) 2class Application(tk.Frame): 3 def __init__(self,master): 4 super().__init__(master) 5 self.pack() 6 self.master.title("テスト") 7 self.master.geometry("550x280+500+350") 8 self.mainwidget() 9 self.mainwindow = [] 10 self.subwindow = [] 11 style = ttk.Style() 12 style.theme_use("default") 13 style.map("Treeview") 14 15 #子ウィンドウ生成指示(検索画面生成指示) 16 def search_window(self): 17 self.mainwindow.append(tk.Toplevel(self.master, highlightthickness=5, highlightbackground="white", highlightcolor="red")) 18 self.subwindow.append(SubWindow(self.mainwindow[len(self.mainwindow)-1],len(self.mainwindow))) 19 #子ウィンドウ生成ボタン 20 def mainwidget(self): 21 self.btn13 = ttk.Button(self.master, text="検索", width=10, state="enable", command=self.search_window) 22 self.btn13.place(x=10, y=10) 23 24#子ウィンドウ生成(検索画面) 25class SubWindow(tk.Frame): 26 def __init__(self,master,num): 27 super().__init__(master) 28 self.pack() 29 master.geometry("550x280+500+350") 30 master.title("検索画面") 31 master.grab_set() 32 self.sub_widget() 33 self.subwindow = [] 34 self.CorrectWindow = [] 35 36 def sub_widget(self): 37 self.btn13 = ttk.Button(self.master, text="修正", width=10, state="enable", command=self.rep_window) 38 self.btn13.place(x=10, y=10) 39 40 #修正画面表示 41 def rep_window(self): 42 self.subwindow.append(tk.Toplevel(self.master, highlightthickness=5, highlightbackground="white", highlightcolor="red")) 43 self.CorrectWindow.append(CorrectWindow(self.subwindow[len(self.subwindow)-1],len(self.subwindow))) 44 45 #こちらの記載が有効にならない状況です。 46 win = tk.Toplevel(self.master, ...略...) 47 win.bind("<<Close>>", lambda e: self.master.destroy()) 48 self.subwindow.append(win) 49 50class CorrectWindow(tk.Frame): 51 def __init__(self,master,num): 52 super().__init__(master) 53 self.pack() 54 master.geometry("550x250+500+350") 55 master.title("修正画面") 56 self.correct_widget() 57 master.grab_set() 58 59 def correct_widget(self): 60 self.correct_btn14 = ttk.Button(self.master, text="キャンセル", width=10, state="enable", command=self.onclick) 61 self.correct_btn14.place(x=10, y=10) 62 63 def onclick(self): 64 self.master.event_generate("<<Close>>") 65 self.correct_btn14.bind("<1>", onclick) 66 self.correct_btn14.pack() 67 print(self.master) 68 69def main(): 70 win = tk.Tk() 71 app = Application(win) 72 app.mainloop() 73 74if __name__ == '__main__': 75 main() 76 77

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/03/10 04:01
2022/03/10 07:27 編集
2022/03/11 03:35
2022/03/11 07:45
2022/03/14 10:06
2022/03/15 03:28
2022/03/16 04:07 編集
2022/03/15 06:43
2022/03/16 02:08
2022/03/16 02:35