Unable to destroy app

I am trying to develop a Dialog that asks a question, stores it and return the answer when asked.

This code below works, but my main issue is that I am not being to finish the application opened by wxPython… The Dialog itself disappears but the python icon is still in my taskbar (I am using Mac - High Sierra).

You will see below that I am trying all the codes (that I know of) to close it… Destroy, del, Close, EndModal, Exit, even sys.exit

Can anyone help me to close the application?? Is there something wrong with my code?

class YesNoDialog(wx.Dialog):
def init(self, app):
wx.Dialog.init(self, None, -1, style=wx.DEFAULT_DIALOG_STYLE, title=“TOOL”)
windowSizer = wx.GridBagSizer(1,1)
windowSizer.Add(wx.StaticText(self, -1, “Do you want to continue?”), (0, 0), span=(1, 10), flag=wx.ALL, border=10)

 yes = wx.Button(self, -1, "Yes", size=(50, 30))
 no = wx.Button(self, -1, "No", size=(50, 30))

 windowSizer.Add(yes, (1, 0), flag=wx.ALL, span=(1, 2), border=10)
 windowSizer.Add(no, (1, 3), span=(1, 3), flag=wx.ALL, border=10)

 yes.Bind(wx.EVT_BUTTON, lambda event: self.stop(event, 1))
 no.Bind(wx.EVT_BUTTON, lambda event: self.stop(event, 2))
 self.Bind(wx.EVT_CLOSE, lambda event: app.Exit())

 self.SetSizerAndFit(windowSizer)
 self.Center()

def stop(self, event, step):
self.EndModal(step)
self.Close()
self.Destroy()
event.skip()

``

class StepDialog(wx.Dialog):
def init(self):
self.app = wx.App(0)
self.app.MainLoop()
self.step = YesNoDialog(self.app).ShowModal()

def tearDown(self):
sys.exit(0)

def getStep(self):
self.step.Exit()
wx.CallLater(5, self.tearDown())
self.step.Destroy()
self.Destroy()
self.app.Exit()
self.app.ExitMainLoop()
del self.app
return self.step

``

main.py
def main():
step = StepDialog().getStep()

``

It’s not clear to me why works at all. Your StepDialog class
derives from wx.Dialog, but it is not actually a dialog at all.
It’s just a plain function. Further, its constructor calls
wx.MainLoop. which should not return until the last window closes.
You should have been blocked at that point.
If ALL you are doing is a modal dialog, then you do not need a main
loop. The ShowModal function starts its own main loop. This code
works:

···

Gabrielle C wrote:

        I am trying to develop a

Dialog that asks a question, stores it and return the answer
when asked.

        This code below works, but my main issue is that I am not

being to finish the application opened by wxPython… The
Dialog itself disappears but the python icon is still in my
taskbar (I am using Mac - High Sierra).

`import wx``

``

``class YesNoDialog(wx.Dialog):``

``  def __init__(self, app):``

``           wx.Dialog.__init__(self, None, -1,

style=wx.DEFAULT_DIALOG_STYLE, title=“TOOL”)``

``     windowSizer = wx.GridBagSizer(1,1)``

``           windowSizer.Add(wx.StaticText(self, -1, "Do you want

to continue?"), (0, 0), span=(1, 10), flag=wx.ALL, border=10)``

``

``     yes = wx.Button(self, -1, "Yes", size=(50, 30))``

``     no = wx.Button(self, -1, "No", size=(50, 30))``

``

``           windowSizer.Add(yes, (1, 0), flag=wx.ALL, span=(1, 2),

border=10)``

``           windowSizer.Add(no, (1, 3), span=(1, 3), flag=wx.ALL,

border=10)``

``

``           yes.Bind(wx.EVT_BUTTON, lambda event: self.stop(event,

1))``

``           no.Bind(wx.EVT_BUTTON, lambda event: self.stop(event,

2))``

``     self.Bind(wx.EVT_CLOSE, lambda event: app.Exit())``

``

``     self.SetSizerAndFit(windowSizer)``

``     self.Center()``

``

``  def stop(self, event, step):``

``     self.EndModal(step)``

``     event.Skip()``

``

``def main():``

``    app = wx.App(0)``

``    step = YesNoDialog(app)``.ShowModal()``

``    print( step )``

``

``main()``

`
-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

Hi Tim,

Thanks for your help…

You are right, my StepDialog should not derive from Dialog… the Dialog though should in fact block the rest of the application because the answer of it will define the next action in my main.

That said, I tried to adapt my code to what you said (removing the StepDialog in between), it didn’t work because my main continued doing the rest of the code without waiting for the answer in the Dialog…

I tried then, to just adapt considering your code with your main still being my StepDialog, but even without the MainLoop, the application does not close :confused:

···

On 19 July 2018 at 15:08, Tim Roberts timr@probo.com wrote:

