Amazing-Python-Scripts

Форк
0
180 строк · 4.8 Кб
1
# importing the modules
2
import tkinter as tk
3
from tkinter import messagebox
4
from tkinter import ttk
5
import datetime
6
import database
7

8

9
# creating a function to return date and time
10
def getdate():
11
    return datetime.datetime.now()
12

13

14
# Creating the connection
15
connection = database.connect()
16
database.create_table1(connection)
17
database.create_table2(connection)
18

19

20
def store_exercise():
21
    date = getdate()
22
    data = exercise_entry.get("1.0", 'end-1c')
23
    if data != "":
24
        exercise_entry.delete("1.0", "end")
25
        database.add_exercise(connection, date, data)
26
        messagebox.showinfo("Success", "Log is inserted")
27
    else:
28
        messagebox.showerror("Error", "Enter Something")
29

30

31
def store_food():
32
    date = getdate()
33
    data = food_entry.get("1.0", "end-1c")
34
    if data != "":
35
        food_entry.delete("1.0", "end")
36
        database.add_food(connection, date, data)
37
        messagebox.showinfo("Success", "Log is inserted")
38
    else:
39
        messagebox.showerror("Error", "Enter Something")
40

41

42
def show_exercise():
43
    con = database.connect()
44
    cor = con.cursor()
45
    try:
46
        cor.execute('''SELECT * from exercise''')
47
        rows = cor.fetchall()
48
        new = tk.Tk()
49
        new.title("Exercise Log")
50
        new.geometry("650x500")
51

52
        frm = tk.Frame(new)
53
        frm.pack(side=tk.LEFT, padx=20)
54

55
        tv = ttk.Treeview(frm, selectmode='browse')
56
        tv.pack()
57
        verscrlbar = ttk.Scrollbar(new,
58
                                   orient="vertical",
59
                                   command=tv.yview)
60

61
        verscrlbar.pack(side='right', fill='x')
62

63
        tv.configure(xscrollcommand=verscrlbar.set)
64

65
        tv["columns"] = ("1", "2", "3")
66
        tv['show'] = 'headings'
67

68
        tv.column("1", width=50, anchor='c')
69
        tv.column("2", width=250, anchor='c')
70
        tv.column("3", width=400, anchor='w')
71

72
        tv.heading("1", text="Sl.No")
73
        tv.heading("2", text="Time")
74
        tv.heading("3", text="Data")
75

76
        for i in rows:
77
            tv.insert("", "end", values=i)
78

79
        new.mainloop()
80
    except:
81
        messagebox.showerror("Error", "Some Error Occurred")
82

83

84
def show_food():
85
    con = database.connect()
86
    cor = con.cursor()
87
    try:
88
        cor.execute('''SELECT * from food''')
89
        rows = cor.fetchall()
90
        new = tk.Tk()
91
        new.title("Food Log")
92
        new.geometry("650x500")
93

94
        frm = tk.Frame(new)
95
        frm.pack(side=tk.LEFT, padx=20)
96

97
        tv = ttk.Treeview(frm, selectmode='browse')
98
        tv.pack()
99
        verscrlbar = ttk.Scrollbar(new,
100
                                   orient="vertical",
101
                                   command=tv.yview)
102

103
        verscrlbar.pack(side='right', fill='x')
104

105
        tv.configure(xscrollcommand=verscrlbar.set)
106

107
        tv["columns"] = ("1", "2", "3")
108
        tv['show'] = 'headings'
109

110
        tv.column("1", width=50, anchor='c')
111
        tv.column("2", width=250, anchor='c')
112
        tv.column("3", width=400, anchor='w')
113

114
        tv.heading("1", text="Sl.No")
115
        tv.heading("2", text="Time")
116
        tv.heading("3", text="Data")
117

118
        for i in rows:
119
            tv.insert("", "end", values=i)
120

121
        new.mainloop()
122
    except:
123
        messagebox.showerror("Error", "Some Error Occurred")
124

125

126
def delete_exercise_log():
127
    messagebox.showinfo("Delete", "The Exercise Log is deleted")
128
    database.delete_exercise(connection)
129

130

131
def delete_food_log():
132
    messagebox.showinfo("Delete", "The Food Log is deleted")
133
    database.delete_food(connection)
134

135

136
# Making the GUI
137
root = tk.Tk()
138

139
root.title("main")
140
root.geometry("500x500")
141

142
heading = tk.Label(root, text="Health Log book",
143
                   font=('Helvetica', 18, 'bold'))
144
heading.pack()
145

146
exercise_heading = tk.Label(
147
    root, text=" 1) Enter each exercise separated with commas", font=('Helvetica', 11, 'bold'))
148
exercise_heading.place(x=30, y=40)
149

150
exercise_entry = tk.Text(root, height=5, width=42)
151
exercise_entry.pack(pady=30)
152

153
exercise_submit = tk.Button(root, text="Submit", command=store_exercise)
154
exercise_submit.place(x=210, y=160)
155

156
food_heading = tk.Label(root, text="2) Enter each food separated with commas", font=(
157
    'Helvetica', 11, 'bold'))
158
food_heading.place(x=30, y=200)
159

160
food_entry = tk.Text(root, height=5, width=42)
161
food_entry.pack(pady=40)
162

163
food_submit = tk.Button(root, text="Submit", command=store_food)
164
food_submit.place(x=210, y=330)
165

166
retrieve_exercise = tk.Button(
167
    root, text="Show Exercise Log", command=show_exercise)
168
retrieve_exercise.place(x=50, y=400)
169

170
retrieve_food = tk.Button(root, text="Show food Log", command=show_food)
171
retrieve_food.place(x=300, y=400)
172

173
delete_exercise = tk.Button(
174
    root, text="Delete Exercise Log", command=delete_exercise_log)
175
delete_exercise.place(x=50, y=450)
176

177
delete_food = tk.Button(root, text="Delete food Log", command=delete_food_log)
178
delete_food.place(x=300, y=450)
179

180
root.mainloop()
181

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.