-1

I had the idea to make a Countup from a Date with days, month and years and I want to show the countup on a small screen with a raspberry pi or something like that. How can I generate a loop etc. to update the numbers on screen (e. g. every hour)?

Here is the code I wrote:

import time

now = time.localtime()
day=now.tm_mday
month=now.tm_mon
year=now.tm_year
hour=now.tm_hour
minute=now.tm_min
summer=now.tm_isdst
print("Tag:", day)
print("Monat:", month)
print("Jahr:", year)



if month<4:
    j=year-2020
elif month==4:
    if day<4:
        j=year-2020
    else:
        j=year-2019
else:
    j=year-2019


if month<4:
    m=month+7
elif month==4:
    if day<7:
        m=month+7
    elif day>=7:
        m=0
    else:
        m=month-4
else:
    m=month-4




if day<7:
    t=day+24
elif day==7:
    t=0
else:
    t=day-7
print(j , ":" , m , ":" , t) 

The Timer should work from the 7th of april 2020.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
VRK
  • 51
  • 4

2 Answers2

0

This loop works fairly well...

from datetime import datetime                              # for managing dates and times
import time                                                # for sleeping
while True:                                                # Just loop forever
    time.sleep(0.0)                                        # Set this to modify the tick rate
    d = datetime.now()                                     # Update time
    print(d.strftime("%A %d. %B %Y %H:%M:%S.%f"),end="\r") # Print time

If you are confused by the strftime just check out the formatting placeholders here: https://strftime.org/

kpie
  • 9,588
  • 5
  • 28
  • 50
0

So, I put the code you sent me in my project and it worked perfectly. My next step is to put the numbers (years,month,days) in a window or something like that, to show the counter on a screen with a raspberry pi. So, how can I open a window, which shows the counter and updates every hour without closing the window?

That's my code so far:

import time

now = time.localtime()
hour=now.tm_hour
minute=now.tm_min
summer=now.tm_isdst
#print("Tag:", day)
#print("Monat:", month)
#print("Jahr:", year)


while True:
    time.sleep(3600.0)
    day=7
    month=4
    year=now.tm_year
    if month<4:
        j=year-2020
    elif month==4:
        if day<4:
            j=year-2020
        else:
            j=year-2019
    else:
        j=year-2019


    if month<4:
        m=month+7
    elif month==4:
        if day<7:
            m=month+7
        elif day>=7:
            m=0
        else:
            m=month-4
    else:
        m=month-4




    if day<7:
        t=day+24
    elif day==7:
        t=0
    else:
        t=day-7
    print(j , ":" , m , ":" , t) #vllt noch h und min???


VRK
  • 51
  • 4