Tkinter focus a window ignoring the back?

Asked

Viewed 7,019 times

7

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

The problem is that if I click on the window that this "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 anyone. When I click the window open button again it should not open the button again but switch to the window.

I don’t know if you can do this, I saw some things about Grab but I don’t really understand if anyone can help me thank you from now on!

#-*- 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()
  • 1

    It would be nice if you could add to your questions, the code you already have and indicate through it what your doubt, it becomes easier for people to help you, as well as increase their interest in your question

  • Ready now I think you’ll understand!

  • I made an edit on your question, check if this is exactly what you are asking, because as you did not put points or line breaks it was difficult to understand

  • Let’s see how I can explain it better, think about the following frame called frame1, and a button that opens another frame that is called frame2, when it appears frame2 I want it to occur the following, not being able to select frame1 in any way until frame2 has been closed, I think it’s something like wait_window! anything I have Skype and I can show you what I really want through the remote. PS: Vlw same guy for striving to be able to help me!

  • I suggest you add this explanation to your question, edit it in a way that makes it clear, as you left with this comment... it is easier for those who know how to help

  • blz but I managed to solve my problem here I walked giving a studied and I managed to solve using Toplevel and Transient!

  • then as soon as you can, put your answer, explaining how you resolved, one’s doubt may be another’s doubt

Show 2 more comments

2 answers

3


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()
  • Thank you so much for your help my dear, I didn’t know what to do I really broke my head to understand and you show me in a clear and simple way to do this, have a great day!

1

I managed to solve my problem as follows... note that when opening the window by clicking on open the open window becomes the focus and there is no way to touch the back on how much not to 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()

Browser other questions tagged

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