我正在创建一个简单的GUI,用于通过读取sqlite db为GUI中的按钮着色和添加文本来更新进程的状态。表单是正确创建的,我可以根据按钮事件更新数据库,但是我无法弄清楚如何定期读取数据库的这些更改并根据这些更改更新GUI。
我现在在我的代码中添加了while和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()
答案 0 :(得分:1)
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),你完成功能后第二次延迟开始,从而确保你永远不要打扰自己