Re: Gnome "Quit" menu is like clicking a button in a wxPython modal dialog

Dietmar Schwertberger <[email protected]> Sun, 13 Dec 2020 12:37:47 +0100
Newsgroups gmane.comp.python.wxpython
Message-ID <[email protected]>
I have just tested with a small sample. See attachment.
The dialog returns with wx.ID_CANCEL and then the frame receives a 
EVT_CLOSE.
That's correct behaviour. "Quit" triggers the EVT_CLOSE, but the modal 
dialog needs to be closed first.

If ESC/Cancel has unwanted side effects in your application, you could 
introduce a delay. If EVT_CLOSE is received within a few ms, don't 
perform the side effect.

Regards,

Dietmar

-- 
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/30dcd60d-dde5-9293-d9c4-e230351568b8%40schwertberger.de.
dialog.py (text/plain, 3.8 KB)
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#
# generated by wxGlade 0.9.9pre on Fri Jan  3 20:14:00 2020
#

import wx

# begin wxGlade: dependencies
# end wxGlade

# begin wxGlade: extracode
# end wxGlade


class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyFrame.__init__
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.SetSize((400, 300))
        self.SetTitle("frame")

        # Menu Bar
        self.frame_menubar = wx.MenuBar()
        wxglade_tmp_menu = wx.Menu()
        item = wxglade_tmp_menu.Append(wx.ID_ANY, "Show Dialog", "")
        self.Bind(wx.EVT_MENU, self.show_modal, item)
        self.frame_menubar.Append(wxglade_tmp_menu, "Menu")
        self.SetMenuBar(self.frame_menubar)
        # Menu Bar end

        self.panel_1 = wx.Panel(self, wx.ID_ANY)

        sizer_1 = wx.BoxSizer(wx.VERTICAL)

        self.button_1 = wx.Button(self.panel_1, wx.ID_ANY, "Show Dialog (modal)")
        sizer_1.Add(self.button_1, 0, wx.ALL, 4)

        static_text_1 = wx.StaticText(self.panel_1, wx.ID_ANY, "Text from dialog:")
        sizer_1.Add(static_text_1, 0, wx.ALL, 4)

        self.text_ctrl_1 = wx.TextCtrl(self.panel_1, wx.ID_ANY, "", style=wx.TE_READONLY)
        sizer_1.Add(self.text_ctrl_1, 0, wx.ALL | wx.EXPAND, 4)

        self.panel_1.SetSizer(sizer_1)

        self.Layout()

        self.Bind(wx.EVT_BUTTON, self.show_modal, self.button_1)
        self.Bind(wx.EVT_CLOSE, self.on_close)
        # end wxGlade
    def on_close(self, event):
        print("on_close")
        event.Skip()

    def show_modal(self, event):  # wxGlade: MyFrame.<event_handler>
        # event might be a button or menu event; it's not used anyway
        # instantiate dialog and set text control to initial value
        with MyDialog(self) as dlg:
            dlg.text_ctrl_1.SetValue(self.text_ctrl_1.GetValue())
            # show as modal dialog
            res = dlg.ShowModal()
            print("res", res, wx.ID_CANCEL)
            if res == wx.ID_OK:
                # user has hit OK -> read text control value
                self.text_ctrl_1.SetValue(dlg.text_ctrl_1.GetValue())

# end of class MyFrame

class MyDialog(wx.Dialog):
    def __init__(self, *args, **kwds):
        # begin wxGlade: MyDialog.__init__
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)
        self.SetTitle("dialog")

        sizer_1 = wx.BoxSizer(wx.VERTICAL)

        static_text_1 = wx.StaticText(self, wx.ID_ANY, "Enter text:")
        sizer_1.Add(static_text_1, 0, wx.ALL, 8)

        self.text_ctrl_1 = wx.TextCtrl(self, wx.ID_ANY, "")
        sizer_1.Add(self.text_ctrl_1, 0, wx.ALL | wx.EXPAND, 8)

        sizer_1.Add((20, 20), 1, wx.EXPAND, 0)

        sizer_2 = wx.StdDialogButtonSizer()
        sizer_1.Add(sizer_2, 0, wx.ALIGN_RIGHT | wx.ALL, 4)

        self.button_OK = wx.Button(self, wx.ID_OK, "")
        self.button_OK.SetDefault()
        sizer_2.AddButton(self.button_OK)

        self.button_CANCEL = wx.Button(self, wx.ID_CANCEL, "")
        sizer_2.AddButton(self.button_CANCEL)

        sizer_2.Realize()

        self.SetSizer(sizer_1)
        sizer_1.Fit(self)

        self.SetAffirmativeId(self.button_OK.GetId())
        self.SetEscapeId(self.button_CANCEL.GetId())

        self.Layout()
        # end wxGlade

# end of class MyDialog

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, wx.ID_ANY, "")
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

# end of class MyApp

if __name__ == "__main__":
    app = MyApp(0)
    app.MainLoop()