2016-04-11 9 views
0

我正在創建一個簡單的GUI,用於通過讀取sqlite數據庫着色並向GUI中的按鈕添加文本來更新進程的狀態。窗體創建正確,我可以根據按鈕事件更新數據庫,但我無法弄清楚如何定期讀取數據庫的這些更改,並根據這些更改更新GUI。基於sqlite3數據建立在定時器間隔上的wxPython表單

我現在在我的代碼中添加了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() 
+1

這是一個移動用戶閱讀的很多代碼。你看過wxpython事件計時器嗎? http://stackoverflow.com/questions/10486500/wxpython-timer-event-interval – Torxed

回答

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)您完成該功能,從而確保您永遠不會中斷自己

相關問題