I am working on a little Python project to track how much time I am spending coding and doing things outdoors. Coding subtracts and outdoors stuff adds.
My code is looking pretty bad. Any suggestions on how to make it better?
import tkinter as tk
import time
import json
import atexit
from flask import Flask
root = None
start_time = 0
end_time = 0
_add = False
_sub = False
entries_num = 0
_click = False
name = ''
_name = None
run_txt = None
def simplify(_time):
return time.strftime('%H:%M:%S', time.localtime(_time))
def simple_mode(mode):
if mode == 0:
return "Add"
elif mode == 1:
return "Subtract"
def start():
global start_time
start_time = int(time.time())
run_txt.configure(text="Running", fg="green")
def end():
global end_time
end_time = int(time.time())
run_txt.configure(text="Not Running", fg="red")
time_manager()
def find_mode():
if _add and not _sub:
return 0
elif _sub and not _add:
return 1
# 0 is add 1 is subtract
def time_manager():
global entries_num
entries_num += 1
mode = simple_mode(find_mode())
entry_raw = {'name': _name.get(), 'mode': mode, 'start': simplify(start_time), 'end': simplify(end_time), 'total_time': simplify(end_time-start_time-(-2211688800))}
entries.update({str(entries_num): entry_raw})
display()
def add():
global _add
global _sub
_add = True
_sub = False
def sub():
global _add
global _sub
_add = False
_sub = True
def display():
height = 1
width = 5
cells = {}
for i in range(height): # Rows
for j in range(width): # Columns
b = tk.Label(root, text="")
b.grid(row=i + 16 + entries_num, column=j)
cells[(i, j)] = b
cells[(0, 0)]['text'] = entries[str(entries_num)]['name']
cells[(0, 1)]['text'] = entries[str(entries_num)]['total_time']
cells[(0, 2)]['text'] = entries[str(entries_num)]['mode']
cells[(0, 3)]['text'] = entries[str(entries_num)]['start']
cells[(0, 4)]['text'] = entries[str(entries_num)]['end']
class MainApplication(tk.Frame):
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, *args, **kwargs)
self.parent = parent
title_lbl = tk.Label(root)
title_lbl['text'] = 'TimeTracker'
title_lbl.configure(font=('Roboto', 20))
title_lbl.grid(row=0, column=0, sticky='ew')
srt_btn = tk.Button(root, text="Start", command=start, font=('Roboto', 8)).grid(row=10, column=0, sticky='sw')
end_btn = tk.Button(root, text="End", command=end, font=('Roboto', 8)).grid(row=10, column=0, sticky='se')
add_btn = tk.Button(root, text="Add Time", command=add, font=('Roboto', 8)).grid(row=11, column=0, sticky='se')
sub_btn = tk.Button(root, text="Subtract Time", command=sub, font=('Roboto', 8))
sub_btn.grid(row=12, column=0, sticky='se')
global _name
_name = tk.Entry(root, width=50)
_name.grid(row=10, column=2)
_name.insert(0, "Entry Name")
_name.configure(state=tk.DISABLED)
def on_click(event):
_name.configure(state=tk.NORMAL)
_name.delete(0, tk.END)
# make the callback only work once
_name.unbind('<Button-1>', on_click_id)
on_click_id = _name.bind('<Button-1>', on_click)
global run_txt
run_txt = tk.Label(root)
run_txt.configure(text="Not Running", fg="red")
run_txt.grid(row=11, column=2)
def save():
with open('data.json', 'w') as fb:
_json = json.dumps(entries)
fb.write(_json)
if __name__ == "__main__":
root = tk.Tk()
with open('data.json', 'r') as fr:
text = fr.read()
entries = json.loads(text)
keys = entries.keys()
for key in keys:
entries_num = int(key)
display()
root.title("TimeTracker")
root.minsize(200, 400)
atexit.register(save)
MainApplication(root).grid()
root.mainloop()