首页 > 解决方案 > 使用 tkinter 和 pymysql 使用 Python 创建日志系统时出现问题

问题描述

真的问题不在于tkinter,而是在数据库中注册数据时,它不是在执行try:,而是except:显示未注册的消息。

import tkinter as tk
from tkinter import *
from tkinter import messagebox

import pymysql
    
    bd = pymysql.connect(
        host="localhost",
        user="root",
        passwd="",
        db="informacion_poblacion"


        )

    cursor=bd.cursor()    
    sql = "INSERT INTO poblacion(id, Nombres, Apellidos, Tipo_doc, Documento, Pais_origen, Tiempo_permanencia, Genero, Fecha_nacimiento, Edad, Estado_civil, Numero_hijos, Direccion_residencia, Barrio_residencia, Telefono_contacto, Ocupacion, Nivel_escolarida, Eps, Regimen, Email, Antecedentes_medicos, Convenio, Contact_emergency )\
        VALUES (NOT NULL,'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}')".format(nom.get(), apell.get(), t_doc.get(), doc.get(), pai_ori.get(), tiem_perm.get(), gen.get(), fech_nac.get(), age.get(), est_civ.get(), num_hij.get(), dir_resid.get(), bar_resid.get(), tel_cont.get(), ocup.get(), niv_esc.get(), ep.get(), regim.get(), emai.get(), ant_med.get(), conv.get(), emerg.get())
    try:
        cursor.execute(sql)
        bd.commit()
        messagebox.showinfo(message="Registro Exitoso", title="Aviso")
    except:
        bd.rollback()
        messagebox.showinfo(message="No registrado", title="Aviso")

        bd.close()

def eliminar():
    bd = pymysql.connect(
        host="localhost",
        user="root",
        passwd="",
        db="informacion_poblacion"


        )

    cursor=bd.cursor()    
    sql = "DELETE FROM poblacion WHERE Documento='"+doc.get()+"'"
    try:
        cursor.execute(sql)
        bd.commit()
        messagebox.showinfo(message="Borrado Exitoso", title="Aviso")
    except:
        bd.rollback()
        messagebox.showinfo(message="No eliminado", title="Aviso")

        bd.close()

def actualizar():
    bd = pymysql.connect(
        host="localhost",
        user="root",
        passwd="",
        db="informacion_poblacion"


        )

    cursor=bd.cursor()    
    sql = "UPDATE informacion_poblacion SET Nombres='"+nom.get()+"',Apellidos='"+apell.get()+"',Tipo_doc='"+t_doc.get()+"',Documento='"+doc.get()+"', Pais_origen='"+pai_ori.get()+"', Tiempo_permanencia='"+tiem_perm.get()+"', Genero='"+gen.get()+"', Fecha_nacimiento='"+fech_nac.get()+"', Edad='"+age.get()+"', Estado_civil='"+est_civ.get()+"', Numero_hijos='"+num_hij.get()+"', Direccion_residencia='"+dir_resid.get()+"', Barrio_residencia='"+bar_resid.get()+"', Telefono_contacto='"+tel_cont.get()+"', Ocupacion='"+ocup.get()+"', Nivel_escolarida='"+niv_esc.get()+"', Eps='"+ep.get()+"', Regimen='"+regim.get()+"', Email='"+emai.get()+"', Antecedentes_medicos='"+ant_med.get()+"', Convenio='"+conv.get()+"', Contact_emergency='"+emerg.get()+"' WHERE Nombre'"+nom.get()+"'"
    try:
        cursor.execute(sql)
        bd.commit()
        messagebox.showinfo(message="Actualización Exitosa", title="Aviso")
    except:
        bd.rollback()
        messagebox.showinfo(message="No actualizado", title="Aviso")

        bd.close()

def salir():
    vn.destroy()

#Creación de ventana
vn = tk.Tk()
vn.title("Formulario de registro Hospital San Isidro") #Titulo de la ventana(user interface) UI
vn.geometry("500x700") #Dimensiones de la ventana
vn.configure(background="light grey") #Color de fondo de la UI

left_frame = tk.Frame(vn, width=300, height=300, bg='grey')
left_frame.pack(side='left', fill='both', padx=10, pady=5, expand=True)
        
right_frame = tk.Frame(vn, width=300, height=300, bg='grey')
right_frame.pack(side='right', fill='both', padx=10, pady=5, expand=True)

