2016-04-11 28 views
0

Bir sqlite db okuyarak GUI düğmelerine metin ekleyerek ve metin ekleyerek bir işlemin durumunu güncellemek için kullanılan basit bir GUI oluşturuyorum. Form doğru bir şekilde oluşturuldu ve düğme olaylarına bağlı olarak db'yi güncelleyebiliyorum, ancak bu değişiklikleri db'ye düzenli aralıklarla nasıl okuyacağımı anlayamıyorum ve bu değişikliklere dayanarak GUI'yi güncelleyemiyorum.wxPython formu sqlite3 verisine dayalı zamanlayıcı aralığı üzerine inşa edildi

Şimdi, kodun içinde olması gerektiğine inandığım yerde True ve time.sleep yöntemini ekledim, ancak şimdi eklediğim formu asla oluşturmuyor. Ürün listesini, oldukça uzun olduğu için aşağıdaki örneğe dahil etmedim. GUI'yi sürekli olarak sqlite değerlerine göre nasıl güncelleyeceğine dair bir fikri olan var mı?

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

Bu, bir mobil kullanıcının okuyabileceği bir çok koddur. Wxpython etkinlik zamanlayıcılarını incelediniz mi? http://stackoverflow.com/questions/10486500/wxpython-timer-event-interval – Torxed

cevap

1
olmayan bir engelleme şekilde GUI güncellemeniz gerekir
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 

... Bu muhtemelen en kolay ... Bu saniyelik bir gecikme sonrasında başladığı (wx.Timer üzeri) küçük bir yararı vardır Bu nedenle işlevi tamamlayın, böylece hiç bir zaman için kendinizi durdurmadığınızdan emin olun