May-12-2021, 02:34 PM
The View class adds an expose event to Frame. The expose_event() method is called when the View becomes visible. In the example I use this to update a control in the View to some global value shared by all the views. Type a value in the entry box and <Enter>. If you change to a different tab the entry widget is updated to reflect the shared value.
import tkinter as tk from tkinter import ttk class View(tk.Frame): """A frame with an expose event""" shared_text = '' def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.bind('<Expose>', self.expose_event) self.local_text = tk.StringVar() self.entry = tk.Entry(self, textvariable=self.local_text) self.entry.pack() self.entry.bind('<Return>', self.set_value) def set_value(self, event): """Called when enter key is pressed in entry widget""" View.shared_text = self.local_text.get() def expose_event(self, event): """Called when I become visible""" self.local_text.set(View.shared_text) root = tk.Tk() notebook = ttk.Notebook(root) notebook.pack(fill=tk.BOTH, expand=tk.YES) for i in range(5): notebook.add(View(notebook), text=str(i)) root.mainloop()This solution only works for data that may have changed while the View was not visible and must be updated when it becomes visible. This will work great for something like a notebook if the notebook is self contained; i.e. only pages in the notebook can change values displayed in other pages and only one page can be visible at a time. It may not be applicable to your problem.