Creating a Graphical User Interface using Python's Tkinter Library
fromtkinterimport*
importos.path
importplatform
importtime
fromosimport path
fromdatetimeimport datetime
window =Tk()
window.geometry('350x90')
window.title("Image Analyzer")
lbl=Label(window, text="Enter File: ")
lbl.grid(column=0, row=0)
txt =Entry(window, width=30)
txt.grid(column=2,row=0)
txt.focus()
defbtn1_clicked():
# Get entry text
file_dir=txt.get()
# Get file from dir
try:
# Check if file exists
ifpath.exists(file_dir):
# open file
files =dict()
f =open(file_dir, "r")
lines =f.readlines()
k =0
for line in lines:
line =line.rstrip("\n")
k +=1
if k ==1: # reading file name
file_date= line
file_year=file_date.split('-')[-1]
elif k ==2: # creation date
file_name= line
elif k ==3: # file size
file_size=int(line)
# Store file
ifnotfile_yearin files:
files[file_year] =list()
files[file_year].append((file_name, file_size, file_date))
k =0
f.close()
print("\t\tPhoto Library Report")
print("")
print("File Name\tDate Created\tNumber of Bytes")
print("")
total_all=0
for year in files:
total_year=0
print("")
for file in files[year]:
total_year+=file[1]
file_name=file[0]
file_size=file[1]
file_date=file[2]
print(file_name+"\t"+file_date+"\t"+str(file_size))
print("")
print("Total Bytes for Files Created in "+ year +"\t"+str(total_year))
total_all+=total_year
print("Total Bytes for All Files\t\t"+str(total_all))
except:
print("File not found!")
defbtn2_clicked():
# Get entry text
file_dir=txt.get()
# Get file from dir
try:
# Check if file exists
ifpath.exists(file_dir):
# open file
files =dict()
f =open(file_dir, "r")
lines =f.readlines()
k =0
for line in lines:
line =line.rstrip("\n")
k +=1
if k ==1: # reading file name
file_date= line
file_year=file_date.split('-')[-1]
elif k ==2: # creation date
file_name= line
elif k ==3: # file size
file_size=int(line)
# Store file
ifnotfile_yearin files:
files[file_year] =list()
files[file_year].append((file_name, file_size, file_date))
k =0
f.close()
html=""
html ="\n"
html +="\n"
total_all=0
for year in files:
total_year=0
for file in files[year]:
total_year+=file[1]
file_name=file[0]
file_size=file[1]
file_date=file[2]
html +=""+file_name+"
File NameDate CreatedNumber of Bytes
\n"
html +="Total Bytes for Files Created in "+ year +""+str(total_year) +"
\n"
total_all+=total_year
html +="Total Bytes for All Files "+str(total_all)+"
\n"
print(html)
except:
print("File not found!")
btn=Button(window, text="Write Photo Report", command = btn1_clicked)
btn.grid(column=0, row=1)
btn_html=Button(window, text="Write Photo Report to html", command = btn2_clicked)
btn_html.grid(column=0, row=2)
window.mainloop()