每当我运行我的 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()