2016-04-11 2 views
0

Ich erstelle eine einfache GUI, die verwendet wird, um den Status eines Prozesses durch Ausfärben und Hinzufügen von Text zu den Schaltflächen in der GUI durch Lesen einer SQLite db zu aktualisieren. Das Formular ist korrekt erstellt und ich bin in der Lage, die db basierend auf den Button-Ereignissen zu aktualisieren, aber ich kann nicht herausfinden, wie diese Änderungen in regelmäßigen Abständen in der db gelesen werden und die GUI basierend auf diesen Änderungen aktualisieren.wxPython Formular auf Timer-Intervall basierend auf sqlite3 Daten aufgebaut

Ich habe jetzt die Methode while True und time.sleep in meinem Code hinzugefügt, wo ich glaube, dass es sein sollte, aber jetzt, da ich es hinzugefügt habe, formt das Formular nie. Ich habe die Produktliste im folgenden Beispiel nicht aufgenommen, da sie ziemlich lang ist. Hat jemand eine Idee, wie man die GUI basierend auf den SQLite-Werten ständig aktualisiert?

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

Dies ist eine Menge Code für einen mobilen Benutzer zu lesen. Haben Sie sich die wxpython Event Timer angesehen? http://stackoverflow.com/questions/10486500/wxpython-timer-event-interval – Torxed

Antwort

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 

Sie benötigen, um Ihre GUI in einer nicht-blockierenden Art und Weise zu aktualisieren ... Dies ist wahrscheinlich die einfachste ... dies hat einen geringen Vorteil (über wx.Timer), dass die zweite Verzögerung beginnt nach Sie beenden die Funktion und stellen sicher, dass Sie sich nicht für immer unterbrechen

Verwandte Themen