[英]Argument of type "datetime" cannot be assigned to parameter "value" of type "str"
I am currently working on a Python and SQL project.我目前正在从事 Python 和 SQL 项目。
There I am building a GUI that takes information from user input and stores them in a MySQL database locally.我在那里构建了一个 GUI,它从用户输入中获取信息并将它们存储在本地的 MySQL 数据库中。
There are a few warnings/errors that I am trying to resolve.我正在尝试解决一些警告/错误。
Annotated the error-raising lines by comments.通过注释注释了错误引发的行。
elif (x == "Learn Python The Hard Way"):
self.book_id_var.set("Book Id: 2")
self.book_title_var.set("Learn Python The Hard Way")
self.book_author_var.set("Zde A. Sham")
d1 = datetime.datetime.today()
d2 = datetime.timedelta(days = 15)
d3 = d1 + d2
self.date_borrowed_var.set(d1) # Argument of type "datetime" cannot be assigned to parameter "value" of type "str" in function "set", "datetime" is incompatible with "str"
self.date_due_var.set(d3) # Argument of type "datetime" cannot be assigned to parameter "value" of type "str" in function "set", "datetime" is incompatible with "str"
self.days_on_book_var.set("15")
self.late_return_fine_var.set("Rs.25")
self.date_over_due_var.set("NO")
self.final_price_var.set("Rs.725")
In line 10 and 13, d1
and d3
are throwing the error commented.在第 10 行和第 13 行中, d1
和d3
抛出已注释的错误。 Unfortunately, I am not able to find solution to it.不幸的是,我无法找到解决方案。
Can I ignore "datetime" is incompatible with "str" , and if not, what would be a workaround?我可以忽略"datetime" is incompatible with "str"吗?如果不能,有什么解决方法?
In order to cast your datetime object to string you can call the.strftime method builtin to the datetime package为了将日期时间 object 转换为字符串,您可以调用日期时间 package 内置的 .strftime 方法
eg use:例如使用:
d3.strftime("%d.%m.%y")
instead of just而不仅仅是
d3
same goes for d1 - ("%d.%m.%y") represents the format in which your datetime is being represented. d1 也是如此 - ("%d.%m.%y") 表示您的日期时间的表示格式。
Results would look like this:结果看起来像这样:
self.date_borrowed_var.set(d1.strftime("%d.%m.%y"))
self.date_due_var.set(d3.strftime("%d.%m.%y"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.