Python Forum
Doubt approach update 2 Treeview at the same time
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Doubt approach update 2 Treeview at the same time
#1
Hello,

I would like some guidance on a question / problem I have.

I'm doing a task that consists of an inventory system, things so far very well.

I have made a tkinter window with a "notebook" menu, (inventory/sale)

In notebook 1 I have done a "treeview" that shows mysql data, and buttons to add, delete and update product, so far everything is perfect.

The question I have now is when creating "notebook 2" (sales), that I will make another Treeview and show the same data, but my question is how can I update the "2 Treeviews" at the same time. Because only the one I interacted with is updated, and the other treeview is updated when I interact or restart the application.

Is there any way or way to be able to update the two Treeview at the same time?

I have thought about after(), but I don't know if it is a good option or there is another option.

Any additional information would be appreciated.
Reply
#2
Can you see both views at the same time? If not, only update the visible view. The hidden view should update when it becomes visible. This should be automatic if you set up the table models correctly.
Reply
#3
Hi, I've tried it but when I change the tab it doesn't update, I have to close and open the program again to see the update.

I'm trying after() but still the same

from tkinter import *
from tkinter import ttk
import tkinter as tk
import pymysql
from conexion import *
from tkinter import messagebox
from PIL import Image, ImageTk
from globales import VariablesGlobales
import threading

class Menuventas:
    def __init__(self, ventas):
        self.ventas = ventas

        self.boton = ttk.Button(self.ventas.tab3, text="Haz clic")
        #self.boton.pack(padx=10, pady=10)
        #self.boton.grid(row=4, column=0, padx=50, pady=50)
        self.boton.place(x=10, y=10, width=160, height=40)

        self.variables_globales = VariablesGlobales()

        def cargar_datos():
            self.connection3 = connect_to_database()
            self.cursor3 = self.connection3.cursor()

            print(self.variables_globales.variable_global1)

            self.cursor3.execute("SELECT * FROM almacen WHERE id_usuario = '"+self.variables_globales.variable_global1+"' ORDER BY id DESC")  # Reemplaza 'tabla' con el nombre de tu tabla
            self.datos_treeview = self.cursor3.fetchall()

            for self.colun in self.datos_treeview:
                self.c_id = self.colun[0]
                self.c_producto = self.colun[1]
                self.c_precio_compra = self.colun[2]
                self.c_precio_venta = self.colun[3]
                self.c_stock = self.colun[4]
                self.c_proveedor = self.colun[5]
                self.c_categoria = self.colun[6]

                self.treeview_almacen.insert("", "end", values=(self.c_id, self.c_producto, self.c_precio_compra, self.c_precio_venta, self.c_stock, self.c_proveedor, self.c_categoria))
                
                #self.treeview_almacen.bind("<<TreeviewSelect>>", self.mostrar_seleccion
                #self.treeview_almacen.bind("<<TreeviewSelect>>", self.mostrar_seleccion)
                
            self.treeview_almacen.after(1000, cargar_datos)


        #Listado productos

        self.treeview_almacen = ttk.Treeview(self.ventas.tab3, columns=("codigo","producto", "precio_compra", "precio_venta", "stock", "proovedor", "categoria"))
        self.treeview_almacen.place(x=20, y=210, width=1085, height=340)
        
        # Configurar encabezados de columnas
        self.treeview_almacen.heading("#0", text="")
        self.treeview_almacen.heading("codigo", text="CÓDIGO")
        self.treeview_almacen.heading("producto", text="PRODUCTO")
        self.treeview_almacen.heading("precio_compra", text="PRECIO COMPRA")
        self.treeview_almacen.heading("precio_venta", text="PRECIO VENTA")
        self.treeview_almacen.heading("stock", text="STOCK")
        self.treeview_almacen.heading("proovedor", text="PROVEEDOR")
        self.treeview_almacen.heading("categoria", text="CATEGORÍA")

        #self.estilo = ttk.Style()
        #self.estilo.configure("Treeview.Cell", anchor="center")  # Centrar contenido
        
        # Ajustar el ancho de las columnas
        self.treeview_almacen.column("#0", width=0)
        self.treeview_almacen.column("codigo", width=100)
        self.treeview_almacen.column("producto", width=200)
        self.treeview_almacen.column("precio_compra", width=50)
        self.treeview_almacen.column("precio_venta", width=50)
        self.treeview_almacen.column("stock", width=40)
        self.treeview_almacen.column("proovedor", width=130)
        self.treeview_almacen.column("categoria", width=100)

        self.scrollbar_y = tk.Scrollbar(self.ventas.tab3, orient="vertical", command=self.treeview_almacen.yview)
        self.treeview_almacen.configure(yscrollcommand=self.scrollbar_y.set)

        self.scrollbar_y.pack(side="right", fill="y")

        #self.treeview_almacen.configure(yscrollcommand=self.vertical_scrollbar.set, xscrollcommand=self.horizontal_scrollbar.set)

        #self.treeview_almacen.insert("", "end", text="1", values=("Producto A", 10, "tonto"))

        cargar_datos()

        self.update_thread = threading.Thread(target=cargar_datos)
        self.update_thread.start()
