简体   繁体   English

“datetime”类型的参数不能分配给“str”类型的参数“value”

[英]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.我正在尝试解决一些警告/错误。

This is my code这是我的代码

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 行中, d1d3抛出已注释的错误。 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.

相关问题 “Series[Dtype]”类型的参数不能分配给“DataFrame”类型的参数 - Argument of type “Series[Dtype]” cannot be assigned to parameter of type “DataFrame” function 中“int”类型的参数不能分配给“slice”类型的参数“__s” “__setitem__” “int”与“slice”不兼容 - Argument of type "int" cannot be assigned to parameter "__s" of type "slice" in function "__setitem__" "int" is incompatible with "slice" function“__setitem__”中类型“Literal['x']”的参数不能分配给类型“slice”的参数“__s” - Argument of type "Literal['x']" cannot be assigned to parameter "__s" of type "slice" in function "__setitem__" Pylance:“int”类型的参数不能分配给“Literal[24, 48]”类型的参数 - Pylance: Argument of type "int" cannot be assigned to parameter of type "Literal[24, 48]" 无法将参数类型分配给参数类型“列表”<int> '</int> - The argument type can't be assigned to the parameter type 'List<int>' to_datetime 无法识别的值类型:<class 'str'></class> - to_datetime Unrecognized value type: <class 'str'> Python DateTime TypeError:无法将类型“ Period”与类型“ str”进行比较 - Python DateTime TypeError: Cannot compare type 'Period' with type 'str' 为什么参数类型“Dict [str,Union [str,int]]”不接受“Dict [str,str]”类型的值(mypy) - Why doesn't parameter type "Dict[str, Union[str, int]]" accept value of type "Dict[str, str]" (mypy) 参数验证失败:参数 Key 的类型无效。,值:,类型:<class 'str'> ,有效类型:<class 'dict'></class></class> - Parameter validation failed: Invalid type for parameter Key., value: , type: <class 'str'>, valid types: <class 'dict'> 参数验证失败:\\n参数 Filters[0].Values[0] 的类型无效,值:{},类型:<class 'dict'> , 有效类型:<class 'str'> - Parameter validation failed:\nInvalid type for parameter Filters[0].Values[0], value: {}, type: <class 'dict'>, valid types: <class 'str'>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM