Commits

Michele Orrù committed c0f9225

layout revisited

  • Participants
  • Parent commits b81de7c

Comments (0)

Files changed (1)

player-example.py

 __date__ = "00-00-00"
 
 
-class Frame(wx.Frame):
+class Player(wx.Frame):
     """
     The main window has to deal with events like foobar.
     """
     # path used to load images
     ICONSPATH = os.path.join('icons', '')
 
-    def __init__(self, *args, **kwds):
-        wx.Frame.__init__(self, *args, **kwds)
-
-        # set title
-        self.SetTitle("wxVLCPlayer")
+    def __init__(self, title):
+        wx.Frame.__init__(self, None, -1, title, 
+                          pos=wx.DefaultPosition, size=(550, 500))
 
         # Menu Bar
         #   File Menu
         self.SetMenuBar(self.frame_menubar)
 
         # Panels
-        panel = wx.Panel(self, -1)
-        #  The first panel hosts the video
-        pnl1 = wx.Panel(self, -1)
-        pnl1.SetBackgroundColour(wx.BLACK)
-        #  The second panel hosts some control buttons
-        pnl2 = wx.Panel(self, -1)
-        #   slider for current file status and volume
-        self.pslider = wx.Slider(pnl2, -1, 0, 0, 1000)
-        self.vslider = wx.Slider(pnl2, -1, 0, 0, 100, size=(120, -1))
-        #   control buttons: play, pause, and toggle volume
-        bplay = wx.BitmapButton(pnl2, -1,
-                                wx.Bitmap(self.ICONSPATH+'pause.png'))
-        bvolume = wx.BitmapButton(pnl2, -1,
-                                  wx.Bitmap(self.ICONSPATH+'volume.png'))
-        #   bind controls to events
-        self.Bind(wx.EVT_BUTTON, self.OnPlay, bplay)
-        self.Bind(wx.EVT_BUTTON, self.OnToggleVolume, bvolume)
-        self.Bind(wx.EVT_SLIDER, self.OnSeek, self.pslider)
-        self.Bind(wx.EVT_SLIDER, self.OnSetVolume, self.vslider)
+        # The first panel holds the video and it's all black 
+        videopanel = wx.Panel(self, -1)
+        videopanel.SetBackgroundColour(wx.BLACK)
 
-        #   give to everything a pretty layout
-        sizer2 = wx.BoxSizer(wx.HORIZONTAL)
-        sizer2.Add(self.pslider, 1, flag=wx.EXPAND)
-        sizer2.Add(bplay)
-        sizer2.Add((-1, -1), 1)
-        sizer2.Add(bvolume)
-        sizer2.Add(self.vslider, flag=wx.TOP | wx.LEFT, border=5)
-        pnl2.SetSizer(sizer2)
-
-        # Sizer
+        # The second panel holds controls
+        ctrlpanel = wx.Panel(self, -1 )
+        timeslider = wx.Slider(ctrlpanel, -1, 0, 0, 1000)
+        pause = wx.BitmapButton(ctrlpanel, -1, wx.Bitmap(self.ICONSPATH+"pause.png"))
+        play  = wx.BitmapButton(ctrlpanel, -1, wx.Bitmap(self.ICONSPATH+"play.png"))
+        volume = wx.BitmapButton(ctrlpanel, -1, wx.Bitmap(self.ICONSPATH+'volume.png'))
+        volslider = wx.Slider(ctrlpanel, -1, 0, 0, 100, size=(120, -1))
+        # bind controls to events
+        self.Bind(wx.EVT_BUTTON, self.OnPlay, play)
+        self.Bind(wx.EVT_BUTTON, self.OnToggleVolume, volume)
+        self.Bind(wx.EVT_SLIDER, self.OnSeek, timeslider)
+        self.Bind(wx.EVT_SLIDER, self.OnSetVolume, volslider)
+        # Give a pretty layout to the controls
+        ctrlbox = wx.BoxSizer(wx.VERTICAL)
+        box1 = wx.BoxSizer(wx.HORIZONTAL)
+        box2 = wx.BoxSizer(wx.HORIZONTAL)
+        # box1 contains the timeslider
+        box1.Add(timeslider, 1)
+        # box2 contains some buttons and the volume controls
+        box2.Add(pause)
+        box2.Add(play, flag=wx.RIGHT, border=5)
+        box2.Add((-1, -1), 1)
+        box2.Add(volume)
+        box2.Add(volslider, flag=wx.TOP | wx.LEFT, border=5)
+        # Merge box1 and box2 to the ctrlsizer
+        ctrlbox.Add(box1, flag=wx.EXPAND | wx.BOTTOM, border=10)
+        ctrlbox.Add(box2, 1, wx.EXPAND)
+        ctrlpanel.SetSizer(ctrlbox)
+        # Put everything togheter
         sizer = wx.BoxSizer(wx.VERTICAL)
