I create an object, its constructor creates a Toplevel() window. I close the window and this object stays. I don’t want that. Can i somehowe destroy it? I try to use WM_DELETE_ WINDOW and it works but my guts say that its not how it should be done. Maybe i should inherit my class (GameActionsWindowClass) from Toplevel? Would it be better?
from tkinter import * class GameActionsWindowClass: def __init__(self,**kwargs): self.ga_top = Toplevel() self.ga_top.protocol("WM_DELETE_WINDOW", self.kill) self.ga_top.title("Game Actions Panel") self.game_actions_file_button_obj = Button( self.ga_top, text="import game actions file", command=lambda : self.import_actions_file_dialog() ) self.game_actions_file_button_obj.pack() def switch_to_me(self): self.ga_top.attributes("-topmost",True) # TODO: this doesn't work self.ga_top.attributes("-topmost",False) #self.ga_top.deiconify() # ... nor this #self.ga_top.lift() # nor this self.game_actions_file_button_obj.focus_set() def import_actions_file_dialog(): pass def kill(self): self.ga_top.destroy() if hasattr(self, "ga_top"): # this returns positiver result print("i exist") self.ga_top = None class MainWindowClass: def __init__(self,tk_root_obj): self.tk_root_obj = tk_root_obj self.game_actions_panel = None self.tk_root_obj.title("Main Window") self.game_actions_panel_button_obj = Button(tk_root_obj, text="Game Actions panel", command=lambda :self.open_game_actions_panel() ) self.game_actions_panel_button_obj.pack() def open_game_actions_panel(self): if self.game_actions_panel is not None and self.game_actions_panel.ga_top is not None: self.game_actions_panel.switch_to_me() else: self.game_actions_panel = GameActionsWindowClass() # self.game_actions_panel.ga_top.protocol("WM_DELETE_WINDOW", self.kill2) def kill2(self): # i tried this, but i think its ugly solution self.game_actions_panel.ga_top.destroy() self.game_actions_panel = None # if __name__ == "__main__": tk_root_obj = Tk() main_window_obj = MainWindowClass(tk_root_obj) tk_root_obj.mainloop()
submitted by /u/domanpanda
[link] [comments]
r/learnpython I create an object, its constructor creates a Toplevel() window. I close the window and this object stays. I don’t want that. Can i somehowe destroy it? I try to use WM_DELETE_ WINDOW and it works but my guts say that its not how it should be done. Maybe i should inherit my class (GameActionsWindowClass) from Toplevel? Would it be better? from tkinter import * class GameActionsWindowClass: def __init__(self,**kwargs): self.ga_top = Toplevel() self.ga_top.protocol(“WM_DELETE_WINDOW”, self.kill) self.ga_top.title(“Game Actions Panel”) self.game_actions_file_button_obj = Button( self.ga_top, text=”import game actions file”, command=lambda : self.import_actions_file_dialog() ) self.game_actions_file_button_obj.pack() def switch_to_me(self): self.ga_top.attributes(“-topmost”,True) # TODO: this doesn’t work self.ga_top.attributes(“-topmost”,False) #self.ga_top.deiconify() # … nor this #self.ga_top.lift() # nor this self.game_actions_file_button_obj.focus_set() def import_actions_file_dialog(): pass def kill(self): self.ga_top.destroy() if hasattr(self, “ga_top”): # this returns positiver result print(“i exist”) self.ga_top = None class MainWindowClass: def __init__(self,tk_root_obj): self.tk_root_obj = tk_root_obj self.game_actions_panel = None self.tk_root_obj.title(“Main Window”) self.game_actions_panel_button_obj = Button(tk_root_obj, text=”Game Actions panel”, command=lambda :self.open_game_actions_panel() ) self.game_actions_panel_button_obj.pack() def open_game_actions_panel(self): if self.game_actions_panel is not None and self.game_actions_panel.ga_top is not None: self.game_actions_panel.switch_to_me() else: self.game_actions_panel = GameActionsWindowClass() # self.game_actions_panel.ga_top.protocol(“WM_DELETE_WINDOW”, self.kill2) def kill2(self): # i tried this, but i think its ugly solution self.game_actions_panel.ga_top.destroy() self.game_actions_panel = None # if __name__ == “__main__”: tk_root_obj = Tk() main_window_obj = MainWindowClass(tk_root_obj) tk_root_obj.mainloop() submitted by /u/domanpanda [link] [comments]
I create an object, its constructor creates a Toplevel() window. I close the window and this object stays. I don’t want that. Can i somehowe destroy it? I try to use WM_DELETE_ WINDOW and it works but my guts say that its not how it should be done. Maybe i should inherit my class (GameActionsWindowClass) from Toplevel? Would it be better?
from tkinter import * class GameActionsWindowClass: def __init__(self,**kwargs): self.ga_top = Toplevel() self.ga_top.protocol("WM_DELETE_WINDOW", self.kill) self.ga_top.title("Game Actions Panel") self.game_actions_file_button_obj = Button( self.ga_top, text="import game actions file", command=lambda : self.import_actions_file_dialog() ) self.game_actions_file_button_obj.pack() def switch_to_me(self): self.ga_top.attributes("-topmost",True) # TODO: this doesn't work self.ga_top.attributes("-topmost",False) #self.ga_top.deiconify() # ... nor this #self.ga_top.lift() # nor this self.game_actions_file_button_obj.focus_set() def import_actions_file_dialog(): pass def kill(self): self.ga_top.destroy() if hasattr(self, "ga_top"): # this returns positiver result print("i exist") self.ga_top = None class MainWindowClass: def __init__(self,tk_root_obj): self.tk_root_obj = tk_root_obj self.game_actions_panel = None self.tk_root_obj.title("Main Window") self.game_actions_panel_button_obj = Button(tk_root_obj, text="Game Actions panel", command=lambda :self.open_game_actions_panel() ) self.game_actions_panel_button_obj.pack() def open_game_actions_panel(self): if self.game_actions_panel is not None and self.game_actions_panel.ga_top is not None: self.game_actions_panel.switch_to_me() else: self.game_actions_panel = GameActionsWindowClass() # self.game_actions_panel.ga_top.protocol("WM_DELETE_WINDOW", self.kill2) def kill2(self): # i tried this, but i think its ugly solution self.game_actions_panel.ga_top.destroy() self.game_actions_panel = None # if __name__ == "__main__": tk_root_obj = Tk() main_window_obj = MainWindowClass(tk_root_obj) tk_root_obj.mainloop()
submitted by /u/domanpanda
[link] [comments]