2017-09-10 45 views
-2

我目前正在使用Python 2.7,Kivy和Sqlite3構建一個應用程序。我無法傳遞一個變量,這是一個來自text_input小部件(來自Kivy花園的Date Picker)的值,該值在函數choose_date_water中,我將它傳遞給另一個函數view_water_figures,以便它可以用於查詢Sqlite表。我一直試圖找出幾天,但沒有多少運氣,希望得到一些幫助。如何在Python/Kivy的另一個函數中將變量從一個函數傳遞給Sqlite3查詢

  1. 如何將第一個函數的變量傳遞給第二個函數?
  2. 我是否正確使用這個變量來查詢Sqlite表格dp1

相關的代碼:

class LogSheet(TabbedPanel): 


    def choose_date_water(self): 
     box = FloatLayout() 
     box.add_widget(Label(text = "Select Date To View", font_size = (30), pos_hint = {'center_x':0.5, 'center_y': 0.9 })) 
     #box.add_widget(TextInput(hint_text = "dd/mm/yyyy", font_size = (33), size_hint = (None, None), size = (300, 50), pos_hint = {'center_x':0.5, 'center_y': 0.6 })) 
     dp1 = box.add_widget(DatePicker(pHint_x = (0.35), pHint_y = (0.55), size_hint = (None, None), size = (190, 50), font_size = (33), pos_hint = {'center_x':0.5, 'center_y': 0.6 })) 

     btn1 = Button(text = "OK", size_hint = (None, None), size = (200, 50), pos_hint = {'center_x':0.5, 'center_y': 0.25 }) 

     box.add_widget(btn1) 

     popup1 = Popup(title = "Choose Date", title_size = (40), title_align = 'center', content = box, size_hint = (None, None), size = (600, 300)) 

     btn1.bind(on_press = self.view_water_figures, on_release = popup1.dismiss) 

     popup1.open() 
     return dp1 

    def view_water_figures(self): 
     conn = sqlite3.connect('logsheet.db') 
     c = conn.cursor() 

     conn.execute("SELECT today_total_dw_vol, today_total_fw_vol, total_evap_out FROM waterfigures WHERE date = ?", (dp1,)) 
     conn.commit() 
     wf = c.fetchall() 

     for item in wf: 
      I1 = str(item[1]) 
      I2 = str(item[2]) 
      I3 = str(item[3]) 

     box = FloatLayout() 
     box.add_widget(Label(text = "Total Dist Water Used: " + I1, pos_hint={'center_x':0.15, 'center_y': 0.9 })) 
     box.add_widget(Label(text = "Total Fresh Water Used: " + I2, pos_hint={'center_x':0.15, 'center_y': 0.8 })) 
     box.add_widget(Label(text = "Total #1 Evap Produced: " + I3, pos_hint={'center_x':0.15, 'center_y': 0.7 })) 



     btn1 = Button(text = "OK", size_hint = (None, None), size = (200, 50), pos_hint={'center_x': 0.25, 'center_y': .1}) 
     btn2 = Button(text = "Export Data To Excel", size_hint = (None, None), size = (200, 50), pos_hint={'center_x': 0.75, 'center_y': .1}) 

     box.add_widget(btn1) 
     box.add_widget(btn2) 

     popup2 = Popup(title='WATER FIGURES', title_size= (30), title_align = 'center', content = box, size_hint=(None, None), 
       size=(600, 500)) 

     btn1.bind(on_release = popup2.dismiss) 

     popup2.open() 


     c.close() 
     conn.close() 
+1

代碼很大,並不代表你的問題。請詳細說明問題。 – RedEyed

+1

感謝您的反饋,我已刪除不相關的代碼。 – Aiden

回答

1

使用self製作日期選取器LogSheet類的成員。然後,你可以從同一類的其他方法(功能)使用self.dp1訪問:

def choose_date_water(self): 
    box = FloatLayout() 
    box.add_widget(Label(text = "Select Date To View", font_size = (30), pos_hint = {'center_x':0.5, 'center_y': 0.9 })) 

    self.dp1 = box.add_widget(DatePicker(pHint_x = (0.35), pHint_y = (0.55), size_hint = (None, None), size = (190, 50), font_size = (33), pos_hint = {'center_x':0.5, 'center_y': 0.6 })) 
    # etc... 

現在self.dp1是類的成員,並且是在該類的其他方法訪問。您可以訪問它view_water_figures()這樣的:

def view_water_figures(self): 
    conn = sqlite3.connect('logsheet.db') 
    c = conn.cursor() 

    c.execute("SELECT today_total_dw_vol, today_total_fw_vol, total_evap_out FROM waterfigures WHERE date = ?", (self.dp1.text,)) 
    wf = c.fetchall() 
    # etc... 

首先用光標運行查詢,而不是連接。其次使用self.dp1訪問日期選擇器。日期選擇器可能會提供一個text屬性,其中包含用戶選擇的日期值(不確定您正在使用哪個小部件)。

您對參數化查詢的使用是正確的,但是,在數據庫上執行讀操作時不必調用conn.commit()

+0

感謝您的幫助,非常感謝。我現在有一個DatePicker小部件(它是一個text_input小部件)的錯誤,我得到一個錯誤:AttributeError:'NoneType'對象沒有屬性'文本'。這是我的原創不同的問題,所以我會重新發布作爲另一個問題。 – Aiden

+0

@Aiden:你只需要弄清楚日期選擇器的哪個屬性包含所選日期。由於它基於文本小部件,我猜想它可能是'.text',但可能不是。 – mhawke

相關問題