演示如何在pygtk的一个窗口中开两个mplayer播放器

   1 #!/usr/bin/env python
   2 
   3 import subprocess
   4 import sys
   5 import time
   6 import os
   7 
   8 import pygtk
   9 pygtk.require('2.0')
  10 import gtk
  11 
  12 
  13 class TwinWindow:
  14     def __init__(self, file1, file2):
  15         self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  16         self.window.connect("destroy", self.destroy)
  17         self.window.set_border_width(10)
  18         self.box = gtk.HBox(homogeneous=True, spacing=10)
  19         self.window.add(self.box)
  20         self.socket1 = gtk.Socket()
  21         self.socket2 = gtk.Socket()
  22         self.box.pack_start(self.socket1)
  23         self.box.pack_start(self.socket2)
  24         self.plug1 = gtk.Plug(0)
  25         self.plug2 = gtk.Plug(0)
  26         self.socket1.add_id(self.plug1.get_id())
  27         self.socket2.add_id(self.plug2.get_id())
  28         self.socket1.show()
  29         self.socket2.show()
  30         self.box.show()
  31         self.window.resize(960, 480)
  32         self.window.show()
  33         self.popen1 = subprocess.Popen(["/usr/bin/env", "mplayer","-quiet", "-wid", "%d"%self.socket1.get_id(), file1], stdout = subprocess.PIPE, stderr = subprocess.PIPE )
  34         self.popen2 = subprocess.Popen(["/usr/bin/env", "mplayer","-quiet", "-wid", "%d"%self.socket2.get_id(), file2], stdout = subprocess.PIPE, stderr = subprocess.PIPE )
  35         #self.popen2 = subprocess.Popen("mplayer -quiet -wid %d %s" % (self.socket2.get_id(), file2), stdout = subprocess.PIPE, stderr = subprocess.PIPE, shell = True )
  36         
  37     def destroy(self,widget, data=None):
  38         self.popen1.terminate()
  39         self.popen2.terminate()
  40         time.sleep(1)
  41         self.popen1.kill()
  42         self.popen2.kill()
  43         os.system('reset') # workaround for the problem that shell cannot echo
  44         gtk.main_quit()
  45 
  46     def main(self):
  47         gtk.main()
  48 
  49 if __name__ == '__main__':
  50     root = TwinWindow(sys.argv[1], sys.argv[2])
  51     root.main()

PyGtk (2009-12-30 23:39:01由czk编辑)

ch3n2k.com | Copyright (c) 2004-2020 czk.