Python - Acceso a base de datos Access

 
Vista:

Acceso a base de datos Access

Publicado por Gustavo Hidalgo (1 intervención) el 12/05/2021 21:19:57
Buenas tardes, mucho gusto y gracias por la oportunidad, estoy en etapa de aprendizaje en el entorno de desarrollo de Python, Estoy trabajando con PyCharm y Access, le cuento que me ha gustado mucho este lenguaje de desarrollo.
El problema: Estoy haciendo un ejercicio de conexión con una base de datos en Access, realmente funciona bien cuando ejecuto el archivo registrarPrueba.py solo. Es decir, los datos que son tomados de la ventana (escrita en Python tkinter) y llevados a una base de datos con éxito. Y así con todas las otras ventanas de CRUD. Es decir, al ejecutarlas individualmente funcionan bien y cumplen el objetivo.
Al momento de implementar un menú para integrar todas las otras ventanas, no me sale error, pero sin embargo no captura los datos que han sido escritos en la ventana (Por ejemplo: la ventana registrarPrueba.py). y por ende no existen datos para enviar en el objeto per, de esta manera no se registran datos en la base de datos.
He buscado información y realmente no encuentro una solución. Agradecería mucho si me dan una mano al respecto. Gracias de antemano.

Expongo mi código.

modoGraficoVista.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
from tkinter import *
import tkinter as tk
from Vistas.registroPrueba import *
 
#definición de funcion para botones
def registarPersona():
    if __name__ == '__main__':
        ventana1 = tk.Tk()
        app = Application(ventana1)
        ventana1.mainloop()
 
 
#Crear raiz
raiz = tk.Tk()
raiz.title("Menú principal")
raiz.geometry("350x220")
raiz.resizable(1,1)   #permite redimensionar a lo ancho y alto
raiz.iconbitmap("..\\icono.ico")
#crear menú
# Crear la barra de menús
barraMenu = Menu(raiz)
#Crear los menus
mnuArchivo = Menu(barraMenu)
mnuAccionPersona = Menu(barraMenu)
#Crear los comandos o sub menus
mnuArchivo.add_command(label="Abrir")
mnuArchivo.add_command(label="Guardar")
mnuArchivo.add_separator()
mnuArchivo.add_command(label="Salir",command=raiz.destroy)
 
mnuAccionPersona.add_command(label="Registar", command=registarPersona)
mnuAccionPersona.add_command(label="Actualizar")
mnuAccionPersona.add_command(label="Buscar")
mnuAccionPersona.add_command(label="Eliminar")
 
#Agregar los submenus
barraMenu.add_cascade(label = "Archivo",menu = mnuArchivo)
barraMenu.add_cascade(label = "Acciones Persona",menu = mnuAccionPersona)
#Indicar que la barra de menus estará en la ventana
raiz.config(menu=barraMenu)
raiz.mainloop()

registrarPrueba.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
import tkinter as tk
from tkinter import messagebox
from Servicios.personaGarficaServicio import *
from Entidades.persona import Persona
 
class Application(tk.Frame):
 
    def __init__(self, ventana1):
        super().__init__(ventana1)
        self.master = ventana1
        self.titulo = ventana1.title("Datos Personales")
        self.redir = ventana1.resizable(1, 1)  # permite redimensionar a lo ancho y alto
        self.icono = ventana1.iconbitmap("..\\icono.ico")
        self.geometria = ventana1.geometry("350x220")
        self.pack(fill="both",expand="True")
        self.pack()
        self.create_cuerpo()
        #self.mainloop()
 
    def create_cuerpo(self):
 
        self.datoN = tk.StringVar()
        self.labelNombre = tk.Label(self,text="Nombres:",font=("Comic Sans MS",9))
        self.labelNombre.grid(row=0,column=2,sticky="e",padx="3",pady="3")
        self.textNombre = tk.Entry(self,textvariable = self.datoN)
        self.textNombre.focus()
        self.textNombre.grid(row=0,column=3,padx="3",pady="3")
 
        self.datoA = tk.StringVar()
        self.labelApellido = tk.Label(self, text="Apellido:", font=("Comic Sans MS", 9))
        self.labelApellido.grid(row=1, column=2, sticky="e", padx="3", pady="3")
        self.textApellido = tk.Entry(self,textvariable = self.datoA)
        self.textApellido.grid(row=1, column=3, padx="3", pady="3")
 
        self.datoC = tk.StringVar()
        self.labelCedula = tk.Label(self, text="Cédula:", font=("Comic Sans MS", 9))
        self.labelCedula.grid(row=2, column=2, sticky="e", padx="3", pady="3")
        self.textCedula = tk.Entry(self,textvariable = self.datoC)
        self.textCedula.grid(row=2, column=3, padx="3", pady="3")
 
        self.datoCo = tk.StringVar()
        self.labelCorreo = tk.Label(self, text="Correo:", font=("Comic Sans MS", 9))
        self.labelCorreo.grid(row=3, column=2, sticky="e", padx="3", pady="3")
        self.textCorreo = tk.Entry(self,textvariable = self.datoCo)
        self.textCorreo.grid(row=3, column=3, padx="3", pady="3")
 
        self.datoE = tk.IntVar()
        self.labelEdad = tk.Label(self, text="Edad:", font=("Comic Sans MS", 9))
        self.labelEdad.grid(row=4, column=2, sticky="e", padx="3", pady="3")
        self.textEdad = tk.Entry(self,textvariable = self.datoE)
        self.textEdad.grid(row=4, column=3, padx="3", pady="3")
 
        self.btnRegistrar = tk.Button(self)
        self.btnRegistrar["text"] = "Registrar"
        self.btnRegistrar["command"] = self.disparar
        self.btnRegistrar.grid(row=7,column=2,padx="3",pady="3")
 
        self.btnLimpiar = tk.Button(self)
        self.btnLimpiar["text"] = "Limpiar"
        self.btnLimpiar["command"] = self.limpiar
        self.btnLimpiar.grid(row=7, column=3, padx="3", pady="3")
 
        self.btnSalir = tk.Button(self)
        self.btnSalir["text"] = "Salir"
        self.btnSalir["command"] = self.master.destroy
        self.btnSalir.grid(row=7, column=4, padx="3", pady="3")
 
 
    def disparar(self):
        per = Persona(nombre="", apellido="", cedula="", correo="", edad="")
        try:
            per.nombre = self.datoN.get()
            per.apellido = self.datoA.get()
            per.cedula = self.datoC.get()
            per.correo = self.datoCo.get()
            per.edad = int(self.datoE.get())
            if not per:
                cont = registar(per)
            else:
                cont =0
            if cont > 0:
                messagebox.showinfo(message="Sus datos fueron actualizados satisfactoriamente!!!",
                                    title="Actualización de datos")
            else:
                messagebox.showinfo(message="Existió un problema y sus datos no se actualizarón!!!",
                                    title="Actualización de datos")
        except ValueError as e:
            messagebox.showinfo(message="Existió un problema y sus datos no se actualizarón!!!",
                                title="Actualización de datos")
 
    def limpiar(self):
        self.textNombre.delete(0,"end")
        self.textApellido.delete(0, "end")
        self.textCedula.delete(0, "end")
        self.textCorreo.delete(0, "end")
        self.textEdad.delete(0, "end")
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder