2017-03-09 131 views
1

我有允許用戶點擊日曆並輸出日期的代碼。我想把日期鏈接到一個SQL查詢,但有錯誤:TypeError:int()參數必須是字符串,類似字節的對象或數字,而不是'Calendar'

File "C:\Users\dansi\AppData\Local\Programs\Python\Python36-32\gui test 3.py", line 293, in datefunction 
agro = int(agro) 
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Calendar' 

代碼的問題:

def datefunction(self,agro): 
    print(agro) 
    agro = int(agro) 
    agro = datetime.datetime(agro) 
    timeslot = cursor.execute('''SELECT * FROM dates WHERE Date = (?)''',(agro,)) 
    list1 = list(cursor.fetchall()) 
    print(list1) 

def _show_selection(self, text, bbox): 
    """Configure canvas for a new selection.""" 
    agro = self.selection 
    print(self.selection, self.datefunction()) 
    print(agro) 

比方說,用戶點擊在3月3日,它會輸出

'2017-03-03 00-00-00' 

如何才能克服這個錯誤?

+0

你'datefunction'沒有'self'你可以解決它你也不會給它一個「agro」... –

+0

@WillemVanOnsem由於某種原因,它在我的代碼上,但不在這裏 – simons21

+0

你是什麼意思?我不明白你說什麼。 –

回答

2

問題是datefunction有一個參數agro。但由於它是一個實例函數,因此self的範圍是agro

def datefunction(self,agro): 
    print(agro) 
    agro = int(agro) 
    agro = datetime.datetime(agro) 
    timeslot = cursor.execute('''SELECT * FROM dates WHERE Date = (?)''',(agro,)) 
    list1 = list(cursor.fetchall()) 
    print(list1) 

def _show_selection(self, text, bbox): 
    """Configure canvas for a new selection.""" 
    agro = self.selection 
    print(self.selection, self.datefunction(agro)) 
    print(agro)

編輯

如果你只想要日期,你可以修改代碼:

def datefunction(self,agro): 
    print(agro) 
    agro = int(agro) 
    agro = datetime.datetime(agro).date() 
    timeslot = cursor.execute('''SELECT * FROM dates WHERE Date = (?)''',(agro,)) 
    list1 = list(cursor.fetchall()) 
    print(list1) 

def _show_selection(self, text, bbox): 
    """Configure canvas for a new selection.""" 
    agro = self.selection 
    print(self.selection, self.datefunction(agro)) 
    print(agro)
+0

謝謝,它現在可行!不知道這是否是正確的地方,但有沒有辦法讓我只有日期,而不是時間? – simons21

+1

@ simons21:你可以在'.datetime(agro)'對象上調用'.date()'。 –

相關問題