Close an easygui Python script with the standard 'close' button

Question!

I've created a very simple app, which presents an easygui entrybox() and continues to loop this indefinitely as it receives user input.

I can quit the program using the Cancel button as this returns None, but I would also like to be able to use the standard 'close' button to quit the program. (ie. top right of a Windows window, top left of a Mac window) This button currently does nothing.

Taking a look at the easygui module I found this line:

root.protocol('WM_DELETE_WINDOW', denyWindowManagerClose )

This would seem to be the culprit. I'm no TKinter expert but I could probably work out how to alter this handler to act the way I want.

However, as I'd rather not mess about with the easygui module, Is there a way to override this behavior from my main script, and have the close button either close the program outright or return None?



Answers

I found a solution, the answer is below the choice box, when he defines some functions (you can just type to find denyWindowManagerClose) and go to where he defines it. Just erase it and put this code in its place.

    def denyWindowManagerClose():
#------------------------------------------------------------------
#  Changed by ProgrammingBR
#  Enables the user to close the window without entering a value
#  Youtube URL: https://www.youtube.com/channel/UCTZh6kWz_iYACNE6Jcy2lhw
#------------------------------------------------------------------
    global __enterboxText
    __enterboxText = None
    boxRoot.quit()

You can create a backup file, but this will work for all boxes, the suggestions given here aside from this will not work, I tried the them. if you want to see I have a video on it (it is in Portuguese, but you can follow it) just go to the youtube channel, I will upload it soon and post the link here :)



It would require altering the easygui module, yes. I will get it modified!

** I have sent in a e-mail to the EasyGUI creator explaning this [12:12 PM, January 23/09]

** I just want to say that the possibility of this change happening - if at all, which I doubt - is very tiny. You see, EasyGUI is intended to be a simple, discrete way to create GUIs. I think that this addition wouldn't help any, especially since the interface is very sequential, so it would be confusing to new users. [12:19 PM, January 23/09]

** The EasyGUI creator said this in reply to my e-mail:

An easygui dialog should never exit the application -- it should pass back a value to the caller and let the caller decide what to do.

But this is an interesting idea. Rather than simply ignoring a click on the "close" icon, easygui boxes could return the same value that a click on the "cancel" button would return. I'll meditate on this.

-- Steve Ferg

I think that this is progress at least. [2:40 PM, January 23/09]



I don't know right now, but have you tried something like this?:

root.protocol('WM_DELETE_WINDOW',  self.quit)

or

root.protocol('WM_DELETE_WINDOW',  self.destroy)

I haven't tried, but google something like "Tkinter protocol WM_DELETE_WINDOW"

By : Angel


This video can help you solving your question :)
By: admin