Question

I'm using a class called customDialog, based on Based on http://effbot.org/tkinterbook/tkinter-dialog-windows.htm The standard code creates a class which subclasses Toplevel, and creates modal dialogs. I'm trying to adapt the code so it can also create modeless dialogs. The standard code calls the Toplevel constructor, creates the dialog and then does self.wait_window(self) at the end, which waits until the dialog window is dismissed. But I'm not succeeding.

My naive attempt was to simply skip the wait_window() (that's the attempt in my code snipped below). I've tried a few other things, but the dialog remains modal, that is, I can't interact with the application's main window. What is forcing it to be modal? The dialogs contain widgets and a few buttons, typically.

class Dialog(Toplevel):

    def __init__(self, parent, title = None,modal=True):
        Toplevel.__init__(self, parent)
        self.transient(parent)
        if title:
            self.title(title)
        self.parent = parent
        self.result = None
        body = Frame(self)
        #register validators
        self.validatePosInt = (body.register(self.OnValidatePosInt),
                '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')

        self.initial_focus = self.body(body)   #this calls the body function which is overridden, and which draws the dialog
        body.grid()
        self.buttonbox()
        self.grab_set()
        if not self.initial_focus:
            self.initial_focus = self
        self.protocol("WM_DELETE_WINDOW", self.cancel)
        self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
                                  parent.winfo_rooty()+50))
        self.initial_focus.focus_set()

        if modal:
            self.wait_window(self)
Was it helpful?

Solution

Remove the line that says self.grab_set(). A "grab" means to grab all events and direct them toward this window, which you don't want.

If you read a bit deeper into the web page where you got the code you'll see that it mentions this exact line and why it is there:

... Next, the constructor creates the dialog body, and then calls grab_set to make the dialog modal

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top