-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagenda.py
199 lines (181 loc) · 7.62 KB
/
agenda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#
# Agenda en tkinter python3 y sqlite3
# Juan Manuel Fernandez
# https://github.com/juanmfer
#
#
import tkinter as tk
from tkinter import ttk
from tkinter import *
import sqlite3
from sqlite3 import Error
from tkinter import font
from tkcalendar import Calendar, DateEntry
from tkinter import messagebox
from pathlib import Path
import os.path
#################### SQLITE3 base de datos - create db, table connection - creo db, tablas y conexion
def crear_conexion(basedatos):
""" crear una conexión de base de datos a la base de datos SQLite
especificada en basedatos
:paramametro basedatos, archivo de base de datos
:retorna conexion o no
"""
conn = None
try:
conn = sqlite3.connect(basedatos)
return conn
except Error as e:
print(e)
return conn
def creo_tabla(conn, crear_tabla_sql):
try:
c = conn.cursor()
c.execute(crear_tabla_sql)
except Error as e:
print(e)
def iniciodb(): ###### creo base de datos - create db
##### Selecciona tu ubicacion
database = r"agenda.db"
#database = os.path.dirname(os.path.abspath(__file__)) + '/agenda.db'
sql_crear_tabla_agenda = """ CREATE TABLE IF NOT EXISTS agenda(
id INTEGER PRIMARY KEY AUTOINCREMENT,
fecha text NOT NULL,
agendar text NOT NULL
); """
# creo conexion a base de datos - create a database connection
conn = crear_conexion(database)
# creo las tablas - create tables
if conn is not None:
# creo la tabla agenda - create agenda table
creo_tabla(conn, sql_crear_tabla_agenda)
else:
print("No se puede crear la base de datos - cannot create the database connection.")
###################################### SQLITE3 SELECT INSERT DELETE
###### insertar informacion
def agrego():
conn = sqlite3.connect(r"agenda.db")
#conn = sqlite3.connect(os.path.dirname(os.path.abspath(__file__)) + '/agenda.db')
c = conn.cursor()
fecha = str(fechavar.get())
agendar = str(agendarvar.get())
c.execute("INSERT INTO agenda VALUES (NULL,?,?);",(fecha,agendar)) #laid
conn.commit()
muestro()
conn.close()
###### Actualizo Tree
def actualizar():
conn = sqlite3.connect(r"agenda.db")
#conn = sqlite3.connect(os.path.dirname(os.path.abspath(__file__)) + '/agenda.db')
c = conn.cursor()
c.execute("SELECT * FROM agenda ORDER BY id DESC")
registros = c.fetchall()
#registros = c.fetchone()
for reg in tree.get_children():
tree.delete(reg)
for i in registros:
tree.insert("", tk.END, values=i)
conn.close()
###### Muestro datos en Tree
def muestro():
conn = sqlite3.connect(r"agenda.db")
#conn = sqlite3.connect(os.path.dirname(os.path.abspath(__file__)) + '/agenda.db')
c = conn.cursor()
c.execute("SELECT * FROM agenda ORDER BY id DESC")
registros = c.fetchall()
for reg in tree.get_children():
tree.delete(reg)
for i in registros:
tree.insert("", tk.END, values=i)
conn.close()
###### Elimino Registros seleccionados en Tree
def EliminarReg():
conn = sqlite3.connect("agenda.db")
#conn = sqlite3.connect(os.path.dirname(os.path.abspath(__file__)) + '/agenda.db')
cur = conn.cursor()
try:
messageDelete = messagebox.askyesno("Confirmar", "Deseas eliminar permanentemente el recordatorio?")
if messageDelete > 0:
for selected_item in tree.selection():
cur.execute("DELETE FROM agenda WHERE id=?", (tree.set(selected_item, '#1'),))
conn.commit()
tree.delete(selected_item)
conn.close()
except Exception as e:
print(e)
##### Muestro registros por dia
def muestrodia():
conn = sqlite3.connect(r"agenda.db")
fechavardiam = str(fechavardia.get())
#conn = sqlite3.connect(os.path.dirname(os.path.abspath(__file__)) + '/agenda.db')
c = conn.cursor()
c.execute("SELECT * FROM agenda WHERE fecha = ?", (fechavardiam, ))
registros = c.fetchall()
for reg in tree.get_children():
tree.delete(reg)
for i in registros:
tree.insert("", tk.END, values=i)
conn.close()
if __name__ == '__main__':
###### ventana principal - root window
root = tk.Tk()
root.geometry('1000x900')
root.title('Agenda Sqlite')
root.resizable(False, False)
###### Estilos entry - style entry
style = ttk.Style()
style.configure(
"MyEntry.TEntry",
padding=5
)
micalendario = Calendar(root,selectmode = "day") ###### crear calendario - create calendar
fechavar = StringVar() ###### variable fecha
agendarvar = StringVar() ###### variable recordatorio
fechavardia = StringVar() ###### variable fecha
###### textos - text
seleccionar = ttk.Label(text='Seleccionar Fecha',font=font.Font(family="Verdana", size=11))
seleccionar.place(x=50, y=40)
agendartxt = ttk.Label(text='Recordatorio',font=font.Font(family="Verdana", size=11))
agendartxt.place(x=50, y=90)
###### Calendarios - calendar
micalendario = DateEntry(width=20, background='darkblue',foreground='white', borderwidth=20,font=font.Font(family="Verdana", size=11), textvariable=fechavar)
micalendario.pack(padx=10, pady=10)
micalendario.place(x=200, y=40)
###### Textbox recordatorio
agendar = ttk.Entry( justify=tk.LEFT, show="", width=50, font=font.Font(family="Verdana", size=14),style="MyEntry.TEntry", textvariable=agendarvar)
agendar.place(x=200, y=85)
###### Botones
guardodb = Button(text="Guardar", width=14, font=font.Font(family="Verdana", size=11), command=agrego)
guardodb.place(x=816, y=86)
##### labelframe - Eliminar - delete selection
delselectreg = ttk.LabelFrame(root, text='Eliminar Selección')
delselectreg.place(x=740, y=780)
borrodato = Button(delselectreg, text="Borrar Registro", width=14, font=font.Font(family="Verdana", size=11), command=EliminarReg)
borrodato.pack(padx=15, pady=27)
##### labelFrame - Todos los recordatorios - all reg
mostrartodosr = ttk.LabelFrame(root, text='Todos los Recordatorios')
mostrartodosr.place(x=500, y=780)
actualizo = Button(mostrartodosr, text="Actualizar", width=14, font=font.Font(family="Verdana", size=11), command=actualizar)
actualizo.pack(padx=15, pady=27)
##### LabelFrame - Diario - day
lfdiario = ttk.LabelFrame(root, text='Recordatorios Diarios')
lfdiario.place(x=40, y=780)
micalendario2 = DateEntry(lfdiario, width=20, background='darkblue',foreground='white', borderwidth=20,font=font.Font(family="Verdana", size=11), textvariable=fechavardia)
micalendario2.pack(padx=15, pady=5)
mostrardia = Button(lfdiario,text="Mostrar", width=14, font=font.Font(family="Verdana", size=11), command=muestrodia)
mostrardia.pack(padx=15, pady=10)
###### Treeview Muestro registros -
tree = ttk.Treeview(root,columns=("Registro", "Fecha", "Recordatorio"),height=30, selectmode='browse', padding=10)
tree.place(x=40, y=140)
vsb = ttk.Scrollbar(root, orient="vertical", command=tree.yview) ###### Scrollbar treeview
vsb.place(x=30+910+2, y=143, height=600+20)
tree.configure(yscrollcommand=vsb.set, padding=4)
tree.heading('Registro', text=" Registro", anchor=W)
tree.heading('Fecha', text=" Fecha", anchor=W)
tree.heading('Recordatorio', text=" Recordatorio", anchor=W)
tree.column('#0', stretch=NO, minwidth=0, width=0)
tree.column('#1', stretch=NO, minwidth=0, width=80)
tree.column('#2', stretch=NO, minwidth=0, width=200)
tree.column('#3', stretch=NO, minwidth=0, width=610)
iniciodb() ###### creo db si es necesario
root.mainloop()