try: int(entry.get()) except TypeError: # handle the error
import tkinter as tk class Main(tk.Frame): def __init__(self, master=None, **kwargs): super().__init__(master, **kwargs) lbl = tk.Label(self, text="this is the main frame\n") lbl.pack() btn = tk.Button(self, text='click me', command=self.open_popup) btn.pack() self.output = tk.Label(self) self.output.pack() def open_popup(self): print("runs before the popup") p = Popup(self) print("runs after the popup closes") self.output.config(text=f"Got data returned:\n {p.result!r}") class Popup(tk.Toplevel): """modal window requires a master""" def __init__(self, master, **kwargs): super().__init__(master, **kwargs) lbl = tk.Label(self, text="this is the modal window popup\ntype something") lbl.pack() self.ent = tk.Entry(self) self.ent.insert(0, "Hello world") self.ent.pack() btn = tk.Button(self, text="OK", command=self.on_ok) btn.pack() # The following commands keep the popup on top. # Remove these if you want a program with 2 responding windows. # These commands must be at the end of __init__ self.transient(master) # set to be on top of the main window self.grab_set() # hijack all commands from the master (clicks on the main window are ignored) master.wait_window(self) # pause anything on the main window until this one closes def on_ok(self): self.result = self.ent.get() # save the return value to an instance variable. self.destroy() def main(): root = tk.Tk() window = Main(root) window.pack() root.mainloop() if __name__ == '__main__': main()
import tkinter as tk class Main(tk.Frame): def __init__(self, master=None, **kwargs): super().__init__(master, **kwargs) lbl = tk.Label(self, text="this is the main frame\n") lbl.pack() btn = tk.Button(self, text='click me', command=self.open_popup) btn.pack() self.output = tk.Label(self) self.output.pack() def open_popup(self): Popup(self, self.update_main) # send the method that should be called at the end def update_main(self, result): self.output.config(text=f"Got data returned:\n {result!r}") class Popup(tk.Toplevel): def __init__(self, master, callback, **kwargs): super().__init__(master, **kwargs) self.callback = callback lbl = tk.Label(self, text="this is the modal window popup\ntype something") lbl.pack() self.ent = tk.Entry(self) self.ent.insert(0, "Hello world") self.ent.pack() btn = tk.Button(self, text="OK", command=self.on_ok) btn.pack() def on_ok(self): self.callback(self.ent.get()) # send the back to the other class. self.destroy() def main(): root = tk.Tk() window = Main(root) window.pack() root.mainloop() if __name__ == '__main__': main()
import tkinter as tk class Main(tk.Frame): def __init__(self, master=None, **kwargs): super().__init__(master, **kwargs) lbl = tk.Label(self, text="this is the main frame\n") lbl.pack() btn = tk.Button(self, text='click me', command=self.open_popup) btn.pack() self.txtvar = tk.StringVar(value="Hello World") self.output = tk.Label(self, textvariable=self.txtvar) self.output.pack() def open_popup(self): Popup(self, self.txtvar) class Popup(tk.Toplevel): def __init__(self, master, txtvar, **kwargs): super().__init__(master, **kwargs) lbl = tk.Label(self, text="this is the modal window popup\ntype something") lbl.pack() self.ent = tk.Entry(self, textvariable=txtvar) # could also use txtvar.set() in the ok method. self.ent.pack() btn = tk.Button(self, text="OK", command=self.destroy) btn.pack() def main(): root = tk.Tk() window = Main(root) window.pack() root.mainloop() if __name__ == '__main__': main()
import tkinter as tk data_structure = "" # any kind of data structure class Main(tk.Frame): def __init__(self, master=None, **kwargs): super().__init__(master, **kwargs) lbl = tk.Label(self, text="this is the main frame\n") lbl.pack() btn = tk.Button(self, text='click me', command=self.open_popup) btn.pack() self.output = tk.Label(self) self.output.pack() master.bind("<<mcoombesEvent>>", self.update_main) def open_popup(self): Popup(self, self.update_main) def update_main(self, event=None): self.output.config(text=f"Got data returned:\n {data_structure!r}") class Popup(tk.Toplevel): def __init__(self, master, callback, **kwargs): super().__init__(master, **kwargs) self.callback = callback lbl = tk.Label(self, text="this is the modal window popup\ntype something") lbl.pack() self.ent = tk.Entry(self) self.ent.insert(0, "Hello world") self.ent.pack() btn = tk.Button(self, text="OK", command=self.on_ok) btn.pack() def on_ok(self): global data_structure data_structure = self.ent.get() # save the data. self.master.event_generate("<<mcoombesEvent>>") # trigger actions elsewhere self.destroy() def main(): root = tk.Tk() window = Main(root) window.pack() root.mainloop() if __name__ == '__main__': main()
import tkinter as tk from queue import Queue class Main(tk.Frame): def __init__(self, master=None, **kwargs): super().__init__(master, **kwargs) self.data_structure = Queue() lbl = tk.Label(self, text="this is the main frame\n") lbl.pack() btn = tk.Button(self, text='click me', command=self.open_popup) btn.pack() self.output = tk.Label(self) self.output.pack() self.event_monitor() # start the loop to monitor for new data def open_popup(self): Popup(self) def event_monitor(self): if not self.data_structure.empty(): result = self.data_structure.get() self.output.config(text=f"Got data returned:\n {result!r}") self.after(100, self.event_monitor) class Popup(tk.Toplevel): """modal window requires a master""" def __init__(self, master, **kwargs): super().__init__(master, **kwargs) lbl = tk.Label(self, text="this is the modal window popup\ntype something") lbl.pack() self.ent = tk.Entry(self) self.ent.insert(0, "Hello world") self.ent.pack() btn = tk.Button(self, text="OK", command=self.on_ok) btn.pack() def on_ok(self): self.master.data_structure.put(self.ent.get()) # send the data to the queue self.destroy() def main(): root = tk.Tk() window = Main(root) window.pack() root.mainloop() if __name__ == '__main__': main()
import tkinter as tk class Main(tk.Frame): def __init__(self, master=None, **kwargs): super().__init__(master, **kwargs) lbl = tk.Label(self, text="this is the main frame\n") lbl.pack() btn = tk.Button(self, text='click me', command=self.open_popup) btn.pack() self.txtvar = tk.StringVar(value="Hello World") self.txtvar.trace_add("write", self.update_main) self.output = tk.Label(self) self.output.pack() def open_popup(self): print("runs before the popup") Popup(self, self.txtvar) print("runs after the popup closes") def update_main(self, *event): self.output.config(text=f"Got data returned:\n {self.txtvar.get()!r}") class Popup(tk.Toplevel): def __init__(self, master, txtvar, **kwargs): super().__init__(master, **kwargs) lbl = tk.Label(self, text="this is the modal window popup\ntype something") lbl.pack() self.ent = tk.Entry(self, textvariable=txtvar) # or use txtvar.set() from the ok button callback self.ent.pack() btn = tk.Button(self, text="OK", command=self.destroy) btn.pack() def main(): root = tk.Tk() window = Main(root) window.pack() root.mainloop() if __name__ == '__main__': main()