0

我正在 wxPython 中构建一个应用程序,我在博客的某个地方读到,您不能同时在同一个应用程序上使用 wx.FD_OPEN 和 wx.FD_SAVE,这是真的吗?

如果这是真的,这是否意味着我必须搬到 Tkinter?

编辑:我目前拥有的。

      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()
4

1 回答 1

4

我不确定我是否理解问题所在。当我将此代码放入实际运行的东西中时,它对我来说很好:

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()

我在 Windows 7 上使用 wxPython 2.8.12.1 和 Python 2.6.6

于 2013-03-18T13:39:22.530 回答