ExternalInterface , between python and flash

來源:互聯網
上載者:User

rar:  http://www.sandy1219.com/python/python-swf.rar

 

connectSwf.py

import wx;<br />import os;<br />from wx.lib.flashwin import FlashWindow;<br />#from xml.dom import minidom;<br />class ConnectSwf(wx.Frame):<br /> def __init__(self):<br /> wx.Frame.__init__(self,None,-1,title="swf",size=(400,300),style=wx.DEFAULT_FRAME_STYLE^(wx.RESIZE_BORDER|wx.MAXIMIZE_BOX));<br /> #print os.getcwd();<br /> self.swfPanel = SWFgui(self);<br /> self.setBtn = wx.Button(self,wx.NewId(),"set");<br /> self.Bind(wx.EVT_BUTTON, self.setFlash, self.setBtn);<br /> self.getBtn = wx.Button(self,wx.NewId(),"get");<br /> self.Bind(wx.EVT_BUTTON, self.getFlash, self.getBtn);<br /> self.playBtn = wx.Button(self,wx.NewId(),"play");<br /> self.Bind(wx.EVT_BUTTON,self.startPlay,self.playBtn)<br /> self.stopBtn = wx.Button(self,wx.NewId(),"stop");<br /> self.Bind(wx.EVT_BUTTON,self.endPlay,self.stopBtn)<br /> mainSizer = wx.BoxSizer(wx.VERTICAL);<br /> mainSizer.Add(wx.StaticLine(self),0,wx.EXPAND);<br /> btnSizer1 = wx.BoxSizer(wx.HORIZONTAL);<br /> btnSizer1.Add((20,20),1);<br /> btnSizer1.Add(self.setBtn);<br /> btnSizer1.Add((20,20),1);<br /> btnSizer1.Add(self.getBtn);<br /> btnSizer1.Add((20,20),1);<br /> btnSizer2 = wx.BoxSizer(wx.HORIZONTAL);<br /> btnSizer2.Add((20,20),1);<br /> btnSizer2.Add(self.playBtn);<br /> btnSizer2.Add((20,20),1);<br /> btnSizer2.Add(self.stopBtn);<br /> btnSizer2.Add((20,20),1);<br /> mainSizer.Add(btnSizer1,0,wx.EXPAND|wx.ALL,10);<br /> mainSizer.Add(btnSizer2,0,wx.EXPAND|wx.ALL,10);<br /> mainSizer.Add(self.swfPanel,0,wx.EXPAND|wx.ALL,10);<br /> self.SetSizer(mainSizer);<br /> mainSizer.Fit(self);<br /> mainSizer.SetSizeHints(self);<br /> def startPlay(self,event):<br /> self.swfPanel.flash.ctrl.CallFunction(self.formatXML("playFlash" ,"" ))<br /> def endPlay(self,event):<br /> self.swfPanel.flash.ctrl.CallFunction(self.formatXML("stopFlash" , ""))<br /> def setFlash(self,event):<br /> self.swfPanel.flash.ctrl.CallFunction(self.formatXML("ExternalReceiver", str( range(5) )) )<br /> def getFlash(self,event):<br /> self.swfPanel.flash.ctrl.CallFunction(self.formatXML("getFlash" , ""))<br /> def OnProgress(self,event):<br /> print event;<br /> def FlashCall(self, xmlStr):<br /> print "FlashCall"<br /> dlg = wx.MessageBox(xmlStr,"from flash");<br /> #def parserXML(self,xmlStr):<br /> # xmldoc = minidom.parseString(xmlStr);<br /> # print xmldoc.toXML();<br /> #return xmlStr;<br /> def formatXML(self,flashFunctionName='',sendStr=''):<br /> xmlStr = "<invoke name=/"" + flashFunctionName + "/" returntype=/"xml/">"<br /> xmlStr += "<arguments>"<br /> xmlStr += "<string>" + sendStr + "</string>"<br /> xmlStr += "</arguments>"<br /> xmlStr += "</invoke>"<br /> return xmlStr;<br />class SWFgui(wx.Panel):<br /> def __init__(self,parent):<br /> wx.Panel.__init__(self,parent,-1,pos=(0,0),size=(400,300),style=wx.FRAME_SHAPED);<br /> self.flash = FlashWindow(self,style=wx.SUNKEN_BORDER,size=(400,300));<br /> wx.BeginBusyCursor();<br /> self.flash.AddEventSink(parent);<br /> self.flash.LoadMovie(0, os.path.join(os.getcwd(),"pythonswf.swf"));<br /> self.flash.SetSize((400,300))<br /> wx.EndBusyCursor();<br /> print self.flash.FlashVersion();<br /> def getText(self):<br /> return self.flash.GetVariable("var1");<br /> def setText(self,text):<br /> self.flash.SetVariable("var2", text);</p><p>if __name__ == "__main__":<br /> app = wx.PySimpleApp();<br /> frame = ConnectSwf();<br /> frame.Center();<br /> frame.Show();<br /> app.MainLoop();<br />

 

flash:

function externHandler(externalStr:String){<br />txt.text = "from python: " + externalStr;<br />}<br />function stopFlashHandle(s){<br />this.stop();<br />}<br />function playFlashHandle(s){<br />this.play()<br />}<br />if (ExternalInterface.available) {<br />ExternalInterface.addCallback("playFlash",playFlashHandle);<br />ExternalInterface.addCallback("stopFlash",stopFlashHandle);<br />ExternalInterface.addCallback("ExternalReceiver",externHandler);<br />ExternalInterface.addCallback("getFlash",getFlash);<br />}</p><p>b.buttonMode = true;<br />b.addEventListener(MouseEvent.CLICK , clickHandle);<br />function getFlash(s){<br />clickHandle();<br />}<br />function clickHandle(e:MouseEvent=null){<br />ExternalInterface.call("externCall", txt.text);<br />}<br />

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.