wxPython表单建立在基于sqlite3数据的计时器间隔上



我正在创建一个简单的GUI,用于通过读取sqlite数据库为GUI中的按钮着色和添加文本来更新进程的状态。表单创建正确,我可以根据按钮事件更新数据库,但我不知道如何定期读取数据库中的这些更改,并根据这些更改更新GUI。

我现在已经在我的代码中添加了while True和time.sleep方法,我认为它应该在哪里,但现在我添加了它,表单就永远不会构建了。我没有在下面的例子中包括产品列表,因为它很长。有人知道如何根据sqlite值不断更新GUI吗?

import wx
products = []
import sqlite3 as lite
import time

con = lite.connect('test2.db')
with con:
    cur = con.cursor()
    ##creates table if one is not present
    cur.execute("CREATE TABLE IF NOT EXISTS Products_Settle (Name TEXT, Status TEXT, Date DATE)")
    current_date = time.strftime("%d/%m/%y")
    current_day = current_date[0:2]
    current_month = current_date[3:5]

##if new day dump the table and create new
cur.execute("SELECT Date FROM Products_Settle")
Date = cur.fetchone()
Date = str(Date)
Date = Date[3:11]
table_month = Date[3:5]
table_day = Date[0:2]


if current_month > table_month:
    cur.execute("DROP TABLE Products_Settle")
    cur.execute("CREATE TABLE Products_Settle(Name TEXT, Status TEXT, Date DATE)")
    ##on new day creates new table with status for all products set to U and new dat
    for i in products:
        name = i
        status = "U"
        date = time.strftime("%d/%m/%y")
        cur.execute("INSERT INTO Products_Settle VALUES (?, ?, ?)",  (name, status, date))
elif current_day > table_day:
    cur.execute("DROP TABLE Products_Settle")
    cur.execute("CREATE TABLE Products_Settle(Name TEXT, Status TEXT, Date DATE)")
    ##on new day creates new table with status for all products set to U and new dat
    for i in products:
        name = i
        status = "U"
        date = time.strftime("%d/%m/%y")
        cur.execute("INSERT INTO Products_Settle VALUES (?, ?, ?)",  (name, status, date))
##send to database
con.commit()




class MyForm(wx.Frame):
    while True:
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, "Settlement Status", size=(200, 940))
            panel = wx.Panel(self, wx.ID_ANY)





            vertical = 0
            horizontal = 0
            Prev_product = ""




            for i in products:

                with con:


                     if products.index(i) == 0:
                         Prev_product = i
                         button = wx.Button(panel, id=wx.ID_ANY, label=" ", pos=(horizontal, vertical), name = i)
                         sizer = wx.BoxSizer(wx.VERTICAL)
                         self.buildButtons(button, sizer)
                         name1 = (button.GetName(),)
                         cur = con.cursor()
                         cur.execute("SELECT Status FROM Products_Settle WHERE Name = (?)", (name1))
                         Status = cur.fetchone()
                         if '%s' % (Status) == "U":
                            button.SetLabel("U")
                            button.SetBackgroundColour('grey')
                         elif '%s' % (Status) == "P":
                            button.SetLabel("P")
                            button.SetBackgroundColour('green')
                         elif '%s' % (Status) == "R":
                            button.SetLabel("R")
                            button.SetBackgroundColour('red')

                     elif i[:2] == Prev_product[:2]:
                        shift = 75
                        horizontal += shift
                        Prev_product = i
                        button = wx.Button(panel, id=wx.ID_ANY, label=" ", pos=(horizontal, vertical), name = i)
                        sizer = wx.BoxSizer(wx.VERTICAL)
                        self.buildButtons(button, sizer)
                        name1 = (button.GetName(),)
                        cur = con.cursor()
                        cur.execute("SELECT Status FROM Products_Settle WHERE Name = (?)", (name1))
                        Status = cur.fetchone()
                        if '%s' % (Status) == "U":
                            button.SetLabel("U")
                            button.SetBackgroundColour('grey')
                        elif '%s' % (Status) == "P":
                            button.SetLabel("P")
                            button.SetBackgroundColour('green')
                        elif '%s' % (Status) == "R":
                            button.SetLabel("R")
                            button.SetBackgroundColour('red')

                     else:
                        horizontal = 0
                        shift = 40
                        vertical += shift
                        Prev_product = i
                        button = wx.Button(panel, id=wx.ID_ANY, label=" ", pos=(horizontal, vertical), name = i)
                        sizer = wx.BoxSizer(wx.VERTICAL)
                        self.buildButtons(button, sizer)
                        name1 = (button.GetName(),)
                        cur = con.cursor()
                        cur.execute("SELECT Status FROM Products_Settle WHERE Name = (?)", (name1))
                        Status = cur.fetchone()
                        if '%s' % (Status) == "U":
                            button.SetLabel("U")
                            button.SetBackgroundColour('grey')
                        elif '%s' % (Status) == "P":
                            button.SetLabel("P")
                            button.SetBackgroundColour('green')
                        elif '%s' % (Status) == "R":
                            button.SetLabel("R")
                            button.SetBackgroundColour('red')
        time.sleep(15)





    #----------------------------------------------------------------------
    def buildButtons(self, btn, sizer):
        """"""
        btn.Bind(wx.EVT_BUTTON, self.onButton)
        sizer.Add(btn, 0, wx.ALL, 5)

    #----------------------------------------------------------------------
    def onButton(self, event):
        """
        This method is fired when its corresponding button is pressed
        """
        button = event.GetEventObject()

        ##button clicks update db Status
        with con:
            cur = con.cursor()
            name2 = (button.GetName(),)
            cur.execute("SELECT Status FROM Products_Settle WHERE Name = (?)", (name2))
            Status = cur.fetchone()
            if '%s' % (Status) == "U":
                cur.execute("UPDATE Products_Settle SET Status = 'P' WHERE Name = (?)", (name2))

            elif '%s' % (Status) == "P":
                cur.execute("UPDATE Products_Settle SET Status = 'R' WHERE Name = (?)", (name2))
            elif '%s' % (Status) == "R":
                cur.execute("UPDATE Products_Settle SET Status = 'U' WHERE Name = (?)", (name2))
            con.commit()

# Run the program


if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm()
        frame.Show()
        app.MainLoop()
items = itertools.cycle(["APPLE","ORANGE","PEAR","PLUM","PLUOT","MELON","CHERRY","PEACH"])
class MyForm(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,-1,"A form title")
        self.label = wx.StaticText(self,-1,"A Label That Updates")
        self.UpdateFunc(None)
    def UpdateFunc(self,event):
        self.label.SetLabel(next(items))
        # you would update your labels with values from sqlite here...
        wx.CallLater(1000,self.UpdateFunc) # schedule a new call for one second later

您需要以非阻塞的方式更新GUI。。。这可能是最简单的。。。这有一个小好处(相对于wx.Timer),即第二个延迟在您完成功能后开始,从而确保您永远不会中断自己的

最新更新