0

Sorry if I was not specific with the question, but it is a bit long and it is my first time consulting programming issues. I was creating a Toplevel in Python with the Tkinter module and I wanted to block the events in the main window while it is open. Googled and I found the method "grab_set", which takes me to the other end. That is, it blocks the events in the top bar of the window (close, maximize and minimize). Is there something similar to grab_set but without this happening?

As an alternative, I can block all the entrys and buttons, but I don't know any way to unlink or re-link the binds. Is there any way?

original = Tk()
entrada = Entry(original)
entrada.pack()

def otra_ventana(Self):
    ventana = Toplevel(original)
    ventana.grab_set()

original.bind("<F1>", otra_ventana)

The above is a test code that has the problem that I mentioned.

Postscript: The problem only occurs when the Toplevel is open and an attempt is made to close the main window.

Thank you.

6
  • Hi Dante, Welcome to SO. Please take some time to look at How to ask and Tour, it will help you writing a good question. Commented Feb 26, 2020 at 11:47
  • Thanks for the advice Dorian Turba
    – Dante S.
    Commented Feb 26, 2020 at 12:11
  • stovfl Thanks the same for the link, but, as I mentioned earlier, grab_set blocks the events in the top bar of the main window. That is, I press the close button in the main window and it does not close. Try with protocol and it didn't work.
    – Dante S.
    Commented Feb 26, 2020 at 12:17
  • If you meant wait_window, it doesn't work for me either. This is because this pause the code, but, for example, I can still write in the entrys and use the binds in the main window. Thanks the same again.
    – Dante S.
    Commented Feb 26, 2020 at 12:25
  • @DanteS. "keep the X button running": Now I have two X, and both working for me? Did you claim you can't close any of the windwo by clicking X?
    – stovfl
    Commented Feb 26, 2020 at 13:48

2 Answers 2

0

Well if you do not like what grab_set is doing then you could simply disable all widgets in the root window and re-enable them after the top level is closed.

Using winfo_children() we can set up a function to toggle the state of widgets in the root window.

Combined with protocol() we can decide what happens when the toplevel window is closed.

Code:

import tkinter as tk


def toggle_root_state(state='normal'):
    for child in original.winfo_children():
        try:
            child['state'] = state
        except:
            pass


def otra_ventana(_=None):
    ventana = tk.Toplevel(original)
    ventana.protocol("WM_DELETE_WINDOW", lambda: (toggle_root_state(), ventana.destroy()))
    toggle_root_state('disabled')


original = tk.Tk()
original.geometry('225x50')
entrada = tk.Entry(original)
entrada.pack()
original.bind("<F1>", otra_ventana)
original.mainloop()
5
  • I thought the same as you, but not so well coded. Unfortunately, that method does not deactivate binds. I would say "use unbind and then create them again", but I create binds in a personal module, which does not make it so simple.That is why, in the question, I mentioned that as an alternative a method was used to deactivate and reactivate binds. Thanks the same for the dedication.
    – Dante S.
    Commented Feb 26, 2020 at 14:55
  • the same is a possible solution
    – Dante S.
    Commented Feb 26, 2020 at 14:58
  • I was analyzing my module and I think I can play with the conditionals and what you proposed to achieve what I want to achieve. I am going to try and let you know as soon as I have it ready.
    – Dante S.
    Commented Feb 26, 2020 at 15:05
  • If you also want to deactivate the bind you can do so in the function.
    – Mike - SMT
    Commented Feb 26, 2020 at 15:59
  • @DanteS. I have added the functionality to add or remove binding.
    – Mike - SMT
    Commented Feb 26, 2020 at 16:01
0

Ready, I'm done. Thanks to all for the help. For those who have this problem I leave the solution that occurred to me based on my conclusions and the Mike - SMT code.

from tkinter import *

original = Tk()
original.active = True # enter this variable in class Tk
entrada = Entry(original)
entrada.pack()

def toogle(Self=None):
    original.active = not original.active

    for widget in original.winfo_children():
        # only change the state if "state" exists in config and the widget is not Label
        if(widget.config().get("state") and widget.winfo_class() != "Label"):
            widget["state"] = {True:"normal", False:"disabled"}[original.active]

def otra_ventana(Self):
    if(original.active):
        ventana = Toplevel(original)
        ventana.protocol("WM_DELETE_WINDOW", lambda:(toogle(), ventana.destroy()))
        toogle()

original.bind("<F1>", otra_ventana)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.