Wednesday, March 14, 2018

fun with python

I highly recommend that anyone interested in programming install Anaconda.  It turns Python into a Matlab-like environment, particularly if you use the Spyder IDE that installs with Anaconda. 

Plus, it's.....FREE!  And has free modules for just about anything.

This little widget that I run to track my fasts and countdown to retirement only took about 60 lines of code, including comments (code provided below).

# -*- coding: utf-8 -*-
"""
Created on Wed Feb 14 10:03:23 2018

@author: JBC4
"""

from tkinter import *
import time
from datetime import datetime

#Get user inputs
fastStart = input('Input Fast Start Date and Time (MM/DD/YYYY HH:MM:SS):  ')
dateData = datetime.strptime(fastStart,"%m/%d/%Y %H:%M:%S")
fastLength = int(input('Input length of fast in hours: '))


# Calculate the number of days between today and VERA eligibility
retirementDate = datetime.strptime('10/04/2029',"%m/%d/%Y")
currentDay = datetime.now()
deltaDays = retirementDate - currentDay
daysLeft = 'Days to VERA: ' + str(deltaDays.days)
yearsLeft = 'Years Left:  ' + str(round(deltaDays.days/365,2))

# Build and run the clock
root = Tk()
root.title("Carneal's Progress Tracker")
time1 = ''
clock = Label(root, font=('times', 48, 'bold'), bg='yellow')
pctLine = Label(root,font=('times',48,'bold'),bg='yellow')
retirementLine = Label(root,font=('times',48,'bold'),bg='yellow')
yearsLine = Label(root,font=('times',48,'bold'),bg='yellow')
clock.pack(fill=BOTH, expand=1)
pctLine.pack(fill=BOTH,expand=1)
retirementLine.pack(fill=BOTH,expand=1)
yearsLine.pack(fill=BOTH,expand=1)

def tick():
    global time1
    # get the current local time from the PC
    time2 = time.strftime('%H:%M:%S')
    # if time string has changed, update it
    if time2 != time1:
        time1 = time2
        now = time.localtime()
        hoursFasted = (24-dateData.hour)+(now[2]-(dateData.day+1))*24+now[3]+now[4]/60
        pctComplete = (hoursFasted/fastLength)*100
        displayNum = 'Hours Fasted:  ' + str(round(hoursFasted,2))
        displayPct = '% Complete: ' + str(round(pctComplete,2))+'%'
        clock.config(text=displayNum)
        pctLine.config(text=displayPct)
        retirementLine.config(text=daysLeft)
        yearsLine.config(text=yearsLeft)
    # calls itself every 200 milliseconds
    # to update the time display as needed
    # could use >200 ms, but display gets jerky
    clock.after(200, tick)
tick()
root.mainloop(  )



No comments:

Post a Comment

The fire rises

wow.