# Insertar etiquetas y cuadros de texto
e0 = tk.Label(left_frame, text="Nombre(s)", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e0.pack(padx=5, pady=5) # fill permite el escalado de los paneles al modificar la ventana
nom = tk.Entry(left_frame) #donde se ingresara los datos por texto
nom.pack(padx=5, pady=5)

e1 = tk.Label(left_frame, text="Apellido(s)", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e1.pack(padx=5, pady=5)
apell = tk.Entry(left_frame)
apell.pack(padx=5, pady=5)

e2 = tk.Label(left_frame, text="Numero de documento", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e2.pack(padx=5, pady=5)
doc = tk.Entry(left_frame)
doc.pack(padx=5, pady=5)

e3 = tk.Label(left_frame, text="Correo electrónico", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e3.pack(padx=5, pady=5)
emai = tk.Entry(left_frame)
emai.pack(padx=5, pady=5)

e4 = tk.Label(left_frame, text="Tipo de documento", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e4.pack(padx=5, pady=5)
t_doc = tk.Entry(left_frame)
t_doc.pack(padx=5, pady=5)

e5 = tk.Label(left_frame, text="Pais de origen", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e5.pack(padx=5, pady=5)
pai_ori = tk.Entry(left_frame)
pai_ori.pack(padx=5, pady=5)

e6 = tk.Label(left_frame, text="Tiempo de permanencia en meses", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e6.pack(padx=5, pady=5)
tiem_perm = tk.Entry(left_frame)
tiem_perm.pack(padx=5, pady=5)

e7 = tk.Label(left_frame, text="Genero", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e7.pack(padx=5, pady=5)
gen = tk.Entry(left_frame)
gen.pack(padx=5, pady=5)

e8 = tk.Label(left_frame, text="Fecha de nacimiento", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e8.pack(padx=5, pady=5)
fech_nac = tk.Entry(left_frame)
fech_nac.pack(padx=5, pady=5)

e9 = tk.Label(left_frame, text="Edad", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e9.pack(padx=5, pady=5)
age = tk.Entry(left_frame)
age.pack(padx=5, pady=5)

e10 = tk.Label(left_frame, text="Estado civil", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e10.pack(padx=5, pady=5)
est_civ = tk.Entry(left_frame)
est_civ.pack(padx=5, pady=5)

e11= tk.Label(right_frame, text="Número de hijos", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e11.pack(padx=5, pady=5)
num_hij = tk.Entry(right_frame)
num_hij.pack(padx=5, pady=5)

e12 = tk.Label(right_frame, text="Direción de residencia", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e12.pack(padx=5, pady=5)
dir_resid = tk.Entry(right_frame)
dir_resid.pack(padx=5, pady=5)

e13 = tk.Label(right_frame, text="Barrio de residencia", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e13.pack(padx=5, pady=5)
bar_resid = tk.Entry(right_frame)
bar_resid.pack(padx=5, pady=5)

e14 = tk.Label(right_frame, text="Teléfono de contacto", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e14.pack(padx=5, pady=5)
tel_cont = tk.Entry(right_frame)
tel_cont.pack(padx=5, pady=5)

e15 = tk.Label(right_frame, text="Ocupación", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e15.pack(padx=5, pady=5)
ocup = tk.Entry(right_frame)
ocup.pack(padx=5, pady=5)

e16 = tk.Label(right_frame, text="Nivel de escolaridad", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e16.pack(padx=5, pady=5)
niv_esc = tk.Entry(right_frame)
niv_esc.pack(padx=5, pady=5)

e17 = tk.Label(right_frame, text="EPS", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e17.pack(padx=5, pady=5)
ep = tk.Entry(right_frame)
ep.pack(padx=5, pady=5)

e18 = tk.Label(right_frame, text="Régimen", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e18.pack(padx=5, pady=5)
regim =tk.Entry(right_frame)
regim.pack(padx=5, pady=5)

e19 = tk.Label(right_frame, text="Contacto en caso de emergencia", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e19.pack(padx=5, pady=5)
emerg= tk.Entry(right_frame)
emerg.pack(padx=5, pady=5)

e20 = tk.Label(right_frame, text="Antecedentes médicos", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e20.pack(padx=5, pady=5)
ant_med= tk.Entry(right_frame)
ant_med.pack(padx=5, pady=5)

e21 = tk.Label(right_frame, text="Convenio", bg="gray", fg="white") # bg-sombreado, fg-color_letra
e21.pack(padx=5, pady=5)
conv= tk.Entry(right_frame)
conv.pack(padx=5, pady=5)

boton = tk.Button(left_frame, text="Registrar", fg="black", command=insertar_datos)
boton.pack(side='left', padx=5, pady=5)

boton1 = tk.Button(left_frame, text="Consultar", fg="black")
boton1.pack(side='left', padx=5, pady=5)

boton2 = tk.Button(left_frame, text="Actualizar", fg="black")
boton2.pack(side='left', padx=5, pady=5)

boton3 = tk.Button(left_frame, text="Eliminar", fg="black", command=eliminar)
boton3.pack(side='left', padx=5, pady=5)

boton5 = tk.Button(right_frame, text="IMC", fg="black")
boton5.pack(side='left', padx=5, pady=5)

boton5 = tk.Button(right_frame, text="MCV", fg="black")
boton5.pack(side='left', padx=5, pady=5)

boton4 = tk.Button(right_frame, text="salir", fg="black", command=salir) 
boton4.pack(side='right', padx=5, pady=5)

vn.mainloop() # la ventana estará en pantalla hasta que decidamos cerrarla.

我认为问题可能出在try:函数中,但我一直在检查,但找不到错误在哪里。

标签: pythontkinterpymysql

解决方案


推荐阅读