Click here to Skip to main content
15,943,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

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.

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.

What I have tried:

Hi, I've tried it but when I change the tab (notebook) 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), y=10, width=160, height=40)
        self.variables_globales = VariablesGlobales()
        def cargar_datos():
            self.connection3 = connect_to_database()
            self.cursor3 = self.connection3.cursor()
            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")), 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.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"))
        self.update_thread = threading.Thread(target=cargar_datos)
Updated 13-Aug-23 23:31pm
Richard MacCutchan 14-Aug-23 4:01am    
Without seeing your code it is impossible to provide any useful suggestions.
Tomas Sanchez Garcia 14-Aug-23 4:40am    
Sorry, already updated. More than anything I was trying to get an idea.
Richard MacCutchan 14-Aug-23 5:44am    
There is nothing in the code or description that helps to determine what may be the issue. I suggest ypu take a look at Tkinter Notebook Widget[^] to see if that offers any clues.
Richard MacCutchan 14-Aug-23 8:13am    
I have created a simple example with two TreeViews in a Notebook and it works correctly. If I delete an item from both tabs and then switch tabs, the updated tree is visible.
[no name] 14-Aug-23 13:15pm    
Same data (concurrent collection); different trees (views).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900