Tkinter focus on a window ignoring the back?

5

In my code there is a frame with a button and it is called "Open", clicking "Open" will bring up another window.

The problem is that if I click on the window that is the "Open" button again, it will open a new window.

What I want is to be able to switch between all the open windows by selecting any one. When I click again on the open button of the window it should not open the button again but instead switch to the window.

I do not know if I can do this, I saw some things about grab but I do not understand much if someone can help me thank you right away!

#-*- coding:UTF-8 -*-
from Tkinter import *

class novo:

    def __init__(self, janela):

        self.caixa=Frame(janela)
        self.caixa.grid()
        self.b=Button(janela, text='Abrir', command=self.new_jan)
        self.b.grid()
        self.l1=Label(janela, text='raiz!')
        self.l1.grid()

    def new_jan(self):
        jan=Tk()
        self.l=Label(jan, text='apenas fechando essa janela poderá voltar ou clicar na raiz!')
        self.l.grid()
        jan.geometry('300x200')

root=Tk()

novo(root)
root.geometry('300x200')

root.mainloop()
novo()
    
asked by anonymous 25.06.2014 / 13:24

2 answers

2

His original proposal was simply to bring the window forward when it had already been opened. This is accomplished by the following code:

#-*- coding:UTF-8 -*-
from Tkinter import *

class novo:

    def __init__(self, janela):
        # Inicia como None
        self.jan = None
        self.caixa=Frame(janela)
        self.caixa.grid()
        self.b=Button(janela, text='Abrir', command=self.new_jan)
        self.b.grid()
        self.l1=Label(janela, text='raiz!')
        self.l1.grid()

    def new_jan(self):
        # Verifica se já foi criada
        if self.jan is None:
            self.jan=Tk()
            self.jan.protocol("WM_DELETE_WINDOW", self.fecha_jan)
            self.l=Label(self.jan, text='apenas fechando essa janela poderá voltar ou clicar na raiz!')
            self.l.grid()
            self.jan.geometry('300x200')
        else:
            # Se já foi, basta colocá-la na frente
            self.jan.lift()

    def fecha_jan(self):
        # Seta de novo em None para recriar quando abrir
        self.jan.destroy()
        self.jan = None

root=Tk()

novo(root)
root.geometry('300x200')

root.mainloop()
novo()
    
15.08.2014 / 20:11
1

I was able to solve my problem as follows ... Note that when you open the window by clicking open the open window it becomes the focus and you can not move the window until you close it!

#-*- coding:UTF-8 -*-
from Tkinter import *
root=Tk()

class novo:

        def __init__(self, janela):
            self.caixa=Frame(janela)
            self.caixa.grid()
            self.b=Button(janela, text='Abrir', command=self.new_jan)
            self.b.grid()
            self.l1=Label(janela, text='raiz!')
            self.l1.grid()

        def new_jan(self):
            self.jan=Toplevel()
            self.l=Label(self.jan, text='Feche esta para poder voltar a raiz!')
            self.l.grid()
            b=Button(self.jan, text='Fechar', command=self.fecha_jan)
            b.grid()
            self.jan.geometry('300x200')
            self.jan.transient(root)#
            self.jan.focus_force()#
            self.jan.grab_set()#

        def fecha_jan(self):
            self.jan.destroy()


novo(root)

root.geometry('300x200')

root.mainloop()
    
28.06.2014 / 12:42