Home > Net >  Trouble converting string to time
Trouble converting string to time

Time:06-27

I have an alarm program that accepts a user input in a tkinter GUI then I try to store that input as a time datatype. I am having trouble converting the string to time.

a.setAlarm( time.strptime(e.get(), "%H:%M") )
e.delete(0, END)
print(a.getAlarmTime())

a is an instance of my class Alarm. e is an instance of the tkinter class Entry.

In the setAlarm(alarm_time) function, I have the following:

if type(alarm_time) is time:
    self.alarm_time = alarm_time
    print('It\'s a time!')
else:
    print('It\'s not a time!')

CodePudding user response:

I don't know what is your real problem but I see one mistake:

time is NOT type, but module's name.

If you run

print( type( time.strptime("12:00", "%H:%M") ) )

then you see real type

<class 'time.struct_time'>

and you should use time.struct_time instead of time like

#if type(alarm_time) is time.struct_time:
if isinstance(alarm_time, time.struct_time):
    self.alarm_time = alarm_time
    print("It's a time!")
else:
    print("It's not a time!")
  • Related