By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,135 Members | 1,184 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,135 IT Pros & Developers. It's quick & easy.

WxGlade + PyGame...and making the two play together

P: 5
As a fun little task to gain more knowledge of python, i decided to try to mimick the ipod UI with it. here's what i've got so far

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # generated by wxGlade 0.5 on Sat Oct 27 15:46:36 2007
  4.  
  5. import wx, os, pygame
  6. pygame.init()
  7.  
  8. class MyFrame2(wx.MDIChildFrame):
  9.     def __init__(self, *args, **kwds):
  10.         # begin wxGlade: MyFrame2.__init__
  11.         kwds["style"] = wx.DEFAULT_FRAME_STYLE
  12.         wx.MDIChildFrame.__init__(self, *args, **kwds)
  13.         self.__set_properties()
  14.         self.__do_layout()
  15.         # end wxGlade
  16.  
  17.     def __set_properties(self):
  18.         # begin wxGlade: MyFrame2.__set_properties
  19.         self.SetTitle("frame_3")
  20.         # end wxGlade
  21.  
  22.     def __do_layout(self):
  23.         # begin wxGlade: MyFrame2.__do_layout
  24.         sizer_2 = wx.BoxSizer(wx.VERTICAL)
  25.         sizer_2.Add(self.tree_2, 1, wx.EXPAND, 0)
  26.         self.SetSizer(sizer_2)
  27.         sizer_2.Fit(self)
  28.         self.Layout()
  29.         # end wxGlade
  30.  
  31.     def zooey(self, event):
  32.         import time
  33.         time.sleep(2)
  34.         pygame.mixer.music.load(event.GetItem())
  35.         pygame.mixer.music.play()
  36.  
  37.  
  38. # end of class MyFrame2
  39.  
  40.  
  41. class MyFrame(wx.Frame):
  42.     def __init__(self, *args, **kwds):
  43.         # begin wxGlade: MyFrame.__init__
  44.         kwds["style"] = wx.DEFAULT_FRAME_STYLE
  45.         wx.Frame.__init__(self, *args, **kwds)
  46.         self.tree_ctrl_1 = wx.TreeCtrl(self, -1, style=wx.TR_HAS_BUTTONS|wx.TR_NO_LINES|wx.TR_EDIT_LABELS|wx.TR_FULL_ROW_HIGHLIGHT|wx.TR_HIDE_ROOT|wx.TR_ROW_LINES|wx.TR_DEFAULT_STYLE|wx.NO_BORDER)
  47.  
  48.         self.__set_properties()
  49.         self.__do_layout()
  50.  
  51.         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.boogienights, self.tree_ctrl_1)
  52.         # end wxGlade
  53.  
  54.         root = self.tree_ctrl_1.AddRoot('Home')
  55.         ET = self.tree_ctrl_1.AppendItem(root, "Music")    
  56.  
  57.         artist_list = os.listdir('music')
  58.         for i in artist_list:
  59.             self.tree_ctrl_1.AppendItem(ET, i) 
  60.  
  61.     def __set_properties(self):
  62.         # begin wxGlade: MyFrame.__set_properties
  63.         self.SetTitle("frame_1")
  64.         self.tree_ctrl_1.SetBackgroundColour(wx.Colour(255, 255, 255))
  65.         # end wxGlade
  66.  
  67.     def __do_layout(self):
  68.         # begin wxGlade: MyFrame.__do_layout
  69.         sizer_1 = wx.BoxSizer(wx.VERTICAL)
  70.         sizer_1.Add(self.tree_ctrl_1, 1, wx.EXPAND, 0)
  71.         self.SetSizer(sizer_1)
  72.         sizer_1.Fit(self)
  73.         self.Layout()
  74.         # end wxGlade
  75.  
  76.     def boogienights(self, event): # wxGlade: MyFrame.<event_handler>
  77.         self.Hide()
  78.         frame_2 = MyFrame2(None, -1, "")
  79.         frame_2.Show()
  80. # end of class MyFrame
  81.  
  82. if __name__ == "__main__":
  83.     app = wx.PySimpleApp(0)
  84.     wx.InitAllImageHandlers()
  85.     frame_1 = MyFrame(None, -1, "")
  86.     app.SetTopWindow(frame_1)
  87.     frame_1.Show()
  88.     app.MainLoop()
  89.  
  90.  
  91.  
