Search code examples
python-3.xclassuser-interfacettk

Updating data within a table without creating a whole new table


So I am making a program that uses a tab menu to display two tables, I want the tables to refresh and load different sets of data from my SQLite database when you switch tabs. the problem is that I just cant get it to refresh, either due to global variables or because of the sub programs, the actual SQLite coding part of the program is not an issue.

I have mainly tried messing around with different variables and moving things around as I don't really understand the issue and I have managed to hugely confuse myself.

def OrderMenu():
    class App(Frame):



        def __init__(self, parent):

            Frame.__init__(self, parent)
            Frame.pack(self, fill = BOTH)
            self.CreateUI()
            self.LoadTable()
            self.grid(stick = (W))

        def CreateUI(self):
            table = Treeview(self)
            table["columns"] = ("product", "productcode", "quantity", "status")
            table.column("#0", anchor = "w", width = 0)
            table.heading("product" , text ="Name")
            table.column("product" , anchor = "center" , width = 200)
            table.heading("productcode", text = "Item Name")
            table.column("productcode", anchor = "center" , width = 200)
            table.heading("quantity", text = "Amount")
            table.column("quantity", anchor = "center" , width = 200)
            table.heading("status", text = "Status",)
            table.column("status", anchor = "center" , width = 200)
            table.grid(stick = (N,S,W,E))
            self.treeview = table
            self.grid_rowconfigure(0, weight = 1)
            self.grid_columnconfigure(0, weight = 1)

        def LoadTable(self):
            def Load():
                if Type == "Normal":
                    conn = sqlite3.connect("Order.db")

                    cursor = conn.execute("SELECT Name, Item, Amount, Status FROM OrdersTab WHERE Name = ?"(Username))
                    for row in cursor:
                        self.treeview.insert("", "end", values = (row[0], row[1], row[2], row[3]))
                elif Type == "Admin":
                    conn = sqlite3.connect("Order.db")

                    cursor = conn.execute("SELECT Name, Item, Amount, Status FROM OrdersTab ")
                    for row in cursor:
                        self.treeview.insert("", "end", values = (row[0], row[1], row[2], row[3]))

            def LoadHist():
                if Type == "Normal":
                    conn = sqlite3.connect("Order.db")

                    cursor = conn.execute("SELECT Name, Item, Amount, Status FROM HistoryTab WHERE Name = ?" (Username))
                    for row in cursor:
                        self.treeview.insert("", "end", values = (row[0], row[1], row[2], row[3]))

                elif Type == "Admin":
                    conn = sqlite3.connect("Order.db")

                    cursor = conn.execute("SELECT Name, Item, Amount, Status FROM HistoryTab")
                    for row in cursor:
                        self.treeview.insert("", "end", values = (row[0], row[1], row[2], row[3]))
            global X
            X = 1
            Tabid = TabID
            while X == 1:
                if Tabid == 0:
                    Load()
                    X = 0
                elif Tabid == 1:
                    LoadHist()
                    X = 0




    def Exit():
        window.destroy()
        if Type == "Normal":
            StanMain()
        elif Type == "Admin":
            AdMain()
        else:
            print("Account Error")

    window = Tk()
    window.geometry("500x500")

    def OnClick(event):
        TabID = main.index(main.select())
        X = 1

    main = ttk.Notebook(window)
    main.pack(fill = BOTH, expand = True)
    root = ttk.Frame(main)
    main.bind("<Button-1>", OnClick)
    history = ttk.Frame(main)
    main.add(root, text = "On Going Orders")
    main.add(history, text = "Order History")
    TabID = main.index(main.select())
    X = 1

    App(root).pack(fill = BOTH, expand = True)
    App(history).pack(fill = BOTH, expand = True)

    menubar = Menu(root)
    window.config(menu = menubar)
    filemenu = Menu(menubar)
    menubar.add_command(label = "Back", command = Exit)
    menubar.add_command(label = "Add Order")



    window.mainloop

sorry for the code being such a huge mess, this is the section of the program that I am currently editing, this GUI is just one section of a massive program

The code given does not give any error but instead does not update the table how I want it to, it should update to give me a different table on both tabs but instead I get the same table on both tabs.


Solution

  • Just for anyone that comes across this post looking or a solution, I did manage to fix the problem, instead of trying to use the same instance of the class I turned the instance into two classes using the following lines of code:

        HistoryApp = App(HistoryTab)
        OrderApp = App(OrderTab)
    

    then using these two instances I called the right function to allow different sets of data to be loaded into the two different instances:

        OrderApp.Load()
        HistoryApp.LoadHist()
    

    in short terms all I did was instead of using the same table and updating the contents the table, I made two separate table using the same class and stored them into a variable, then called two different functions from within class to load two different sets of data.