为什么wxPython在使用ID_OPEN,wx.OPEN时会出现问题?

时间:2014-01-27 05:56:11

标签: wxpython

我正在开发的文本编辑器程序如下。在使用ID_OPEN时,wxPython终止。在使用wx.OPEN时,这次wxPython也会终止。

import wx
import os
class MyFrame(wx.Frame):
    # init the frame 
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title = title, size=(200,100))
        self.control =  wx.TextCtrl(self, style=wx.TE_MULTILINE)
        self.CreateStatusBar()
        filemenu = wx.Menu()
        # creates menu 


        menuAbout = filemenu.Append(wx.ID_ABOUT, "&About" , "Information About This Program")
        menuExit = filemenu.Append(wx.ID_EXIT,"E&xit","Terminate The Program")
        menuOpen = filemenu.Append(wx.ID_OPEN,"&Open" , "Open a window") # menu to open a window , this gives error


        menubar =wx.MenuBar()
        menubar.Append(filemenu, "&File")
        self.SetMenuBar(menubar)
        # binds event 
        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
        self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
        self.Show()
        # Click on About Menu           
    def OnAbout(self, e):
        dlg = wx.MessageDialog(self, "A Small text editor","Simple Editor")
        dlg.ShowModal()
        dlg.Destroy()
        # Click on Exit Menu 
    def OnExit(self, e):
        self.Close(True)
        # Click on Open 
    def OnOpen(self, e):
        self.dirname = ''
        dlg =  wx.FileDialog(self, "Choose A file",self.dirname,"","*.*",wx.OPEN) # this line also gives error
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            f = open(os.path.join(self.dirname, self.filename),'r')
            self.control.SetValue(f.read())
            f.close()
        dlg.Destroy()

app = wx.App(False)
frame = MyFrame(None, 'Small Editor')
app.MainLoop()

没有给出回溯,Python解释器崩溃:

Unhandled exception at 0x016688e3 in pythonw.exe:
0xC0000005: Access violation reading location 0x00000000.

有人可以给我一个理由和解决方案吗?

1 个答案:

答案 0 :(得分:2)

使用的正确常量是wx.FD_OPEN而不是wx.OPEN,后者可能适用于较旧的wxPython版本。但即使documentation for version 2.8.9.2wx.FD_OPEN