One other thing that I find to be very helpful in terms of getting some quick assistance is to share a clone link to your actual app (if possible).
You can click the
icon in the IDE and select the link for “sharing your source code with others”. In this way others can click that link and get an exact copy of your app, which makes debugging easier in many cases.
This is a good suggestion!
Regarding your particular error: Error messages in Python are usually really helpful, but this one is uncharacteristically cryptic.
“TypeError: unsupported operand type(s) for Sub: ‘datetime’ and ‘NoneType’ at [Form1, line 41]”
An operand is the thing that an operator operates on: if I have a - b
, then a
and b
are the operands. -
is the operator.
‘unsupported operand type(s)’ means that some operator in your code has been given one or more operands of a type that it doesn’t know how to deal with.
‘for Sub’: this refers to the subtract operator.
‘datetime’ and ‘NoneType’: this means that the types of the operands were datetime
and NoneType
.
The code on line 41 of Form1 is:
self.total_time = (datetime.now() - self.start_time)
So we can see why one of the operands was a datetime
- that must be the datetime.now()
. The other operand is of type NoneType
. There’s only one thing that can ever be of type NoneType
, and that’s None
(None
is special in Python, there’s only ever one of it - it’s a singleton. All variables that are set to None
are references to the same instance.)
So your problem is that self.start_time
was None
.
You’re setting self.start_time
to None
on line 34. It looks like you’ve stopped your timing process at that point, so you should disable the Timer component as well. You can do that by setting the Timer component’s interval
to 0
:
self.timer_1.interval = 0
You can set it back to something > 0 in the button_start_click
method, so it’s running again when you re-start the clock.