0

Hola estoy intentando hacer un botón pero me manda un error tengo 2 ventanas la principal y la secundaria cuando pulso el botón para ver la ventana secundaria destruyo la principal para que solo se vea la segunda ahora al momento de volver a la ventana principal no puedo creo una función donde al pulsar el botón de regresar destruya esa ventana y me muestre la principal pero no funciona me podrían ayudar porfa. No se como hacerlo de otra manera estoy aprendiendo pero no encontre de que manera se hace.

from tkinter import *
import tkinter
from tkinter import font

ventana = tkinter.Tk()
ventana.title("SISECOVE")
ventana.geometry("800x500+350+200")
ventana.resizable(0,0)

def admin():
    user1 = tkinter.Tk()
    user1.title("Administrador")
    user1.geometry("500x500+500+200")
    user1.resizable(0,0)
    ventana.destroy()

    a_boton1= tkinter.Button(user1, text="Ingresar")
    a_boton1.place(x=220,y=300)

    a_boton2=tkinter.Button(user1, text="Regresar", command=atras)
    a_boton2.place(x=220, y=400)

    t_user=Entry(user1)
    t_user.place(x=180, y=100)
    l_user=tkinter.Label(user1, text="USUARIO:")
    l_user.place(x=210, y=70)

    t_pass=Entry(user1)
    t_pass.place(x=180, y=200)
    l_pass=tkinter.Label(user1,text="CONTRASEÑA:")
    l_pass.place(x=200, y=170)

def salir():
    ventana.destroy()

def encarg():
    user2 = tkinter.Tk()
    user2.title("Encargado")
    user2.geometry("500x500+500+200")
    user2.resizable(0,0)
    ventana.destroy()

    e_a_boton1= tkinter.Button(user2, text="Ingresar")
    e_a_boton1.place(x=220,y=300)

    e_a_boton2=tkinter.Button(user2, text="Regresar")
    e_a_boton2.place(x=220, y=400)

    e_t_user=Entry(user2)
    e_t_user.place(x=180, y=100)
    e_l_user=tkinter.Label(user2, text="USUARIO:")
    e_l_user.place(x=210, y=70)

    e_t_pass=Entry(user2)
    e_t_pass.place(x=180, y=200)
    e_l_pass=tkinter.Label(user2,text="CONTRASEÑA:")
    e_l_pass.place(x=200, y=170)

etiqueta1 = tkinter.Label(ventana, text= "SISECOVE", font=("Arial", 18))
etiqueta1.place(x=330, y=20)

boton1 = tkinter.Button(ventana, text="Administrador", command= admin)
boton1.place(x=300,y=220)

boton2 = tkinter.Button(ventana,text="Encargado", command= encarg)
boton2.place(x=400,y=220)

boton3 = tkinter.Button(ventana, text="Salir", command= salir)
boton3.place(x=720,y=420)

ventana.mainloop()
1

1 respuesta 1

1

Nunca elimines una ventana u objeto principal en Tkinter, lo mejor que puedes hacer con la ventana principal 'ventana', es simplemente ocultarla, y creas la secundaria que sea hija de la principal, asi cuando quieras volver, destruyes la secundaria y vuelves a mostrar la principal. Si necesitas que te lo explique mejor me lo dices, pero esta bastante claro. Saludos.

from tkinter import *
import tkinter
from tkinter import font

ventana = tkinter.Tk()
ventana.title("SISECOVE")
ventana.geometry("800x500+350+200")
ventana.resizable(0, 0)


def atras(ventana_a_cerrar):
    ventana_a_cerrar.destroy()
    ventana.deiconify()


def admin():
    user1 = tkinter.Toplevel(ventana)
    user1.title("Administrador")
    user1.geometry("500x500+500+200")
    user1.resizable(0, 0)
    # ventana.destroy() No la destruyas, mejor ocultala
    ventana.withdraw()# Ocultala

    a_boton1 = tkinter.Button(user1, text="Ingresar")
    a_boton1.place(x=220, y=300)

    a_boton2 = tkinter.Button(user1, text="Regresar", command=lambda: atras(user1))
    a_boton2.place(x=220, y=400)

    t_user = Entry(user1)
    t_user.place(x=180, y=100)
    l_user = tkinter.Label(user1, text="USUARIO:")
    l_user.place(x=210, y=70)

    t_pass = Entry(user1)
    t_pass.place(x=180, y=200)
    l_pass = tkinter.Label(user1, text="CONTRASEÑA:")
    l_pass.place(x=200, y=170)


def salir():
    ventana.destroy()


def encarg():
    user2 = tkinter.Tk()
    user2.title("Encargado")
    user2.geometry("500x500+500+200")
    user2.resizable(0, 0)
    # ventana.destroy() No la destruyas
    ventana.withdraw() # Ocultala

    e_a_boton1 = tkinter.Button(user2, text="Ingresar")
    e_a_boton1.place(x=220, y=300)

    e_a_boton2 = tkinter.Button(user2, text="Regresar",command=lambda: atras(user2))
    e_a_boton2.place(x=220, y=400)

    e_t_user = Entry(user2)
    e_t_user.place(x=180, y=100)
    e_l_user = tkinter.Label(user2, text="USUARIO:")
    e_l_user.place(x=210, y=70)

    e_t_pass = Entry(user2)
    e_t_pass.place(x=180, y=200)
    e_l_pass = tkinter.Label(user2, text="CONTRASEÑA:")
    e_l_pass.place(x=200, y=170)


etiqueta1 = tkinter.Label(ventana, text="SISECOVE", font=("Arial", 18))
etiqueta1.place(x=330, y=20)

boton1 = tkinter.Button(ventana, text="Administrador", command=admin)
boton1.place(x=300, y=220)

boton2 = tkinter.Button(ventana, text="Encargado", command=encarg)
boton2.place(x=400, y=220)

boton3 = tkinter.Button(ventana, text="Salir", command=salir)
boton3.place(x=720, y=420)

ventana.mainloop()
2
  • Gracias me funciono no sabia que no podía destruir la ventana principal de ahora en adelante la ocultare solamente el 8 feb. 2022 a las 3:38
  • @BrayanPerezContreras si la respuesta te ha ayudado y resuelve el problema podrías marcarla como aceptada :)
    – Adrian
    el 8 feb. 2022 a las 14:15

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.