Using screens in Tkinter-Python

0

Is everything okay with you? So, I'm doing a screenshot program, its function would just be a test consisting of: Open a screen (called by a button) and close the one that was open. I do not know how to do this, I have tried to use repetition loops, but without success ... Code: from tkinter import *

def bt_click():

    janela2 = Tk()
    teste2 = janela2
    janela2.title("Teste2")
    janela2.geometry("200x200+300+300")


janela = Tk()

bt1 = Button(janela, text = "Click me", command = bt_click)

bt1.place(x = 50, y = 100)

janela["bg"] = "green"
janela.geometry("300x300+300+300")
janela.mainloop()

My idea was: Put the whole window in a while loop, and do so:

teste = 1
while teste == 1:
   janela() ...

and when I called the other window (window 2), at the end of the function, cause the test variable to become 2 or any other number, but I think since the test variable is within the function of window2, it becomes local .. How to proceed? Hugs

    
asked by anonymous 06.07.2018 / 04:35

1 answer

0

You have to have a reference outside of the function that creates the new window for the created window - then just call the .destroy method. And this does not have to be a global variable - you can have a list that contains all the created windows - without having a variable with a fixed name for each one.

import tkinter

raiz = None
janelas = []

def criar():
    janela = tkinter.Toplevel(raiz)
    janela.title("janela  {}".format(len(janelas)))
    janelas.append(janela)

def destruir():
    janelas.pop(0).destroy()

def principal():
    global raiz
    raiz = tkinter.Tk()
    bt_criar = tkinter.Button(raiz, text="criar", command=criar)
    bt_destruir = tkinter.Button(raiz, text="destruir", command=destruir)
    bt_criar.pack(side="left")
    bt_destruir.pack(side="right")

principal()
tkinter.mainloop()
    
06.07.2018 / 23:22