Screen printed products in Tkinter-Python

Asked

Viewed 202 times

0

I’m having trouble getting back to one screen without creating another... I couldn’t get her to come back to the same screen I created. I wonder if I could use the .destroy()?
Follow the code below:

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()
  • When I messed with TkInter, I always worked in the same window. I would touch the contents of it, but I would never destroy it

1 answer

0

Every time you urge janela = Tk() you open a new window.

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

Maybe using Toplevel(master) instead of Tk() is better for you. I use in conjunction with Transient, which allows me to open a new window in front of the previous one and when closing the previous one, the new one 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()

Browser other questions tagged

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