-1

每当我运行我的 wxpython 代码时,我都会收到此错误。

    Traceback (most recent call last):
      File "musicplayer.py", line 203, in <module>
        MyPanel(frame, -1)
      File "musicplayer.py", line 17, in __init__
        self.mc = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER)
      File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/media.py", line 96, in __init__
        _media.MediaCtrl_swiginit(self,_media.new_MediaCtrl(*args, **kwargs))
    NotImplementedError

我的程序是在 wxpython 中创建一个音乐播放器。

这是代码:

  import wx

    import wx.media

    import os

    from wx.lib.wordwrap import wordwrap





    class MyPanel(wx.Panel):

    def __init__(self, parent, id):

    

    wx.Panel.__init__(self, parent, id)

    self.SetBackgroundColour("white")

    self.Center()

    self.panel = wx.Panel(self, wx.ID_ANY)

    #self.CreateStatusBar()

    

        try:

            self.mc = wx.media.MediaCtrl(self, style=wx.SIMPLE_BORDER)

        except NotImplementedError:

            self.Destroy()

            raise

        

    

        

       

        loadButton  = wx.BitmapButton(self, -1, wx.Bitmap('icons/stock_media-file.png'),pos=(10,2))

        loadButton.Bind(wx.EVT_LEFT_DOWN, self.onLoadFile, loadButton)

        

        

        

    #   self.sb = self.CreateStatusBar()

        

        

    

        

        

        

       

        playlistButton  = wx.BitmapButton(self, -1, wx.Bitmap('icons/stock_media-list.png'),pos=(80,2))

        #playlistButton.Bind(wx.EVT_ENTER_WINDOW, self.OnWidgetEnter)

        

        



        helpButton  = wx.BitmapButton(self, -1, wx.Bitmap('icons/stock_media-help.png'),pos=(150,10))

        helpButton.Bind(wx.EVT_BUTTON, self.onHelp, helpButton)

        

        #sb.SetStatusText("blabla")



        playButton  = wx.BitmapButton(self, -1, wx.Bitmap('icons/stock_media-play.png'),pos=(50,140))

        playButton.Bind(wx.EVT_LEFT_DOWN, self.onPlay, playButton)



        pauseButton  = wx.BitmapButton(self, -1, wx.Bitmap('icons/stock_media-pause.png'),pos=(130,140))

        pauseButton.Bind(wx.EVT_ENTER_WINDOW, self.onPause, pauseButton)

        

        

        

        stopButton = wx.BitmapButton(self, -1, wx.Bitmap('icons/stock_media-stop.png'),pos=(170,140))

        stopButton.Bind(wx.EVT_LEFT_DOWN, self.onStop, stopButton)



        volumeButton = wx.BitmapButton(self, -1, wx.Bitmap('icons/stock_media-volume.png'),pos=(1200,140))

        volumeButton.Bind(wx.EVT_LEFT_DOWN, self.mute, volumeButton)



        



        nextButton=wx.BitmapButton(self, -1, wx.Bitmap('icons/stock_media-next.png'),pos=(90,140))

        nextButton.Bind(wx.EVT_LEFT_DOWN, self.playForward, nextButton)

        

        

        previousButton=wx.BitmapButton(self, -1, wx.Bitmap('icons/stock_media-previous.png'),pos=(10,140))

        previousButton.Bind(wx.EVT_LEFT_DOWN, self.playBack, previousButton)

        



        

        

       





                # layout ...

        hsizer = wx.BoxSizer(wx.HORIZONTAL)

        

        # also creates a border space

        vsizer = wx.BoxSizer(wx.VERTICAL)

        vsizer.Add(hsizer, 2, wx.EXPAND|wx.ALL, border=-10)

        vsizer.Add(self.mc,6, wx.EXPAND|wx.ALL, border=30)

        self.SetSizer(vsizer)

      

        #slider

    self.slider = wx.Slider(self,-1,pos=(10,100),size = (1350,-1))

        self.slider.Bind(wx.EVT_SLIDER,self.Seek)

    self.info_pos = wx.StaticText(self,-1,pos=(1300,125),size=(450,-1))

    sizer= wx.GridBagSizer(5,5)

    sizer.Add(self.slider,(1,1),(1,4))

        self.Show()



        #volumeslider

        currentVolume = 50

        global slider1

        slider1 = wx.Slider(self,pos=(1250,150),size = (100,-1))

        slider1.SetRange(0, 100)

        slider1.SetValue(currentVolume)

        slider1.Bind(wx.EVT_SLIDER, self.setVol,slider1)

        

        #Timer

        self.timer = wx.Timer(self)

        self.Bind(wx.EVT_TIMER, self.onTimer)

        self.timer.Start(100)



        start_image = wx.Image("Logo.gif") 

    start_image.Rescale(320, 300) 

    image = wx.BitmapFromImage(start_image) 

    mypic = wx.StaticBitmap(self, -1, image, pos=(550,300), style=wx.BITMAP_TYPE_PNG)

       

        ext = ("~~~Title~~~")

        self.file = wx.StaticText(self, -1, ext,size=(355,-1),pos=(600,175))



               

    def onLoadFile(self, evt):

        dlg = wx.FileDialog(self, message="Choose a media file",

            defaultDir=os.getcwd(), defaultFile="",

            style=wx.OPEN|wx.CHANGE_DIR )

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

            path = dlg.GetPath()

            self.doLoadFile(path)

        dlg.Destroy()

        

    def doLoadFile(self, path):

        if not self.mc.Load(path):

            wx.MessageBox("Unable to load %s: Unsupported format?" % path,

                "ERROR", wx.ICON_ERROR | wx.OK)

        else:

            folder, filename = os.path.split(path)

            self.file.SetLabel('%s' % filename)

            self.GetSizer().Layout()

            self.mc.Play()

        

    def onPlay(self,event):

        self.mc.Play()

        self.slider.SetRange(0, self.mc.Length())

        self.mc.SetLabel('length: %d seconds' % (self.mc.Length()/1000))

             

    def onPause(self, evt):

        self.mc.Pause()

        self.sb.SetStatusText('Load file')

        event.Skip()

    def onStop(self, evt):

        self.mc.Stop()



    def Seek(self,event):

       self.mc.Seek(self.slider.GetValue())    



    def onTimer(self,event):

        current = self.mc.Tell()

        self.info_pos.SetLabel(" %i seconds" % (int(current)/1000))

        self.slider.SetValue(current)





    def setVol(self, event):

        currentVolume = slider1.GetValue()

        print int(currentVolume)

        self.mc.SetVolume(currentVolume)



    def mute(self, event):

        currentVolume=0

        self.mc.SetVolume(currentVolume)

       

    def playForward(self,event):

        playbackrate=16

        self.mc.SetPlaybackRate(playbackrate)


    def playBack(self,event):

        playbackrate=-4

        self.mc.SetPlaybackRate(playbackrate)

        

    def OnWidgetEnter(self,e):

        name = e.GetEventObject().GetClassName()

        sb.SetStatusText(name+'widget')

        e.skip()
    

    def onHelp(self, event):

        info = wx.AboutDialogInfo()

        info.Name = "Media 89 Help and Support"

        

        info.Description = wordwrap(

            "This is an example of a help description of how to  "

            "use this music player!"

            "1.You can load your file "

            "2.You can play music"

            "3.Can check the playlist",

            350, wx.ClientDC(self.panel))

        info.WebSite = ("http://homes.soi.rp.edu.sg/101193/home.html", "Online Help")

        info.play = ["media 89 group"]

        info.do = wordwrap("created by media 89!", 500,

                                wx.ClientDC(self.panel))

        

        wx.AboutBox(info)

    


