Amazing-Python-Scripts
180 строк · 4.8 Кб
1# importing the modules
2import tkinter as tk3from tkinter import messagebox4from tkinter import ttk5import datetime6import database7
8
9# creating a function to return date and time
10def getdate():11return datetime.datetime.now()12
13
14# Creating the connection
15connection = database.connect()16database.create_table1(connection)17database.create_table2(connection)18
19
20def store_exercise():21date = getdate()22data = exercise_entry.get("1.0", 'end-1c')23if data != "":24exercise_entry.delete("1.0", "end")25database.add_exercise(connection, date, data)26messagebox.showinfo("Success", "Log is inserted")27else:28messagebox.showerror("Error", "Enter Something")29
30
31def store_food():32date = getdate()33data = food_entry.get("1.0", "end-1c")34if data != "":35food_entry.delete("1.0", "end")36database.add_food(connection, date, data)37messagebox.showinfo("Success", "Log is inserted")38else:39messagebox.showerror("Error", "Enter Something")40
41
42def show_exercise():43con = database.connect()44cor = con.cursor()45try:46cor.execute('''SELECT * from exercise''')47rows = cor.fetchall()48new = tk.Tk()49new.title("Exercise Log")50new.geometry("650x500")51
52frm = tk.Frame(new)53frm.pack(side=tk.LEFT, padx=20)54
55tv = ttk.Treeview(frm, selectmode='browse')56tv.pack()57verscrlbar = ttk.Scrollbar(new,58orient="vertical",59command=tv.yview)60
61verscrlbar.pack(side='right', fill='x')62
63tv.configure(xscrollcommand=verscrlbar.set)64
65tv["columns"] = ("1", "2", "3")66tv['show'] = 'headings'67
68tv.column("1", width=50, anchor='c')69tv.column("2", width=250, anchor='c')70tv.column("3", width=400, anchor='w')71
72tv.heading("1", text="Sl.No")73tv.heading("2", text="Time")74tv.heading("3", text="Data")75
76for i in rows:77tv.insert("", "end", values=i)78
79new.mainloop()80except:81messagebox.showerror("Error", "Some Error Occurred")82
83
84def show_food():85con = database.connect()86cor = con.cursor()87try:88cor.execute('''SELECT * from food''')89rows = cor.fetchall()90new = tk.Tk()91new.title("Food Log")92new.geometry("650x500")93
94frm = tk.Frame(new)95frm.pack(side=tk.LEFT, padx=20)96
97tv = ttk.Treeview(frm, selectmode='browse')98tv.pack()99verscrlbar = ttk.Scrollbar(new,100orient="vertical",101command=tv.yview)102
103verscrlbar.pack(side='right', fill='x')104
105tv.configure(xscrollcommand=verscrlbar.set)106
107tv["columns"] = ("1", "2", "3")108tv['show'] = 'headings'109
110tv.column("1", width=50, anchor='c')111tv.column("2", width=250, anchor='c')112tv.column("3", width=400, anchor='w')113
114tv.heading("1", text="Sl.No")115tv.heading("2", text="Time")116tv.heading("3", text="Data")117
118for i in rows:119tv.insert("", "end", values=i)120
121new.mainloop()122except:123messagebox.showerror("Error", "Some Error Occurred")124
125
126def delete_exercise_log():127messagebox.showinfo("Delete", "The Exercise Log is deleted")128database.delete_exercise(connection)129
130
131def delete_food_log():132messagebox.showinfo("Delete", "The Food Log is deleted")133database.delete_food(connection)134
135
136# Making the GUI
137root = tk.Tk()138
139root.title("main")140root.geometry("500x500")141
142heading = tk.Label(root, text="Health Log book",143font=('Helvetica', 18, 'bold'))144heading.pack()145
146exercise_heading = tk.Label(147root, text=" 1) Enter each exercise separated with commas", font=('Helvetica', 11, 'bold'))148exercise_heading.place(x=30, y=40)149
150exercise_entry = tk.Text(root, height=5, width=42)151exercise_entry.pack(pady=30)152
153exercise_submit = tk.Button(root, text="Submit", command=store_exercise)154exercise_submit.place(x=210, y=160)155
156food_heading = tk.Label(root, text="2) Enter each food separated with commas", font=(157'Helvetica', 11, 'bold'))158food_heading.place(x=30, y=200)159
160food_entry = tk.Text(root, height=5, width=42)161food_entry.pack(pady=40)162
163food_submit = tk.Button(root, text="Submit", command=store_food)164food_submit.place(x=210, y=330)165
166retrieve_exercise = tk.Button(167root, text="Show Exercise Log", command=show_exercise)168retrieve_exercise.place(x=50, y=400)169
170retrieve_food = tk.Button(root, text="Show food Log", command=show_food)171retrieve_food.place(x=300, y=400)172
173delete_exercise = tk.Button(174root, text="Delete Exercise Log", command=delete_exercise_log)175delete_exercise.place(x=50, y=450)176
177delete_food = tk.Button(root, text="Delete food Log", command=delete_food_log)178delete_food.place(x=300, y=450)179
180root.mainloop()181