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

时间:2017-03-09 13:43:06

标签: python python-3.x calendar

我的代码允许用户点击日历,然后输出日期。我想将日期链接到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'

如何克服错误?

1 个答案:

答案 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)