Screens in Tkinter-Python

0

I'm having trouble getting back to one screen without creating another ... I could not get her to go back to the same canvas I created. Can I use .destroy() ? Here is the code:

from tkinter import *

def janela_principal():
   janela1 = Tk()
   janela1.geometry("300x300+200+200")
   janela1 ["bg"] = "green"

bt1 = Button(janela1, text="abrir a segunda janela",command = janela_secundaria)
bt1.place(x = 50, y =100)

def janela_secundaria():
   janela2 = Tk()
   janela2.geometry("300x300+200+200")
   janela2["bg"] = "green"

bt1 = Button(janela2, text = "Voltar", command = janela_principal)
bt1.place(x = 50, y = 100)

janela_principal()
    
asked by anonymous 10.07.2018 / 05:53

1 answer

0

Whenever you instantiate janela = Tk() you open a new window.

bt1 = Button(janela2, text = "Voltar", command = janela2.destroy) can solve your problem yes.

Perhaps using Toplevel (master) instead of Tk () is better for you. I use it in conjunction with transient, which allows me to open a new window in front of the previous one and when I close the previous one, the new closes automatically.

from tkinter import *

def janela_principal():
   janela1 = Tk()
   janela1.geometry("300x300+200+200")
   janela1 ["bg"] = "green"

   bt1 = Button(janela1, text="abrir a segunda janela",command = lambda e:
   janela_secundaria(janela1)) ## der erro, apague o 'e' no lambda e:
   bt1.place(x = 50, y =100)

def janela_secundaria(master):
   janela2 = Toplevel(master)
   janela2.transient(master)
   janela2.geometry("300x300+200+200")
   janela2["bg"] = "green"

   bt1 = Button(janela2, text = "Voltar", command = janela2.destroy)
   bt1.place(x = 50, y = 100)

janela_principal()
    
18.08.2018 / 20:53