-        sizer.Add(pnl1, flag=wx.EXPAND)
-        sizer.Add(pnl2, flag=wx.BOTTOM, border=10)
-
+        sizer.Add(videopanel, 1, flag=wx.EXPAND)
+        sizer.Add(ctrlpanel, flag=wx.EXPAND | wx.BOTTOM | wx.TOP, border=10)
         self.SetSizer(sizer)
-        # sizer.Fit(self)
-        self.Centre()
-
-        # VLC player
+        self.SetMinSize((350, 300))
+        
+        # VLC player controls
         self.player = None
         self.play = False
         self.mute = True
         if dlg.ShowModal() == wx.ID_OK:
             dirname = dlg.GetDirectory()
             filename = dlg.GetFilename()
+            # Create the player!
+            self.player = vlc.MediaPlayer(os.path.join(dirname, filename))
+            # Report the title of the file chosen
+            title = self.player.get_title()
+            #  if an error was encountred while retriving the title, then use
+            #  filename
+            if title == -1:
+                title = filename
+            self.SetTitle("%s - wxVLCplayer" % title)
+
+            self.OnPlay(None)
         dlg.Destroy()
 
-        # Create the player!
-        self.player = vlc.MediaPlayer(os.path.join(dirname, filename))
-
-        # Report the title of the file chosen
-        title = self.player.get_title()
-        #  if an error was encountred while retriving the title, then use
-        #  filename
-        if title == -1:
-            title = filename
-        self.SetTitle("%s - wxVLCplayer" % title)
-
-        self.OnPlay(None)
-
     def OnPlay(self, evt):
         """
         Toggle the status to Play/Pause. If ant file is loaded, open the dialog
         if not self.player:
             self.OnOpen(None)
         else:
-            if not self.play:
                 self.player.play()
-            else:
-                self.player.pause()
-            # finally toggle controls
-            self.play = not self.play
 
         # set the wiindow system where to render VLC's video output
         self.player.set_xwindow(self.GetHandle())
 
+    def OnPause(self, evt):
+        """
+        Pause the player.
+        """
+        if self.player:
+            self.player.pause()
+
     def OnSeek(self, evt):
         """
         Seek for a specified time in player slider.
         """
         Display a simple error dialog.
         """
-        edialog = wx.MessageDialog(self, errormessage, 'Error', wx.OK |
+        edialog = wx.MessageDialog(self, errormessage, 'Error', wx.OK|
                                                                 wx.ICON_ERROR)
         edialog.ShowModal()
 
     # Create a wx.App(), which handles the windowing system event loop
     app = wx.PySimpleApp()
     # Create the window containing our small media player
-    frame = Frame(None, -1, "", pos=wx.DefaultPosition, size=(550, 500))
-
-    # Run the application and show the player window
-    frame.Show()
+    player = Player("Simple Player")
+    # show the player window centred and run the application
+    player.Centre()
+    player.Show()
     app.MainLoop()