photo:

[Image: jyxnA6D.png]

[Image: NOxBYPd.png]
Reply
#4
FYI:
  • Get a copy of John Shipman's documentation here
    It's the original tkinter manual from New Mexico Tech.
    It's old, but there's stuff in there that you won't find in other documents.
  • read Section 26 "Universal widget methods" read entire section, but especially w.update_idletasks() on page 103.
  • Because you are using threads, you may have to implement some sort of semiphore.
    if not familiar with semiphores, The little book of semiphores.
Reply
#5
(Aug-13-2023, 11:43 PM)Larz60+ Wrote: FYI:
  • Get a copy of John Shipman's documentation here
    It's the original tkinter manual from New Mexico Tech.
    It's old, but there's stuff in there that you won't find in other documents.
  • read Section 26 "Universal widget methods" read entire section, but especially w.update_idletasks() on page 103.
  • Because you are using threads, you may have to implement some sort of semiphore.
    if not familiar with semiphores, The little book of semiphores.

Hi, thank you for you answer.

I try to access the link that Jhon Shipman provides me and it is down
Reply
#6
try here
Other useful links here
Reply
#7
I mixed up tkinter and Qt. Qt has a data model that works with QTableView to automatically update the table view when the table data changes, or the table is drawn.

This is a good idea, and just because it is not directly supported by tkinter doesn't mean you shouldn't do it.
1. Create a table class that contains the data you share amongst your pages.
2. Create a table view class that displays the data in a table.
3. Attach an instance of the table class to an instance of the table view class so it knows where to get the info needed to update the table.
4. Add table view instances to a list of views in the table class. When the table data is modified, send a message to each of the views in the list.

Your cargar_datos() method is a good start for the table class (step 1). Convert this to a class where you can set table values, get table values, save to database, retrieve from database. Setting a table value or retrieving from a database will send update messages to all the interested pages.

Menuventas looks like a good start for a table view class. Make this a subclass of tk.TableView. Add an attribute for the table data and an update message that updates the view using information from the table data. (step 2)
Reply
#8
mixing tkinter and Qt is a good idea that I never thought of
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter - update/refresh treeview snakes 5 21,278 Dec-02-2023, 07:05 PM
Last Post: aynous19
  [Tkinter] Update label if there are no records in treeview TomasSanchexx 1 988 Aug-20-2023, 04:45 PM
Last Post: menator01
  [Tkinter] [split] Is there a way to embed a treeview as a row inside another treeview? CyKlop 5 3,457 Oct-20-2021, 12:14 AM
Last Post: CyKlop

Forum Jump:

User Panel Messages

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