Playing Animation with WxPython - python

I try to play a gif in my frame. I use this code in order to do it. Why doesn't it work?
(I use the last version of wxPython - 4.0.7.post2)
import wx
from wx.adv import AnimationCtrl
class Animate(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, -1, title)
self.animation = AnimationCtrl(self)
self.animation.LoadFile('scan.gif')
self.animation.Play()
self.Show()
app = wx.App()
frame = Animate(None, -1, 'Animation')
app.MainLoop()

The argument to the constructor wx.adv.Animation is the filename. So it has to be:
anim = wx.adv.Animation()
anim.LoadFile(r'C:\Users\yuval\PycharmProjects\MultiTyping\pictures\back_gif.gif')
or
anim = wx.adv.Animation(r'C:\Users\yuval\PycharmProjects\MultiTyping\pictures\back_gif.gif')
Furthermore, I recommend to add a wx.BoxSizer to the frame:
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(anim_ctrl)
frame.SetSizerAndFit(sizer)
See the example:
import wx
from wx.adv import AnimationCtrl, Animation
app=wx.App()
frame = wx.Frame(None, -1, title='2', pos=(0, 0), size=(200, 200))
app.SetTopWindow(frame)
anim = Animation(r'C:\Users\yuval\PycharmProjects\MultiTyping\pictures\back_gif.gif')
anim_ctrl = AnimationCtrl(frame, -1, anim)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(anim_ctrl)
frame.SetSizerAndFit(sizer)
frame.Show()
anim_ctrl.Play()
app.MainLoop()

I see nothing wrong with the answer given by #Rabbid76, I suggest that you run the code from the command line, rather than from within some ide.
Here is another take on your problem, it's as concise as I can make it and assumes a local file called scan.gif.
import wx
from wx.adv import AnimationCtrl
class Animate(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, -1, title)
self.animation = AnimationCtrl(self)
self.animation.LoadFile('scan.gif')
self.animation.Play()
self.Show()
app = wx.App()
frame = Animate(None, -1, 'Animation')
app.MainLoop()

Related

Matplotlib canvas won't fit in wxpython panel

I'm trying to show an image inside a panel of a wxpython notebook page using matplotlib. It works perfectly using wx.StaticBitmap but I need to use matplotlib for image manipulation. The code gives this result:
The right panel is a terminal and sizes correctly. The left panel is where the image is meant to fit. The result of wx.lib.inspection is showing that the wxFigureCanvas minsize is (640, 480) but I don't understand why and can't find how to change that. I suspect it's the problem. The code (sorry, short as I could make it):
import cv2 as cv
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
from matplotlib.backends.backend_wxagg import FigureCanvasWxAgg as FigureCanvas
import wx
import wx.lib.agw.aui as aui
class TopRight(wx.Panel):
def __init__(self, parent):
super(TopRight, self).__init__(parent)
self.term = wx.TextCtrl(self,
size=(200, 100),
style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH
)
self.term.SetDefaultStyle(wx.TextAttr(wx.GREEN))
self.term.SetBackgroundColour(wx.BLACK)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.term, 1, wx.EXPAND)
self.SetSizer(self.sizer)
self.term.WriteText("hello")
self.term.Update
class TopLeft(wx.Panel):
def __init__(self, parent):
super(TopLeft , self).__init__(parent)
img_pth = "av_cat.png"
# Intitialise the matplotlib figure
self.figure = Figure()
# Create an axes, turn off the labels and add them to the figure
self.axes = plt.Axes(self.figure,[0,0,1,1])
self.axes.set_axis_off()
self.figure.add_axes(self.axes)
# Add the figure to the wxFigureCanvas
self.canvas = FigureCanvas(self, -1, self.figure)
# Sizer to contain the canvas
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.canvas, 1, wx.EXPAND|wx.ALL)
self.SetSizer(self.sizer)
# If there is an initial image, display it on the figure
if img_pth is not None:
self.setImage(img_pth)
def setImage(self, pathToImage):
# Load the image into matplotlib
image = cv.imread(pathToImage)
# Add the image to the figure and redraw the canvas.
self.axes.imshow(image)
self.canvas.draw()
class explorer_panel(wx.Panel):
def __init__(self, parent):
#Constructor
wx.Panel.__init__(self, parent)
topSplitter = wx.SplitterWindow(self)
image_panel = TopLeft(topSplitter)
terminal_panel = TopRight(topSplitter)
topSplitter.SplitVertically(image_panel, terminal_panel)
topSplitter.SetSashGravity(0.5)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(topSplitter, 1, wx.EXPAND)
self.SetSizer(sizer)
class Main(wx.Frame):
def __init__(self):
wx.Frame.__init__(
self,
parent = None,
title = "Borges Infinite Image",
size = (600,300)
)
panel = wx.Panel(self)
notebook = wx.Notebook(panel)
explorer = explorer_panel(notebook)
notebook.AddPage(explorer, 'Explorer')
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
panel.SetSizer(sizer)
if __name__ == "__main__":
app = wx.App()
frame = Main()
frame.Show()
import wx.lib.inspection as wxli
wxli.InspectionTool().Show()
app.MainLoop()