everything works up to when it's time to play an mp3 file. as you can see, i have a TreeCtrl that lists the items in a folder. However, as you know, when you enter a new folder with an ipod it slides over to a new screen, so i had to have a go at that.. anyway, when it's time for pygame to play a song i get a seg fault (i expected as much).
what i'm trying to have the program do is...when it opens up that next frame (I haven't actually added the ipod-folder-slide effect yet..) and lists the contents and i click a file, i want pygame to play that file. the problem is that i have to idea how to tell pygame which file the user has selected...

all help would be greatly appreciated.
Oct 28 '07 #1
Share this Question
Share on Google+
10 Replies


P: 5
solved:
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # generated by wxGlade 0.5 on Sat Oct 27 15:46:36 2007
  4.  
  5. import wx, os, pygame
  6. pygame.init()
  7.  
  8. class MyFrame2(wx.MDIChildFrame):
  9.     def __init__(self, *args, **kwds):
  10.         # begin wxGlade: MyFrame2.__init__
  11.         kwds["style"] = wx.DEFAULT_FRAME_STYLE
  12.         wx.MDIChildFrame.__init__(self, *args, **kwds)
  13.         self.__set_properties()
  14.         self.__do_layout()
  15.         # end wxGlade
  16.  
  17.     def __set_properties(self):
  18.         # begin wxGlade: MyFrame2.__set_properties
  19.         self.SetTitle("frame_3")
  20.         # end wxGlade
  21.  
  22.     def __do_layout(self):
  23.         # begin wxGlade: MyFrame2.__do_layout
  24.         sizer_2 = wx.BoxSizer(wx.VERTICAL)
  25.         sizer_2.Add(self.tree_2, 1, wx.EXPAND, 0)
  26.         self.SetSizer(sizer_2)
  27.         sizer_2.Fit(self)
  28.         self.Layout()
  29.         # end wxGlade
  30.  
  31.     #def zooey(self, self.event):
  32.      #3   import time
  33.       #  print "nigga whats up" * 10
  34.        # time.sleep(2)
  35.         #music = pygame.mixer.music.load(event.GetItem())
  36.         #pygame.mixer.music.play()
  37.  
  38.  
  39. # end of class MyFrame2
  40.  
  41.  
  42. class MyFrame(wx.Frame):
  43.     def __init__(self, *args, **kwds):
  44.         # begin wxGlade: MyFrame.__init__
  45.         kwds["style"] = wx.DEFAULT_FRAME_STYLE
  46.         wx.Frame.__init__(self, *args, **kwds)
  47.         self.tree_ctrl_1 = wx.TreeCtrl(self, -1, style=wx.TR_HAS_BUTTONS|wx.TR_NO_LINES|wx.TR_EDIT_LABELS|wx.TR_FULL_ROW_HIGHLIGHT|wx.TR_HIDE_ROOT|wx.TR_ROW_LINES|wx.TR_DEFAULT_STYLE|wx.NO_BORDER)
  48.  
  49.         self.__set_properties()
  50.         self.__do_layout()
  51.  
  52.         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.boogienights, self.tree_ctrl_1)
  53.         # end wxGlade
  54.  
  55.         root = self.tree_ctrl_1.AddRoot('Home')
  56.         ET = self.tree_ctrl_1.AppendItem(root, "Music")    
  57.  
  58.         artist_list = os.listdir('music')
  59.         os.chdir('music')
  60.         for i in artist_list:
  61.             self.tree_ctrl_1.AppendItem(ET, i) 
  62.  
  63.     def __set_properties(self):
  64.         # begin wxGlade: MyFrame.__set_properties
  65.         self.SetTitle("frame_1")
  66.         self.tree_ctrl_1.SetBackgroundColour(wx.Colour(255, 255, 255))
  67.         # end wxGlade
  68.  
  69.     def __do_layout(self):
  70.         # begin wxGlade: MyFrame.__do_layout
  71.         sizer_1 = wx.BoxSizer(wx.VERTICAL)
  72.         sizer_1.Add(self.tree_ctrl_1, 1, wx.EXPAND, 0)
  73.         self.SetSizer(sizer_1)
  74.         sizer_1.Fit(self)
  75.         self.Layout()
  76.         # end wxGlade
  77.  
  78.     def boogienights(self, event): # wxGlade: MyFrame.<event_handler>
  79.         item = event.GetItem()
  80.         item = self.tree_ctrl_1.GetItemText(item)
  81.         item = os.getcwd() + "/" + item
  82.         music = pygame.mixer.music.load(item)
  83.         pygame.mixer.music.play()# end of class MyFrame
  84.  
  85. if __name__ == "__main__":
  86.     app = wx.PySimpleApp(0)
  87.     wx.InitAllImageHandlers()
  88.     frame_1 = MyFrame(None, -1, "")
  89.     app.SetTopWindow(frame_1)
  90.     frame_1.Show()
  91.     app.MainLoop()
  92.  
