Create a 'close/exit' option for a child frame that is created using tkinter in python -
i have created script using tkinter, brings child frame when press button. frame occupies full screen size on mac laptop. need create option exit/ or close new frame. best option that?
-thanks
from tkinter import * import tkfont class app(frame): def __init__(self, *args, **kwargs): frame.__init__(self, *args, **kwargs) self.apple = button(self, text="apple", command=self.write_apple) self.apple.pack(side=left) def write_apple(self): self.customfont = tkfont.font(family="helvetica", size=80) t = toplevel(self) t.overrideredirect(true) t.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight())) l = label(t, text="this green apple.",font=self.customfont) l.pack(side="top", fill="both", expand=true) if __name__ == "__main__": root = tk() main = app(root) main.pack(side="top", fill="both", expand=true) root.mainloop()
this solution handles case multiple instances of toplevel
created:
from tkinter import * import tkfont class app(frame): def __init__(self, *args, **kwargs): frame.__init__(self, *args, **kwargs) self.apple = button(self, text="apple", command=self.write_apple) self.apple.pack(side=left) self.top_dict = dict() def destroy_top(self, event): # event.widget instance of label clicked # instance of toplevel top = self.top_dict[event.widget] # destroy instance of toplevel top.destroy() # remove instance of toplevel list del self.top_dict[event.widget] def write_apple(self): self.customfont = tkfont.font(family="helvetica", size=80) # create instance of toplevel top = toplevel(self) top.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight())) label = label(top, text="this green apple.",font=self.customfont) label.pack(side="top", fill="both", expand=true) # bind destroy_top method mouse-button-1 click label.bind('<button-1>', self.destroy_top) # save instance of toplevel using label key self.top_dict[label] = top if __name__ == "__main__": root = tk() main = app(root) main.pack(side="top", fill="both", expand=true) root.mainloop()
note: @bryan oakley noted, removing call overrideredirect
adds window decorations instance of toplevel
.
Comments
Post a Comment