我的代码允许用户点击日历,然后输出日期。我想将日期链接到SQL查询,但有错误:
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 :(得分: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)