Oct 28 '07 #2

bartonc
Expert 5K+
P: 6,596
Right off the bat, I'd suggest a true subclass of wx.APP:
Expand|Select|Wrap|Line Numbers
  1. class MyApp(wx.App):
  2.     def OnInit(self):
  3.         self.main = frame_1(None)
  4.         self.main.Show()
  5.         self.SetTopWindow(self.main)
  6.         return True
  7.  
  8. def main():
  9.     application = MyApp(redirect=False) # or True depending...
  10.     application.MainLoop()
I've noticed some differences in behavior between this an PySimpleApp.

Secondly, there's got to be a better module for playing MP3s than PyGame (unless you need other features of that package).
Oct 28 '07 #3

bartonc
Expert 5K+
P: 6,596
solved:
Can you tell us what you did differently? It's not totally obvious looking at the code.
Thanks.
Oct 28 '07 #4

P: 5
Right off the bat, I'd suggest a true subclass of wx.APP:
Expand|Select|Wrap|Line Numbers
  1. class MyApp(wx.App):
  2.     def OnInit(self):
  3.         self.main = frame_1(None)
  4.         self.main.Show()
  5.         self.SetTopWindow(self.main)
  6.         return True
  7.  
  8. def main():
  9.     application = MyApp(redirect=False) # or True depending...
  10.     application.MainLoop()
I've noticed some differences in behavior between this an PySimpleApp.

Secondly, there's got to be a better module for playing MP3s than PyGame (unless you need other features of that package).
hmm, i'll search around for a better module.

i'll try out what you suggested because after i got it to successfully play
[ here's what i did to get it to work:
once the directory is listed and displayed in the TreeCtrl, i use os.chdir('music'), then os.getcwd() (which would be /music) + "/" + filename; i then use pygaim to play that.

problem now is, when i try to open a frame and have pygaim play the song in that window i get a seg fault.

#edit..lol just noticed a little funnny string i used for debugging..whoops ^_^
here's where i'm at now..
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # generated by wxGlade 0.5 on Sat Oct 27 15:46:36 2007
  4.  
  5. import wx, os, pygame
  6. pygame.init()
  7.  
  8.  
  9. class MyFrame2(wx.MDIChildFrame):
  10.     def __init__(self, *args, **kwds):
  11.         # begin wxGlade: MyFrame2.__init__
  12.         kwds["style"] = wx.DEFAULT_FRAME_STYLE
  13.         wx.MDIChildFrame.__init__(self, *args, **kwds)
  14.         self.tree_2 = wx.TreeCtrl(self, -1, style=wx.TR_HAS_BUTTONS|wx.TR_NO_LINES|wx.TR_EDIT_LABELS|wx.TR_DEFAULT_STYLE|wx.NO_BORDER)
  15.  
  16.         self.__set_properties()
  17.         self.__do_layout()
  18.  
  19.         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.boogie, self.tree_2)
  20.         # end wxGlade
  21.  
  22.     def __set_properties(self):
  23.         # begin wxGlade: MyFrame2.__set_properties
  24.         self.SetTitle("frame_3")
  25.         # end wxGlade
  26.  
  27.     def __do_layout(self):
  28.         # begin wxGlade: MyFrame2.__do_layout
  29.         sizer_2 = wx.BoxSizer(wx.VERTICAL)
  30.         sizer_2.Add(self.tree_2, 1, wx.EXPAND, 0)
  31.         self.SetSizer(sizer_2)
  32.         sizer_2.Fit(self)
  33.         self.Layout()
  34.         # end wxGlade
  35.  
  36.     def boogie(self, event): # wxGlade: MyFrame2.<event_handler>
  37.         pygame.mixer.music.play()
  38. # end of class MyFrame2
  39.  
  40.  
  41. class MyFrame(wx.Frame):
  42.     def __init__(self, *args, **kwds):
  43.         # begin wxGlade: MyFrame.__init__
  44.         kwds["style"] = wx.DEFAULT_FRAME_STYLE
  45.         wx.Frame.__init__(self, *args, **kwds)
  46.         self.tree_ctrl_1 = wx.TreeCtrl(self, -1, style=wx.TR_HAS_BUTTONS|wx.TR_NO_LINES|wx.TR_EDIT_LABELS|wx.TR_FULL_ROW_HIGHLIGHT|wx.TR_HIDE_ROOT|wx.TR_ROW_LINES|wx.TR_DEFAULT_STYLE|wx.NO_BORDER)
  47.  
  48.         self.__set_properties()
  49.         self.__do_layout()
  50.  
  51.         self.Bind(wx.EVT_TREE_ITEM_ACTIVATED, self.boogienights, self.tree_ctrl_1)
  52.         # end wxGlade
  53.  
  54.         root = self.tree_ctrl_1.AddRoot('Home')
  55.         ET = self.tree_ctrl_1.AppendItem(root, "Music")    
  56.  
  57.         artist_list = os.listdir('music')
  58.         os.chdir('music')
  59.         for i in artist_list:
  60.             self.tree_ctrl_1.AppendItem(ET, i) 
  61.  
  62.     def __set_properties(self):
  63.         # begin wxGlade: MyFrame.__set_properties
  64.         self.SetTitle("frame_1")
  65.         self.tree_ctrl_1.SetBackgroundColour(wx.Colour(255, 255, 255))
  66.         # end wxGlade
  67.  
  68.     def __do_layout(self):
  69.         # begin wxGlade: MyFrame.__do_layout
  70.         sizer_1 = wx.BoxSizer(wx.VERTICAL)
  71.         sizer_1.Add(self.tree_ctrl_1, 1, wx.EXPAND, 0)
  72.         self.SetSizer(sizer_1)
  73.         sizer_1.Fit(self)
  74.         self.Layout()
  75.         # end wxGlade
  76.  
  77.     def boogienights(self, event): # wxGlade: MyFrame.<event_handler>
  78.         item = event.GetItem()
  79.         item = self.tree_ctrl_1.GetItemText(item)
  80.         item = os.getcwd() + "/" + item
  81.         music = pygame.mixer.music.load(item)
  82.         # end of class MyFrame
  83.  
  84.  
  85. class MyApp(wx.App):
  86.         def OnInit(self):
  87.               self.main = MyFrame(None)
  88.               self.main.Show()
  89.               self.SetTopWindow(self.main)
  90.               return True
  91.  
  92. def main():
  93.           application = MyApp(redirect=False) # or True depending...
  94.           application.MainLoop()
  95. main()
  96.  