wxpython set sizer background

i am using wxpython and trying to make an background to a sizer without any success, i searched in google without any results.
i try it with this boxsizer
wx.BoxSizer(wx.HORIZONTAL)
I just use panels for this sort of thing. You can set the color of the panel several different ways: you can use a named color, a wx.Color object, a predefined wx.Color object like wx.RED or a tuple of 3 integers.
Here's a simple example:
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.SetBackgroundColour('white')
main_sizer = wx.BoxSizer(wx.VERTICAL)
for number in range(5):
btn = wx.Button(self, label='Button {}'.format(number))
main_sizer.Add(btn, 0, wx.ALL, 5)
self.SetSizer(main_sizer)
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title='Background colors')
panel = MyPanel(self)
self.Show()
if __name__ == '__main__':
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
I wrote a little about this topic here:
https://www.blog.pythonlibrary.org/2009/09/03/wxpython-resetting-the-background-color/
You might also the wxPython wiki helpful:
https://wiki.wxpython.org/GettingStarted

Changing the image size dynamically when the sizer's size changes in wxPython

I am working with python v2.7 and wxPython v3.0 on Windows 8 OS.
In the code snippet provided below simply creates a frame which has a vertical sizer that contains an image and an other horizontal sizer that in turn contains two panels.
Question: When I resize the frame by dragging the border, the panels resize automatically. Unlike the panels the image doesn't resize? How can I make my image to behave like panels for the case of resizing?
Code: The image file used can be downloaded from here: green.bmp
import wx
class Frame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, style=wx.DEFAULT_FRAME_STYLE)
panelA = wx.Panel(self,-1)
imageFile = wx.Image('greenbig.bmp', wx.BITMAP_TYPE_ANY).ConvertToBitmap()
myBitmap = wx.StaticBitmap(panelA, -1, imageFile)
panel1 = wx.Panel(self, -1,style=wx.SIMPLE_BORDER)
panel2 = wx.Panel(self, -1, style=wx.SIMPLE_BORDER)
panelSizer = wx.BoxSizer(wx.HORIZONTAL)
panelSizer.Add(panel1, 1, wx.ALL|wx.EXPAND, 0)
panelSizer.Add(panel2, 1, wx.ALL|wx.EXPAND, 0)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(panelA, 1, wx.ALL|wx.EXPAND, 0)
sizer.Add(panelSizer, 1, wx.ALL|wx.EXPAND, 0)
self.SetSizer(sizer)
self.Show(True)
app = wx.App()
frame = Frame(None, wx.ID_ANY, 'Image')
app.MainLoop()
Thank you for your time!
(eight years later) I was struggling to show an image that was dynamically resized with the window frame. These days, I'm using Python 3 with wxPython version 4. I pieced this solution from a lot of reading of the documentation and tips on how best to use sizers. I then whittled my code down to the absolute minimum required to accomplish this narrow use case.
Notice that I don't ever call Frame.Layout() or Panel.Layout() because I rely on the initial size of the Frame and the fact that a single Panel in a Frame will be automatically resized with the Frame. Also, any call to Fit() was unnecessary. The sizer containing the ImagePanel does need the EXPAND flag, but the ImagePanel does not need a sizer itself.
Note: you'll have to supply your own example image to load.
import wx
class ImagePanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)
self.img = wx.Image(width=1, height=1)
self.ctrl = wx.StaticBitmap(self, bitmap=self.img.ConvertToBitmap())
self.Bind(wx.EVT_SIZE, self.on_resize)
def load_image(self):
with open('image.png', 'rb') as fin:
self.img = wx.Image(fin)
self.update_bitmap()
def update_bitmap(self):
w, h = self.GetSize()
self.ctrl.SetBitmap(self.img.Scale(w, h).ConvertToBitmap())
def on_resize(self, evt):
self.update_bitmap()
class MainWindow(wx.Frame):
def __init__(self):
initial_size = wx.Size(width=480, height=480)
super().__init__(None, wx.ID_ANY, title='Main Window Title', size=initial_size)
self.panel = wx.Panel(self)
self.image = ImagePanel(self.panel)
vbox = wx.BoxSizer(wx.VERTICAL)
vbox.Add(self.image, proportion=1, flag=wx.EXPAND)
self.panel.SetSizer(vbox)
if __name__ == '__main__':
app = wx.App()
win = MainWindow()
win.Show()
win.image.load_image()
app.MainLoop()
The image will never be resized automatically. If you want this behaviour, you need to handle wxEVT_SIZE and do it yourself in its handler.

pop up a window with text in it and automatically close it after certain time

This is the code that i have written. It does close the window but doesnt display the text in it. I need the text displayed and then automatic close of the window.
What changes should i make for it to work
Thanks
Here is the code
import wx
from time import sleep
class Frame(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, title=title, size=(300,200))
self.panel = wx.Panel(self)
box = wx.BoxSizer(wx.VERTICAL)
m_text = wx.StaticText(self.panel, -1, 'File Uploaded!')
m_text.SetSize(m_text.GetBestSize())
box.Add(m_text, 0, wx.ALL, 10)
self.panel.SetSizer(box)
self.panel.Layout()
self.Bind(wx.EVT_ACTIVATE, self.onClose)
def onClose(self, event):
sleep(5)
self.Destroy()
app = wx.App(redirect=True)
top = Frame('test')
top.Show()
app.MainLoop()
I would recommend using a wx.Timer. If you use time.sleep(), you will block wxPython's main loop which makes your application unresponsive. Here is your code modified to use the timer:
import wx
class Frame(wx.Frame):
def __init__(self, title):
wx.Frame.__init__(self, None, title=title, size=(300,200))
self.panel = wx.Panel(self)
box = wx.BoxSizer(wx.VERTICAL)
m_text = wx.StaticText(self.panel, -1, 'File Uploaded!')
m_text.SetSize(m_text.GetBestSize())
box.Add(m_text, 0, wx.ALL, 10)
self.panel.SetSizer(box)
self.panel.Layout()
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.onClose, self.timer)
self.timer.Start(5000)
def onClose(self, event):
self.Close()
app = wx.App(redirect=True)
top = Frame('test')
top.Show()
app.MainLoop()
You can read more about timers in this article:
http://www.blog.pythonlibrary.org/2009/08/25/wxpython-using-wx-timers/
>>> import wx
>>> import time
>>> app = wx.App()
>>> b = wx.BusyInfo('Upload Finished!')
>>> time.sleep(5)
>>> del b
>>>

Animating gif in wxPython

I'm trying to animate a gif using wx-Python (2.7). The code listed below works but i want to create a function that animates the gif for me, so i can use it elsewhere. I've tried searching the internet but i can only find code that animates the gif within the __init__ function. Any ideas?
# -*- coding: cp1252 -*-
import wx
import wx.animate
class MyPanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
self.SetBackgroundColour("black")
gif_fname = "skYmk.gif"
gif = wx.animate.GIFAnimationCtrl(self, id, gif_fname, pos=(10, 10))
gif.GetPlayer().UseBackgroundColour(True)
gif.Play()
app = wx.PySimpleApp()
frame = wx.Frame(None, -1, "wx.animate.GIFAnimationCtrl()", size = (200, 220))
MyPanel(frame, -1)
frame.Show(True)
app.MainLoop()
I don't understand your issue.... what is the problem in doing something like this?
import wx
import wx.animate
class MyPanel(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
self.SetBackgroundColour("black")
gif_fname = "skYmk.gif"
gif = wx.animate.GIFAnimationCtrl(self, id, gif_fname, pos=(10, 10))
gif.GetPlayer().UseBackgroundColour(True)
self.gif = gif
def CallMeLater(self, play=True):
if play:
self.gif.Play()
else:
self.gif.Stop()
app = wx.PySimpleApp()
frame = wx.Frame(None, -1, "wx.animate.GIFAnimationCtrl()", size = (200, 220))
MyPanel(frame, -1)
frame.Show(True)
app.MainLoop()

Categories

Resources