I was thinking of Separating functions from GUI. I want when have different classes of GUIs that share functions, then I will have one class full of function/methods on need. However I failed to separate them, for I could not make instance of Functions class in the gui class in order to utilize its methods. Below is my trial with one function (Pause) shifted to another class. The original code was written by Paulthom12345, So I hope he will not worry for me to use his codes here with some fiddling lol:
It crashes: NameError: name 'Events' is not defined
import wx
import wx.media
import os
class MainFrame(wx.Frame):
self.functions = Events()
def __init__(self):
wx.Frame.__init__(self,None,title = "Media Player")
self.panel = wx.Panel(self)
self.player = wx.media.MediaCtrl(self.panel)
self.load = wx.Button(self.panel,wx.ID_ANY,"Load File")
self.load.Bind(wx.EVT_BUTTON,self.loadFile)
self.play = wx.Button(self.panel,wx.ID_ANY,"Play")
self.play.Bind(wx.EVT_LEFT_DOWN,self.playFile)
self.pause = wx.Button(self.panel,wx.ID_ANY,"Pause")
self.pause.Bind(wx.EVT_LEFT_DOWN,self.functions.pauseFile)
self.stop = wx.Button(self.panel,wx.ID_ANY,"Stop")
self.stop.Bind(wx.EVT_LEFT_DOWN,self.stopFile)
self.slider = wx.Slider(self.panel,wx.ID_ANY,size = (300,-1))
self.slider.Bind(wx.EVT_SLIDER,self.Seek)
self.info_name = wx.StaticText(self.panel,wx.ID_ANY,"Name:")
self.info_length = wx.StaticText(self.panel,wx.ID_ANY,"Time:")
self.info_pos = wx.StaticText(self.panel,wx.ID_ANY,"Pos:")
sizer = wx.GridBagSizer(5,5)
sizer.Add(self.slider,(1,1),(1,4))
sizer.Add(self.load,(5,1))
sizer.Add(self.play,(5,2))
sizer.Add(self.pause,(5,3))
sizer.Add(self.stop,(5,4))
sizer.Add(self.info_name,(2,1),(1,4))
sizer.Add(self.info_length,(3,1),(1,4))
sizer.Add(self.info_pos,(4,1),(1,4))
self.panel.SetSizer(sizer)
self.Show()
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.onTimer)
self.timer.Start(100)
self.panel.SetInitialSize()
self.SetInitialSize()
def onTimer(self,event):
current = self.player.Tell()
self.info_pos.SetLabel("Pos: %i seconds" % (int(current)/1000))
self.slider.SetValue(current)
def Seek(self,event):
self.player.Seek(self.slider.GetValue())
def loadFile(self,event):
msg = wx.FileDialog(self, message = "Open a media file",
style = wx.OPEN,
wildcard = "Media Files|*.wma;*.mp3;*.avi")
if msg.ShowModal() == wx.ID_OK:
path = msg.GetPath()
self.path = path
if not self.player.Load(path):
wx.MessageBox("Unable to load this file, it is in the wrong format")
else:
self.player.Play()
def playFile(self,event):
print self.path
self.player.Play()
print self.player.Play()
self.slider.SetRange(0, self.player.Length())
self.info_length.SetLabel('length: %d seconds' % (self.player.Length()/1000))
self.info_name.SetLabel("Name: %s" % (os.path.split(self.path)[1]))
self.panel.SetInitialSize()
self.SetInitialSize()
#def pauseFile(self,event):
# self.player.Pause()
def stopFile(self,event):
self.player.Stop()
app = wx.App(False)
frame = MainFrame()
app.MainLoop()
class Events():
def __init__(self):
def pauseFile(self,event):
self.Pause()