Oct 28 '07 #5

bartonc
Expert 5K+
P: 6,596
hmm, i'll search around for a better module.

i'll try out what you suggested because after i got it to successfully play
[ here's what i did to get it to work:
once the directory is listed and displayed in the TreeCtrl, i use os.chdir('music'), then os.getcwd() (which would be /music) + "/" + filename; i then use pygaim to play that.

problem now is, when i try to open a frame and have pygaim play the song in that window i get a seg fault.
I deduce from
Expand|Select|Wrap|Line Numbers
  1. item = os.getcwd() + "/" + item
that you are on an "other" OS (lol). So please tell us about your OS and platform.
Thanks.
Oct 28 '07 #6

Smygis
100+
P: 126
If you are on Linux you can use gstreamer.
In Debian/Ubuntu its:
python-gst0.10 - generic media-playing framework (Python bindings)
dont know about other distros, But i guess its something similar.
But something i think is nicer is MPD. The Music Playing Daemon (Realy geeky). Sorta easy to use. But the python bindings for it totaly sucks.
Oct 28 '07 #7

P: 5
I deduce from
Expand|Select|Wrap|Line Numbers
  1. item = os.getcwd() + "/" + item
that you are on an "other" OS (lol). So please tell us about your OS and platform.
Thanks.
i don't know about being on an "other" OS ;)...Ubuntu is the only OS I have installed ^_^


I'll check into gstreamer, Smygis, thank you
Oct 28 '07 #8

bartonc
Expert 5K+
P: 6,596
i don't know about being on an "other" OS ;)...Ubuntu is the only OS I have installed ^_^
"Other" refers to OSes who's path names that don't start C:\. Knowing which one you are using is required info if we are to give relevant suggestions/solutions.
Oct 29 '07 #9

P: 5
taking all suggestions on how to pursue coding this ipod UI
ANY advice is very welcomed

i'm check out pygtk to see if i like it better than wx..
Oct 30 '07 #10

bartonc
Expert 5K+
P: 6,596
i'll check out pygtk to see if i like it better than wx..
IMO, GTK is not as well structured as wx. We have on member on these boards who is required to use GTK and wishes he xould switch to wx. I've had a look at GTK's class structure and didn't see anything that I liked.

I also prefer Boa Constructor over wxGlade for GUI generation.

Just my two cents worth.
Oct 30 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.