Package teamwork :: Package widgets :: Module player
[hide private]
[frames] | no frames]

Source Code for Module teamwork.widgets.player

 1  from Tkinter import * 
 2  import Pmw 
 3  from images import getImage 
 4   
5 -class PlayerControl(Pmw.ButtonBox):
6 """A ButtonBox subclass for a colleciton of media player buttons""" 7
8 - def __init__(self, parent=None, **kw):
9 """Accepts all the standard ButtonBox options""" 10 optiondefs = () 11 self.defineoptions(kw,optiondefs) 12 Pmw.ButtonBox.__init__(self,parent=parent) 13 self.initialiseoptions(PlayerControl) 14 self.images = {} 15 self.state = {}
16
17 - def add(self,name,**kw):
18 """Adds a button of the given name and corresponding image""" 19 button = Pmw.ButtonBox.add(self,name,**kw) 20 self.addImage(name,name) 21 self.state[name] = 0 22 self.drawImage(name) 23 return button
24
25 - def delete(self,index):
26 """Deletes the corresponding button (name-type index preferred""" 27 Pmw.ButtonBox.delete(self,index) 28 try: 29 del self.images[index] 30 del self.state[index] 31 except KeyError: 32 # Hmm, maybe there's a better way 33 pass
34
35 - def getImage(self,name):
36 """Utility method to get the image widget of the named button""" 37 try: 38 image = PhotoImage(file=getImage('media-%s.gif' % (name))) 39 except: 40 image = None 41 return image
42
43 - def addImage(self,name,image):
44 """Adds the named image to the named button's toggle sequence""" 45 pic = self.getImage(image) 46 if pic: 47 try: 48 self.images[name].append(pic) 49 except KeyError: 50 self.images[name] = [pic]
51
52 - def drawImage(self,name):
53 """Utility image that sets named button's label to be its 54 currently relevant image""" 55 button = self.component(name) 56 try: 57 button.configure(image=self.images[name][self.state[name]]) 58 except KeyError: 59 button.configure(text=name)
60
61 - def toggle(self,name):
62 """Advances the named button in its image sequence""" 63 self.state[name] = (self.state[name]+1) % len(self.images[name]) 64 self.drawImage(name)
65 66 if __name__ == '__main__': 67 root = Tk() 68 Pmw.initialise(root) 69 control = PlayerControl(root,orient=HORIZONTAL) 70 control.pack(side=TOP,fill=X,expand=1) 71 control.add('prev') 72 button = control.add('play') 73 button.configure(command=lambda c=control:c.toggle('play')) 74 control.addImage('play','pause') 75 control.add('next') 76 # Test adding a button with a missing image 77 control.add('hello') 78 try: 79 root.mainloop() 80 except KeyboardInterrupt: 81 root.destroy() 82