Python + Tkinter Screen Management

Asked

Viewed 983 times

2

I’m creating a system with a few screens, Every time I click on the button to call the screen it creates a new instance of the same screen, so if the user keeps clicking on the same button several new screens will be created equal. I want that when opening a screen can only open another of the same if the first is closed.

Follows Code:

from tkinter import *
import time



#MENU PRINCIPAL DO SISTEMA
janelamenu = Tk()

def janela_criar_usuario():
    janelacriarusuario = Tk()

    janelacriarusuario.geometry("600x600+250+50")
    #janelacriarusuario.mainloop()

def janela_edit_usuario():
    janelaeditusuario = Tk()

    janelaeditusuario.geometry("600x600+250+50")
    janelaeditusuario.mainloop()


menu_bt_criar = Button(janelamenu, text='Criar Usuario',command=janela_criar_usuario)
menu_bt_edituser = Button(janelamenu, text='Editar Usuario',command=janela_edit_usuario)
menu_bt_criar.place(x=30,y=100)
menu_bt_edituser.place(x=30,y=130)

janelamenu.geometry("200x600+50+50")
janelamenu.title("Menu Principal")
janelamenu.mainloop()

1 answer

0


The solution I found was to open a main window with the system menu displaying the two buttons with the create and edit user options. When one of the buttons is pressed the main window is destroyed and a new window with the same name is created with new elements and a back button to the main menu. This way the user will not be able to open more than one window at the same time.

from tkinter import *

janela = Tk()
label = Label(janela, text='Menu Principal')
label.pack()


def criar_usuario():
    global janela
    janela.destroy()
    janela = Tk() 
    label = Label(janela, text='Criar Usuario')
    label.pack()
    janela.geometry("600x600+250+50")
    menu_bt = Button(janela, text='Voltar/Menur',command=menu)
    menu_bt.pack()


def editar_usuario():
    global janela
    janela.destroy()
    janela = Tk() 
    label = Label(janela, text='Editar Usuario')
    label.pack()
    janela.geometry("600x600+250+50")
    menu_bt = Button(janela, text='Voltar/Menur',command=menu)
    menu_bt.pack()


def menu():
    global janela
    janela.destroy()
    janela = Tk()

    label = Label(janela, text='Menu Principal')
    label.pack()

    menu_bt_criar = Button(janela, text='Criar Usuario',command=criar_usuario)
    menu_bt_edituser = Button(janela, text='Editar Usuario',command=editar_usuario)

    menu_bt_criar.place(x=30,y=100)
    menu_bt_edituser.place(x=30,y=130)

    janela.geometry("600x600+250+50")
    janela.title("Menu Principal")

    janela.mainloop()


menu_bt_criar = Button(janela, text='Criar Usuario',command=criar_usuario)
menu_bt_edituser = Button(janela, text='Editar Usuario',command=editar_usuario)

menu_bt_criar.place(x=30,y=100)
menu_bt_edituser.place(x=30,y=130)

janela.geometry("600x600+250+50")
janela.title("Menu Principal")

janela.mainloop()
  • Great solution! I managed to solve my problem, thank you very much!

  • I’m glad my answer helped you. Hug and see you next time!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.