TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是'Calendar'

问题描述:

我有允许用户点击日历并输出日期的代码。我想把日期链接到一个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

你是什么意思?我不明白你说什么。 –

问题是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()'。 –