Search code examples
wxpythonfiledialog

What is the proper way to handle multiple conditionals in a wxpython file dialog?


My wxpython GUI has a method for opening a FileDialog:

def open_filedlg(self,event):
    dlg = wx.FileDialog(self, "Choose XYZ file", getcwd(), "",
             "XYZ files (*.dat)|*.dat|(*.xyz)|*.xyz", wx.OPEN)
    if dlg.ShowModal() == wx.ID_OK:
         self.xyz_source=str(dlg.GetPath())
         self.fname_txt_ctl.SetValue(self.xyz_source)
         dlg.Destroy()
         return
    if dlg.ShowModal() == wx.ID_CANCEL:
         dlg.Destroy()
         return

If I want to cancel, I have to hit the "Cancel" button twice. If I reverse the order of the conditionals, Cancel works OK, but then I have to hit the "Open" button twice to get a file name. Using "elif" instead of the second "if" doesn't change the behavior. What is the correct way to do this? Thanks.


Solution

  • The problem is, that you are opening the dialog twice (once with each 'dlg.ShowModal').

    Try something like

    dialogStatus = dlg.ShowModal()
    if dialogStatus == wx.ID_OK:
        ...