Last active
March 11, 2016 14:15
-
-
Save stmobo/3b69a75b27945ddddc3c to your computer and use it in GitHub Desktop.
Simple Tkinter countdown program
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import tkinter as tk; | |
import datetime | |
competitionDay = datetime.date(year = 2016, month = 3, day = 11) | |
competitionTimes = [ | |
datetime.time(hour = 9, minute = 00, second = 0), | |
datetime.time(hour = 10, minute = 51, second = 0), | |
datetime.time(hour = 11, minute = 50, second = 0), | |
datetime.time(hour = 13, minute = 21, second = 0), | |
datetime.time(hour = 14, minute = 20, second = 0), | |
datetime.time(hour = 15, minute = 26, second = 0), | |
datetime.time(hour = 16, minute = 42, second = 0), | |
datetime.time(hour = 17, minute = 34, second = 0), | |
] | |
# set to true if on red team | |
redTeam = [ | |
False, | |
False, | |
True, | |
True, | |
True, | |
False, | |
True, | |
True | |
] | |
competitionNumbers = [ | |
1, | |
16, | |
24, | |
28, | |
36, | |
45, | |
55, | |
62 | |
] | |
countdownIndex = 0 | |
for compTime in competitionTimes: | |
if(datetime.datetime.now() < datetime.datetime.combine(competitionDay, compTime)): | |
break | |
countdownIndex += 1 | |
class CountdownApp(tk.Frame): | |
target = datetime.datetime.combine(competitionDay, competitionTimes[countdownIndex]) #datetime.datetime(year = 2016, month = 2, day = 13, hour = 1, minute = 30, second=0) | |
current = datetime.datetime.now() #datetime.datetime(year = 2016, month = 2, day = 13, hour = 1, minute = 30, second=0) | |
redTeamColor = "red" | |
blueTeamColor = "blue" | |
currentColor = (redTeamColor if redTeam[countdownIndex] else blueTeamColor) | |
curIdx = countdownIndex | |
def __init__(self, master=None): | |
tk.Frame.__init__(self, master) | |
self.configure(bg="white") | |
self.bind("<Configure>", self.resize) | |
self.grid() | |
master.rowconfigure(0, weight=1) | |
master.rowconfigure(1, weight=1) | |
master.rowconfigure(2, weight=1) | |
master.columnconfigure(0, weight=1) | |
master.columnconfigure(1, weight=1) | |
master.columnconfigure(2, weight=1) | |
master.columnconfigure(3, weight=1) | |
master.columnconfigure(4, weight=1) | |
self.rowconfigure(0, weight=1) | |
self.rowconfigure(1, weight=1) | |
self.rowconfigure(2, weight=1) | |
self.columnconfigure(0, weight=1) | |
self.columnconfigure(1, weight=1) | |
self.columnconfigure(2, weight=1) | |
self.columnconfigure(3, weight=1) | |
self.columnconfigure(4, weight=1) | |
self.createWidgets() | |
def setTime(self, t): | |
self.current = t | |
def createWidgets(self): | |
self.hrcount = tk.Label(font = "Helvetica", fg=self.currentColor, bg="white") | |
self.mincount = tk.Label(font = "Helvetica", fg=self.currentColor, bg="white") | |
self.seccount = tk.Label(font = "Helvetica", fg=self.currentColor, bg="white") | |
self.hrcount.grid(row=1, column=0, sticky=tk.N + tk.E + tk.S + tk.W) | |
self.mincount.grid(row=1, column=2, sticky=tk.N + tk.E + tk.S + tk.W) | |
self.seccount.grid(row=1, column=4, sticky=tk.N + tk.E + tk.S + tk.W) | |
self.colon1 = tk.Label(text=":", font="Helvetica", fg=self.currentColor, bg="white") | |
self.colon2 = tk.Label(text=":", font="Helvetica", fg=self.currentColor, bg="white") | |
self.colon1.grid(row=1, column=1, sticky=tk.N + tk.E + tk.S + tk.W) | |
self.colon2.grid(row=1, column=3, sticky=tk.N + tk.E + tk.S + tk.W) | |
self.hrlabel = tk.Label(text="Hours", font="Helvetica", fg=self.currentColor, bg="white") | |
self.minlabel = tk.Label(text="Minutes", font="Helvetica", fg=self.currentColor, bg="white") | |
self.seclabel = tk.Label(text="Seconds", font="Helvetica", fg=self.currentColor, bg="white") | |
self.hrlabel.grid(row=2, column=0, sticky=tk.N + tk.E + tk.S + tk.W) | |
self.minlabel.grid(row=2, column=2, sticky=tk.N + tk.E + tk.S + tk.W) | |
self.seclabel.grid(row=2, column=4, sticky=tk.N + tk.E + tk.S + tk.W) | |
self.countdownTitle = tk.Label(text="Next Match: " + str(competitionNumbers[countdownIndex]) + (" (Red Team)" if redTeam[countdownIndex] else " (Blue Team)"), font="Helvetica", fg=self.currentColor, bg="white") | |
self.countdownTitle.grid(row=0, column=2, sticky=tk.N + tk.E + tk.S + tk.W) | |
#self.timelabel = tk.Label(font="Helvetica 108", fg="maroon", bg="white") | |
#self.timelabel.pack(side="top", fill="both", expand=True, padx=10, pady=10) | |
def updateTime(self): | |
if(self.target < datetime.datetime.now()): | |
self.curIdx += 1 | |
if(self.curIdx > len(competitionTimes)): | |
return | |
self.target = datetime.datetime.combine(competitionDay, competitionTimes[self.curIdx]) | |
self.currentColor = (self.redTeamColor if redTeam[self.curIdx] else self.blueTeamColor) | |
self.countdownTitle.configure(text = "Next Match: " + str(competitionNumbers[self.curIdx]) + (" (Red Team)" if redTeam[self.curIdx] else " (Blue Team)"), fg=self.currentColor) | |
self.hrcount.configure(fg = self.currentColor) | |
self.mincount.configure(fg = self.currentColor) | |
self.seccount.configure(fg = self.currentColor) | |
self.hrlabel.configure(fg = self.currentColor) | |
self.minlabel.configure(fg = self.currentColor) | |
self.seclabel.configure(fg = self.currentColor) | |
self.colon1.configure(fg = self.currentColor) | |
self.colon2.configure(fg = self.currentColor) | |
delta = self.target - datetime.datetime.now() #self.current | |
h, r = divmod(delta.seconds, 3600) | |
m, s = divmod(r, 60) | |
self.hrcount.configure(text="%02d" % h) | |
self.mincount.configure(text="%02d" % m) | |
self.seccount.configure(text="%02d" % s) | |
#self.current = self.current + datetime.timedelta(seconds=1) | |
self.after(1000, self.updateTime) | |
def resize(self, event): | |
self.countdownTitle.configure(font = ("Helvetica", self.countdownTitle.winfo_height()//3 - 10)) | |
self.hrcount.configure(font = ("Helvetica", self.hrcount.winfo_width()//2)) | |
self.mincount.configure(font = ("Helvetica", self.mincount.winfo_width()//4)) | |
self.seccount.configure(font = ("Helvetica", self.seccount.winfo_width()//2)) | |
self.hrlabel.configure(font = ("Helvetica", self.hrlabel.winfo_height()//3 - 9)) | |
self.minlabel.configure(font = ("Helvetica", self.minlabel.winfo_height()//3 - 11)) | |
self.seclabel.configure(font = ("Helvetica", self.seclabel.winfo_height()//3 - 13)) | |
self.colon1.configure(font = ("Helvetica", self.colon1.winfo_height()//2)) | |
self.colon2.configure(font = ("Helvetica", self.colon2.winfo_height()//2)) | |
def promptTime(): | |
mon = int(input("Current Month: ")) | |
d = int(input("Current Day: ")) | |
h = int(input("Current Hour: ")) | |
m = int(input("Current Minute: ")) | |
dt = datetime.datetime(year = 2016, month=mon, day=d, hour=h, minute=m, second = 0) | |
return dt | |
root = tk.Tk() | |
root.geometry("1200x600") | |
app = CountdownApp(root) | |
#app.setTime(promptTime()) | |
app.updateTime() | |
app.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment