Play Flash Files with wxPython

bumsfeld 0 Tallied Votes 418 Views Share

The FlashWindow component of wxPython will play those nice animated flash files found on the internet. They have file extension .swf usually.

# Explore the wxPython FlashWindow component
# to play animated flash files (extension .swf)
# uses the wx.lib.pdfwin.FlashWindow class ActiveX control
# from wxPython's new wx.activex module, this allows one
# to use an ActiveX control, as if it would be a wx.Window
# embeds the ShockWave Flash control
# as far as HB knows this works only with Windows
# tested with Python25 and wxPython27 by HB

import  wx

if wx.Platform == '__WXMSW__':
    from wx.lib.flashwin import FlashWindow


class MyPanel(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, -1)
        self.pdf = None

        sizer = wx.BoxSizer(wx.VERTICAL)
        btnSizer = wx.BoxSizer(wx.HORIZONTAL)

        self.flash = FlashWindow(self, style=wx.SUNKEN_BORDER)
        sizer.Add(self.flash, proportion=1, flag=wx.EXPAND)

        btn = wx.Button(self, wx.NewId(), "Open a Flash File")
        self.Bind(wx.EVT_BUTTON, self.OnOpenFileButton, btn)
        btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)

        btn = wx.Button(self, wx.NewId(), "Open a Flash URL")
        self.Bind(wx.EVT_BUTTON, self.OnOpenURLButton, btn)
        btnSizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL, border=5)

        btnSizer.Add((50,-1), proportion=2, flag=wx.EXPAND)
        sizer.Add(btnSizer, proportion=0, flag=wx.EXPAND)

        self.SetSizer(sizer)
        self.SetAutoLayout(True)

    def OnOpenFileButton(self, event):
        # make sure you have flash files available on drive
        dlg = wx.FileDialog(self, wildcard="*.swf")
        if dlg.ShowModal() == wx.ID_OK:
            wx.BeginBusyCursor()
            self.flash.LoadMovie(0, 'file://' + dlg.GetPath())
            wx.EndBusyCursor()
        dlg.Destroy()

    def OnOpenURLButton(self, event):
        # you can retrieve flash files from internet too
        dlg = wx.TextEntryDialog(self, "Enter a URL of a .swf file", "Enter URL")
        if dlg.ShowModal() == wx.ID_OK:
            wx.BeginBusyCursor()
            # setting the movie property works too
            self.flash.movie = dlg.GetValue()
            wx.EndBusyCursor()
        dlg.Destroy()


app = wx.PySimpleApp()
# create window/frame, no parent, -1 is default ID, title, size
# change size as needed
frame = wx.Frame(None, -1, "FlashWindow", size = (500, 400))
# make instance of class, -1 is default ID
MyPanel(frame, -1)
# show frame
frame.Show(True)
# start event loop
app.MainLoop()
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Also works with earlier versions like Python24 and wxPython26.

sneekula 969 Nearly a Posting Maven

It works well on my Windows XP computer! What does .swf stand for?

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

My semi-educated guess is that swf stands for ShockWaveFlash.

akennedy93612 0 Newbie Poster

I am trying to use a flash video player in a python script and the above code does play the video. Although, there are no video player controls. Can video controls be added to the wxPython code to handle the normal video player control functions. Can screen prints be taken of a pause or stopped video.

Any and all help or direction is greatly appreciated.

Thanks,

Doug

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.