app = wx.PySimpleApp()

style=wx.DEFAULT_FRAME_STYLE^wx.RESIZE_BORDER

frame = wx.Frame(None, -1, "Media 89",style=style,size = (1500,1800) )

MyPanel(frame, -1)

frame.Show(True)

app.MainLoop()

   
4

2 回答 2

1

wxMediaCtrl 是构建的可选部分,如果 wxWidgets 的配置脚本无法找到正确的依赖库或未安装它们的 -devel 包,它将自动被排除。当 wxPython 是使用没有 wxMediaCtrl 的 wxWigets 构建时,它会创建一个存根类,如果你尝试使用它,它只会引发 NotImplementedError。

于 2013-01-16T19:19:12.343 回答
0

什么版本的 wxPython?

在 Linux 上,使用 python2.7 和 wxPython 2.8.12.1,我进入我的 shell 并启动 python 解释器并发出一些测试命令:

python
>>> import wx.media
>>> app = wx.App()
>>> frm = wx.Frame(None)
>>> mc = wx.media.MediaCtrl(frm, style=wx.SIMPLE_BORDER)
>>>

没有错误,尽管在最后一行按回车键并返回 python 提示后花了大约 20 秒。你可以在你的命令外壳上做同样的事情,看看你是否仍然得到错误,如果是这样,试试不使用 style 参数?

于 2013-01-14T23:14:15.883 回答