Gabrielle C wrote:

        I am trying to develop a

Dialog that asks a question, stores it and return the answer
when asked.

        This code below works, but my main issue is that I am not

being to finish the application opened by wxPython… The
Dialog itself disappears but the python icon is still in my
taskbar (I am using Mac - High Sierra).

-- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
It's not clear to me why works at all.  Your StepDialog class

derives from wx.Dialog, but it is not actually a dialog at all.
It’s just a plain function. Further, its constructor calls
wx.MainLoop. which should not return until the last window closes.
You should have been blocked at that point.

If ALL you are doing is a modal dialog, then you do not need a main

loop. The ShowModal function starts its own main loop. This code
works:

`import wx``

``

``class YesNoDialog(wx.Dialog):``

``  def __init__(self, app):``

``           wx.Dialog.__init__(self, None, -1,

style=wx.DEFAULT_DIALOG_STYLE, title=“TOOL”)``

``     windowSizer = wx.GridBagSizer(1,1)``

``     windowSizer.Add(wx.StaticText(      self, -1, "Do you want

to continue?"), (0, 0), span=(1, 10), flag=wx.ALL, border=10)``

``

``     yes = wx.Button(self, -1, "Yes", size=(50, 30))``

``     no = wx.Button(self, -1, "No", size=(50, 30))``

``

``           windowSizer.Add(yes, (1, 0), flag=wx.ALL, span=(1, 2),

border=10)``

``           windowSizer.Add(no, (1, 3), span=(1, 3), flag=wx.ALL,

border=10)``

``

``           yes.Bind(wx.EVT_BUTTON, lambda event: self.stop(event,

1))``

``           no.Bind(wx.EVT_BUTTON, lambda event: self.stop(event,

2))``

``     self.Bind(wx.EVT_CLOSE, lambda event: app.Exit())``

``

``     self.SetSizerAndFit(windowSizer)``

``     self.Center()``

``

``  def stop(self, event, step):``

``     self.EndModal(step)``

``     event.Skip()``

``

``def main():``

``    app = wx.App(0)``

``    step = YesNoDialog(app)``.ShowModal()``

``    print( step )``

``

``main()``

`

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 wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

If you call ShowModal, that cannot happen. ShowModal, by
definition, blocks until the dialog calls EndModal. This is NOT
true if you call Show, which makes it non-modal.
Well, of course we don’t have any idea what the rest of your code
looks like. Is this a wx application, or are you just popping up a
dialog in an otherwise non-GUI app?

···

Gabrielle C wrote:

      You

are right, my StepDialog should not derive from Dialog… the
Dialog though should in fact block the rest of the application
because the answer of it will define the next action in my
main.

      That

said, I tried to adapt my code to what you said (removing the
StepDialog in between), it didn’t work because my main
continued doing the rest of the code without waiting for the
answer in the Dialog…

      I

tried then, to just adapt considering your code with your main
still being my StepDialog, but even without the MainLoop, the
application does not close :confused:

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

Well, of course we don’t have any idea what the rest of your code looks like. Is this a wx application, or are you just popping up a dialog in an otherwise non-GUI app?

It is a non-GUI app, but as weird as it might sound, I need the question to be shown in a GUI app… And, I need the entire non-GUI scenario to wait for the answer in the Dialog…

···

On 19 July 2018 at 18:37, Tim Roberts timr@probo.com wrote:

Gabrielle C wrote:

      You

are right, my StepDialog should not derive from Dialog… the
Dialog though should in fact block the rest of the application
because the answer of it will define the next action in my
main.

      That

said, I tried to adapt my code to what you said (removing the
StepDialog in between), it didn’t work because my main
continued doing the rest of the code without waiting for the
answer in the Dialog…

If you call ShowModal, that cannot happen.  ShowModal, by

definition, blocks until the dialog calls EndModal. This is NOT
true if you call Show, which makes it non-modal.

      I

tried then, to just adapt considering your code with your main
still being my StepDialog, but even without the MainLoop, the
application does not close :confused:

-- Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.
Well, of course we don't have any idea what the rest of your code

looks like. Is this a wx application, or are you just popping up a
dialog in an otherwise non-GUI app?

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 wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

It’s perfectly legal to have a non-GUI app put up a GUI dialog. If
your app is single-threaded. then the call to ShowModal will block
until the dialog is dismissed, and you don’t need a MainLoop. If
your app is multi-threaded, then of course the other threads will
continue to run while the main thread handles the dialog.

···

Gabrielle C wrote:

          Well,

of course we don’t have any idea what the rest of your
code looks like. Is this a wx application, or are you
just popping up a dialog in an otherwise non-GUI app?

        It

is a non-GUI app, but as weird as it might sound, I need the
question to be shown in a GUI app… And, I need the entire
non-GUI scenario to wait for the answer in the Dialog…

-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com