views:

426

answers:

2

I want to create modal dialog but which shouldn't behave in a modal way i.e. control flow should continue

if i do

 dlg = wx.Dialog(parent)
 dlg.ShowModal()

 print "xxx"

 dlg.Destroy()

"xxx" will not get printed, but in case of progress dialog

dlg = wx.ProgressDialog.__init__(self,title, title, parent=parent, style=wx.PD_APP_MODAL)
print "xxx"

dlg.Destroy()

"xxx" will get printed

so basically i want to achieve wx.PD__APP__MODAL for a normal dialog?

+1  A: 

Just use Show instead of ShowModal.

If your function (the print "xxx" part) runs for a long time you will either have to manually call wx.SafeYield every so often or move your work to a separate thread and send custom events to your dialog from it.

One more tip. As I understand, you want to execute some code after the modal dialog is shown, here is a little trick for a special bind to EVT_INIT_DIALOG that accomplishes just that.

import wx

class TestFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None)
        btn = wx.Button(self, label="Show Dialog")
        btn.Bind(wx.EVT_BUTTON, self.ShowDialog)

    def ShowDialog(self, event):
        dlg = wx.Dialog(self)
        dlg.Bind(wx.EVT_INIT_DIALOG, lambda e: wx.CallAfter(self.OnModal, e))
        dlg.ShowModal()
        dlg.Destroy()

    def OnModal(self, event):
        wx.MessageBox("Executed after ShowModal")

app = wx.PySimpleApp()
app.TopWindow = TestFrame()
app.TopWindow.Show()
app.MainLoop()
Toni Ruža
but with show it will not be modal, modal it the sense the use can't interact with rest of the app
Anurag Uniyal
It seems to me that you are trying to force some logic that is tied to this dialog outside of it. Is there a good reason for this? Why can't the dialog do the print "xxx" when it's shown?
Toni Ruža
because I need to execute a third part lib UI which itself takes a long to initialize and can only be initialized in main threadso i want to show a progress dialog while it initializes, i can use progressDialog but it is averse to customization
Anurag Uniyal
When you give up control to this long running third party lib init in the main thread you will have trouble refreshing your UI (event based programing thing... functions should not run for a long time in the event loop). It's not impossible to solve but ask yourself is it worth the bother. You could just use a static "loading..." dialog using something like the example I gave and be done with it.
Toni Ruža
yes, you are right but I want a progress animation, good thing is that third party things calls my function for some calculations, so I can Yield at those points, this is almost working with wx.ProgressDialog
Anurag Uniyal
+1 for EVT_INIT_DIALOG, may be i can mix it up with Yields to get effect
Anurag Uniyal
A: 

It was very trivial, just using wx.PD_APP_MODAL style in wx.Dialog allows it to be modal without stopping the program flow, only user input to app is blocked, i thought PD_APP_MODAL is only for progress dialog

Anurag Uniyal