model.html


001| import tkinter as tk
002| 
003| class ModalDialog(tk.Toplevel):
004|     def __init__(self, parent):
005|         super().__init__(parent)
006|         self.parent = parent
007|         self.title('Launch Platform')
008|         self.result = False
009| 
010|         # Build UI
011|         self.wndw = tk.Frame(self)
012|         self.wndw.pack(padx=3, pady=3)
013| 
014|         self.txt_mesg = tk.Label(self.wndw, text="Hello")
015|         self.btn_okay = tk.Button(self.wndw, text="OK", command=self.okay)
016|         self.btn_quit = tk.Button(self.wndw, text="Cancel", command=self.quit)
017| 
018|         self.txt_mesg.pack(side='top', fill='both', expand=True, padx=5, pady=5)
019|         self.btn_quit.pack(side='right')
020|         self.btn_okay.pack(side='right')
021| 
022|         # Modal setup
023|         self.transient(parent)
024|         self.protocol("WM_DELETE_WINDOW", self.quit)
025|         self.withdraw()
026|         self.update_idletasks()
027| 
028|         # Placement with full control
029|         x = parent.winfo_rootx() + 20
030|         y = parent.winfo_rooty() + 10
031|         self.geometry(f"+{x}+{y}")
032| 
033|         self.deiconify()
034|         self.grab_set()
035| 
036|         # Block until this window closes
037|         self.wait_window(self)
038| 
039|     def okay (self):
040|         self.result = True
041|         self.quit()
042| 
043|     def quit (self):
044|         self.grab_release()
045|         self.destroy()
046| 
047| def open_dialog ():
048|     retv = ModalDialog(root)
049|     print(f'{retv.result=}')
050| 
051| root = tk.Tk()
052| root.geometry("600x400+200+150")
053| cmnd = tk.Button(root, text='Open Dialog', command=open_dialog)
054| cmnd.pack(anchor='nw')
055| root.mainloop()
056|