英文:
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('%H:%M:%S')
and create a time object from it:
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)
output:
<class 'datetime.datetime'> 1900-01-01 14:32:15
<class 'datetime.time'> 14:32:15
I'm sure you can also do this directly with msec, so you don't have to create a string first.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论