Python Forum
tkinter - update/refresh treeview
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter - update/refresh treeview
#4
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.
Reply


Messages In This Thread
tkinter - update/refresh treeview - by snakes - May-11-2021, 10:33 PM
RE: tkinter - update/refresh treeview - by MrTim - May-12-2021, 12:35 PM
RE: tkinter - update/refresh treeview - by MrTim - May-12-2021, 12:43 PM
RE: tkinter - update/refresh treeview - by deanhystad - May-12-2021, 02:34 PM
RE: tkinter - update/refresh treeview - by snakes - May-13-2021, 07:10 AM
RE: tkinter - update/refresh treeview - by aynous19 - Dec-02-2023, 07:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Doubt approach update 2 Treeview at the same time TomasSanchexx 7 2,044 Sep-19-2023, 01:19 AM
Last Post: SaintOtis12
  [Tkinter] Update label if there are no records in treeview TomasSanchexx 1 977 Aug-20-2023, 04:45 PM
Last Post: menator01
  [Tkinter] How to insert data json to treeview tkinter? Shakanrose 8 4,575 Jan-19-2023, 03:58 PM
Last Post: Shakanrose
  [Tkinter] Different rows colours in treeview tkinter Krissstian 1 1,317 Nov-20-2022, 09:59 PM
Last Post: woooee
  [Tkinter] About Tkinter Treeview.selection_get() usage. water 3 8,444 Feb-12-2022, 02:19 PM
Last Post: water
  Can't get tkinter database aware cascading comboboxes to update properly dford 6 3,700 Jan-11-2022, 08:37 PM
Last Post: deanhystad
  [Tkinter] [split] Is there a way to embed a treeview as a row inside another treeview? CyKlop 5 3,434 Oct-20-2021, 12:14 AM
Last Post: CyKlop
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,278 Oct-15-2021, 08:01 AM
Last Post: drSlump
  [PyQt] Refresh x-labels in matplotlib animation widget JohnT 5 3,824 Apr-23-2021, 07:40 PM
Last Post: JohnT
  [Tkinter] acceleration of data output in treeview tkinter Vladimir1984 4 4,257 Nov-21-2020, 03:43 PM
Last Post: Vladimir1984

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020