How old am I? (first gui script) - Nwb - Jun-27-2018
Original script:
FilePath = r"C:\ TYPE YOUR FILE PATH"
from tkinter import *
from playsound import playsound
import pymsgbox as Msg
from datetime import datetime
def intWithCommas(x):
if x < 0:
return '-' + intWithCommas(-x)
result = ''
while x >= 1000:
x, r = divmod(x, 1000)
result = ",%03d%s" % (r, result)
return "%d%s" % (x, result)
def Submit(text):
#playsound("Click.mp3")
global Day, Month, Year
Day = DateEntry.get()
Month = DateEntry2.get()
Year = DateEntry3.get()
DateEntry.delete(0, 'end')
DateEntry2.delete(0, 'end')
DateEntry3.delete(0, 'end')
if (len(Day) != 2) or (len(Month) != 2) or (len(Year) != 4):
Msg.native.alert("Please complete the fields", "| How Old Am I |")
else:
a = datetime.today()
b = datetime(int(Year), int(Month), int(Day))
printz = str(intWithCommas((a-b).days*24*60))
ages = str(round((a-b).days/365.25, 2))
Msg.native.alert("You have lived for " + printz +" minutes\n" + " And you're " + ages + " years old" , "| How Old Am I |")
def callback(text):
if not text.get().isnumeric():
corrected = ''.join(filter(str.isnumeric, text.get()))
text.set(corrected)
text.set(text.get()[:2])
try:
if int(text.get()[0:1]) > 3:
text.set(text.get()[:0])
if int(text.get()[0:1]) == 3:
if int(text.get()[1:2]) > 1:
text.set(text.get()[:1])
except ValueError:
pass
def callback2(text2):
if not text2.get().isnumeric():
corrected = ''.join(filter(str.isnumeric, text2.get()))
text2.set(corrected)
text2.set(text2.get()[:2])
try:
if int(text2.get()[0:1]) > 1:
text2.set(text2.get()[:0])
if int(text2.get()[1:2]) > 2:
if int(text2.get()[0:1]) != 0:
text2.set(text2.get()[:1])
except ValueError:
pass
def callback3(text3):
if not text3.get().isnumeric():
corrected = ''.join(filter(str.isnumeric, text3.get()))
text3.set(corrected)
text3.set(text3.get()[:4])
try:
if (int(text3.get()[0:1]) < 1) or (int(text3.get()[0:1]) > 2):
text3.set(text3.get()[:0])
if int(text3.get()[0:1]) == 2:
if int(text3.get()[1:2]) > 0:
text3.set(text3.get()[:1])
if int(text3.get()[2:3]) >1:
text3.set(text3.get()[:2])
if int(text3.get()[0:1]) == 1:
if int(text3.get()[1:2]) < 9:
text3.set(text3.get()[:1])
except ValueError:
pass
root = Tk()
root.title(" | How Old Am I |")
root.geometry("322x150")
root.resizable(False, False)
text = StringVar()
text.trace("w", lambda name, index, mode, text=text: callback(text))
text2 = StringVar()
text2.trace("w", lambda name, index, mode, text2=text2: callback2(text2))
text3 = StringVar()
text3.trace("w", lambda name, index, mode, text3=text3: callback3(text3))
imgs = PhotoImage(file=FilePath+"TypeTheDate.png")
img = Label(root, image = imgs)
img.place(x=0, y=0)
DateEntry = Entry(root, textvariable = text)
DateEntry.place(x=65, y=40, height=20, width=30)
imgs2 = PhotoImage(file=FilePath+"Slash.png")
img2 = Label(root, image = imgs2)
img2.place(x=95, y=30)
DateEntry2 = Entry(root, textvariable = text2)
DateEntry2.place(x=125, y=40, height=20, width=30)
imgs3 = PhotoImage(file=FilePath+"Slash.png")
img3 = Label(root, image = imgs3)
img3.place(x=155, y=30)
DateEntry3 = Entry(root, textvariable = text3)
DateEntry3.place(x=185, y=40, height=20, width=50)
Button = Button(root, text="That's me!")
Button.place(x=105, y=72, width=100)
Button.bind("<Button-1>", Submit)
NoteLabel = Label(root, text="*Note: Type numbers in the format of\n Day / Month / Year [include 0s]")
NoteLabel.place(x=24, y=100)
root.mainloop()
Apart from the fact being that this is is my first fully-made gui script ever, this script is also coming from a newbie! So I might not understand all terms..
I spent a lot of time making sure everything was correct. But please suggest me better alternatives or warn me of bad practices I might have used.
The script is pretty simple it gives you three input fields, makes sure you type in correct values (example cannot type 3 as first digit in year's field) with the help of textvariable, and pops up how old you are in minutes when you click on "That's me!".
Also, when you run the tkinter messagebox version (below over here), you will notice that the button gets stuck every time the message pops up? I changed the module because of that reason do you guys know why that is happening?
If you want to run the script here are instructions:
Use this version of the code instead (uses tkinter messagebox instead) if you do not have pymsgbox:
-> Known issue: Button gets stuck when message pops up. Don't know why that happens.
FilePath = r"C:\ TYPE YOUR FILE PATH"
from tkinter import *
from playsound import playsound
from tkinter import messagebox as Msg
from datetime import datetime
def intWithCommas(x):
if x < 0:
return '-' + intWithCommas(-x)
result = ''
while x >= 1000:
x, r = divmod(x, 1000)
result = ",%03d%s" % (r, result)
return "%d%s" % (x, result)
def Submit(text):
#playsound("Click.mp3")
global Day, Month, Year
Day = DateEntry.get()
Month = DateEntry2.get()
Year = DateEntry3.get()
DateEntry.delete(0, 'end')
DateEntry2.delete(0, 'end')
DateEntry3.delete(0, 'end')
if (len(Day) != 2) or (len(Month) != 2) or (len(Year) != 4):
Msg.showinfo("| How Old Am I |", "Please complete the fields")
else:
a = datetime.today()
b = datetime(int(Year), int(Month), int(Day))
printz = str(intWithCommas((a-b).days*24*60))
ages = str(round((a-b).days/365.25, 2))
Msg.showinfo("| How Old Am I |", "You have lived for " + printz +" minutes\n" + " And you're " + ages + " years old")
def callback(text):
if not text.get().isnumeric():
corrected = ''.join(filter(str.isnumeric, text.get()))
text.set(corrected)
text.set(text.get()[:2])
try:
if int(text.get()[0:1]) > 3:
text.set(text.get()[:0])
if int(text.get()[0:1]) == 3:
if int(text.get()[1:2]) > 1:
text.set(text.get()[:1])
except ValueError:
pass
def callback2(text2):
if not text2.get().isnumeric():
corrected = ''.join(filter(str.isnumeric, text2.get()))
text2.set(corrected)
text2.set(text2.get()[:2])
try:
if int(text2.get()[0:1]) > 1:
text2.set(text2.get()[:0])
if int(text2.get()[1:2]) > 2:
if int(text2.get()[0:1]) != 0:
text2.set(text2.get()[:1])
except ValueError:
pass
def callback3(text3):
if not text3.get().isnumeric():
corrected = ''.join(filter(str.isnumeric, text3.get()))
text3.set(corrected)
text3.set(text3.get()[:4])
try:
if (int(text3.get()[0:1]) < 1) or (int(text3.get()[0:1]) > 2):
text3.set(text3.get()[:0])
if int(text3.get()[0:1]) == 2:
if int(text3.get()[1:2]) > 0:
text3.set(text3.get()[:1])
if int(text3.get()[2:3]) >1:
text3.set(text3.get()[:2])
if int(text3.get()[0:1]) == 1:
if int(text3.get()[1:2]) < 9:
text3.set(text3.get()[:1])
except ValueError:
pass
root = Tk()
root.title(" | How Old Am I |")
root.geometry("322x150")
root.resizable(False, False)
text = StringVar()
text.trace("w", lambda name, index, mode, text=text: callback(text))
text2 = StringVar()
text2.trace("w", lambda name, index, mode, text2=text2: callback2(text2))
text3 = StringVar()
text3.trace("w", lambda name, index, mode, text3=text3: callback3(text3))
imgs = PhotoImage(file=FilePath+"TypeTheDate.png")
img = Label(root, image = imgs)
img.place(x=0, y=0)
DateEntry = Entry(root, textvariable = text)
DateEntry.place(x=65, y=40, height=20, width=30)
imgs2 = PhotoImage(file=FilePath+"Slash.png")
img2 = Label(root, image = imgs2)
img2.place(x=95, y=30)
DateEntry2 = Entry(root, textvariable = text2)
DateEntry2.place(x=125, y=40, height=20, width=30)
imgs3 = PhotoImage(file=FilePath+"Slash.png")
img3 = Label(root, image = imgs3)
img3.place(x=155, y=30)
DateEntry3 = Entry(root, textvariable = text3)
DateEntry3.place(x=185, y=40, height=20, width=50)
Button = Button(root, text="That's me!")
Button.place(x=105, y=72, width=100)
Button.bind("<Button-1>", Submit)
NoteLabel = Label(root, text="*Note: Type numbers in the format of\n Day / Month / Year [include 0s]")
NoteLabel.place(x=24, y=100)
root.mainloop()
Oh and here are the pictures I used just by the way:
(I'll upload them later )
Thanks for reading!
RE: How old am I? (first gui script) - buran - Jun-27-2018
for start:
Read PEP8 for Style Guides, e.g. naming conventions
Read about String formatting, e.g. instead of defining function to add commas
Use IntVar, instead of StringVar
Use datetime module to construct date from user input and it will be way easier to check the input - e.g. is it valid date, is it before today, etc.
RE: How old am I? (first gui script) - Nwb - Jun-27-2018
Thanks I appreciate it. Do you face what I did when running tkinter's messagebox? Every time the messagebox pops up the button that spawns the messagebox gets stuck.. I was wondering if it's just me.
RE: How old am I? (first gui script) - buran - Jun-27-2018
I didn't run it
RE: How old am I? (first gui script) - buran - Jun-27-2018
Normally you would display the result in a widget, e.g. label, not messagebox.
Also, get rid of using global variables
RE: How old am I? (first gui script) - Nwb - Jun-28-2018
(Jun-27-2018, 03:46 PM)buran Wrote: Normally you would display the result in a widget, e.g. label, not messagebox. Also, get rid of using global variables
I want a secondary window to display errors (the result is just temporarily using the msgbox in the future it will pop a window with a picture in it), so messagebox from tkinter seemed very convenient. But this malfunction seems to occur to me with both my GUI scripts so I have to use another function instead of messagebox.
|