在 table 中更新数据而不创建全新的 table

Updating data within a table without creating a whole new table

所以我正在制作一个使用选项卡菜单显示两个 tables 的程序,我希望 tables 在您切换时刷新并从我的 SQLite 数据库加载不同的数据集选项卡。问题是我无法让它刷新,要么是由于全局变量,要么是因为子程序,程序的实际 SQLite 编码部分不是问题。

我主要尝试弄乱不同的变量并四处移动,因为我并不真正理解这个问题,而且我已经设法让自己非常困惑。

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

抱歉代码这么乱,这是我目前正在编辑的程序部分,这个 GUI 只是一个庞大程序的一部分

给出的代码没有给出任何错误,而是没有更新 table 我想要的方式,它应该更新以在两个选项卡上给我一个不同的 table 但是我得到了两个选项卡上的相同 table。

对于遇到这种 post 外观或解决方案的任何人,我确实设法解决了问题,而不是尝试使用 class 的相同实例,我将实例变成了两个 classes 使用以下代码行:

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

然后使用这两个实例我调用了正确的函数以允许将不同的数据集加载到两个不同的实例中:

    OrderApp.Load()
    HistoryApp.LoadHist()

简而言之,我所做的只是没有使用相同的 table 并更新 table 的内容,而是使用相同的 class 制作了两个单独的 table并将它们存储到一个变量中,然后从 class 中调用两个不同的函数来加载两组不同的数据。