0

I am building an application in wxPython and i read somewhere on a blog that you cannot use wx.FD_OPEN and wx.FD_SAVE on the same application at the same time, is this true?

If this is true, does this mean i have to move to Tkinter?

EDIT: What i currently have.

      SAVE_FILE_ID = wx.NewId()
      self.Bind(wx.EVT_MENU, self.saveFile, id=SAVE_FILE_ID)

      LOAD_FILE_ID = wx.NewId()
      self.Bind(wx.EVT_MENU, self.loadFile, id=LOAD_FILE_ID)

      accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL,  ord('O'), LOAD_FILE_ID ),
                                       (wx.ACCEL_CTRL,  ord('S'), SAVE_FILE_ID )])

      self.SetAcceleratorTable(accel_tbl)

      def saveFile(self, event):
        saveFileDialog = wx.FileDialog(self, "Save As", "", "", 
                                      "Python files (*.py)|*.py", 
                                      wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)

        self.text.SaveFile(saveFileDialog.GetPath())
        event.Skip()

      def loadFile(self, event):
        openFileDialog = wx.FileDialog(self, "Open", "", "", 
                                      "Python files (*.py)|*.py", 
                                      wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)

        self.text.LoadFile(openFileDialog.GetPath())
        event.Skip()
3
  • 1
    1) Yes, that's true. 2) Not really. why do you think that? Open and Save are different dialogs for different purposes Commented Mar 18, 2013 at 9:48
  • Well Open and Save as are in the same menu in my program, how can i make both dialogs work via my accelerator? It won't work with load but it will work with save, simply because save comes first in my code. Commented Mar 18, 2013 at 10:35
  • @vjgaero I think you misunderstood. You can have separate Open and Save dialogs no problem. This is what you're doing in your code. What you can't do is have one dialog that both opens and saves (i.e. one dialog with the style wx.FD_OPEN | wx.FD_SAVE) Commented Mar 18, 2013 at 16:08

1 Answer 1

4

I'm not sure I understand what the problem is. When I put this code into something that actually runs, it works fine for me:

import wx

class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial", size=(500,500))

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)

        SAVE_FILE_ID = wx.NewId()
        self.Bind(wx.EVT_MENU, self.saveFile, id=SAVE_FILE_ID)

        LOAD_FILE_ID = wx.NewId()
        self.Bind(wx.EVT_MENU, self.loadFile, id=LOAD_FILE_ID)

        accel_tbl = wx.AcceleratorTable([(wx.ACCEL_CTRL,  ord('O'), LOAD_FILE_ID ),
                                         (wx.ACCEL_CTRL,  ord('S'), SAVE_FILE_ID )]
                                        )
        self.SetAcceleratorTable(accel_tbl)

    #----------------------------------------------------------------------
    def loadFile(self, event):
        openFileDialog = wx.FileDialog(self, "Open", "", "", 
                                       "Python files (*.py)|*.py", 
                                       wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        openFileDialog.ShowModal()
        openFileDialog.GetPath()
        openFileDialog.Destroy()

    #----------------------------------------------------------------------
    def saveFile(self, event):
        saveFileDialog = wx.FileDialog(self, "Save As", "", "", 
                                       "Python files (*.py)|*.py", 
                                       wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT)
        saveFileDialog.ShowModal()
        saveFileDialog.GetPath()
        saveFileDialog.Destroy()

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

I'm using wxPython 2.8.12.1 with Python 2.6.6 on Windows 7

Sign up to request clarification or add additional context in comments.

1 Comment

I compared the code and i realized i wasnt .ShowModal() cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.