Python PyQt5 – 将 QTime 转换为 Python 时间对象

huangapple go评论69阅读模式
英文:

Python PyQt5 - Convert QTime to python time object

问题

我必须解决这个错误:

day_frequency_parameters = self.main_self.ui_scheduled_transmitions_create_window.daily_one_time_timeEdit.time().strftime('%H:%M:%S')
AttributeError: 'QTime' object has no attribute 'strftime'

其中 daily_one_time_timeEdit 是一个 QTimeEdit 对象。

是否有办法将 QTimeEdit 或 QTime 转换为 Python 的时间对象?

英文:

I have to solve this error:

day_frequency_parameters = self.main_self.ui_scheduled_transmitions_create_window.daily_one_time_timeEdit.time().strftime('%H:%M:%S')
AttributeError: 'QTime' object has no attribute 'strftime'

where daily_one_time_timeEdit is a QTimeEdit object.

Is there any way to convert QTimeEdit or QTime to python time object?

答案1

得分: 0

你可以从QTime创建一个时间字符串:

time_str = self.main_self.ui_scheduled_transmitions_create_window.daily_one_time_timeEdit.time().toString('%H:%M:%S')

然后从中创建一个时间对象:

import datetime

time_str = "14:32:15"
datetime_obj = datetime.datetime.strptime(time_str, '%H:%M:%S')
time_obj = datetime_obj.time()
print(type(datetime_obj), datetime_obj)
print(type(time_obj), time_obj)

输出:

<class 'datetime.datetime'> 1900-01-01 14:32:15
<class 'datetime.time'> 14:32:15

我确信你也可以直接使用毫秒来完成这个操作,这样你就不需要首先创建一个字符串。

英文:

You could create a time string from QTime

time_str = self.main_self.ui_scheduled_transmitions_create_window.daily_one_time_timeEdit.time().toString(&#39;%H:%M:%S&#39;)

and create a time object from it:

import datetime

time_str = &quot;14:32:15&quot;
datetime_obj = datetime.datetime.strptime(time_str, &#39;%H:%M:%S&#39;)
time_obj = datetime_obj.time()
print(type(datetime_obj), datetime_obj)
print(type(time_obj), time_obj)

output:

&lt;class &#39;datetime.datetime&#39;&gt; 1900-01-01 14:32:15
&lt;class &#39;datetime.time&#39;&gt; 14:32:15

I'm sure you can also do this directly with msec, so you don't have to create a string first.

huangapple
  • 本文由 发表于 2023年7月14日 03:15:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76682576.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定