wxpython to display gif images one by one - python

the code below is supposed to play some gif image when click a button..and play another gif image when clicking another button..
but when i click the first button it is playing the related image properly..
while by clicking the second button both the first image and the second one are played one by one on infinity loop
...so how to play one gif by button click?
import wx, wx.animate
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY)
panel = wx.Panel(self, wx.ID_ANY)
btn1 = wx.Button(self, -1, "play GIF 1",(50,10))
btn1.Bind(wx.EVT_BUTTON, self.onButton1)
btn2 = wx.Button(self, -1, "play GIF 2",(50,40))
btn2.Bind(wx.EVT_BUTTON, self.onButton2)
#----------------------------------------------------------------------
def onButton1(self, event):
image='animated_1.gif'
self.animateGIF(image)
#----------------------------------------------------------------------
def onButton2(self, event):
image='animated_2.gif'
self.animateGIF(image)
#----------------------------------------------------------------------
def animateGIF(self,image):
gif = wx.animate.GIFAnimationCtrl(self, -1, image,pos=(50,70),size=(10,10))
gif.GetPlayer()
gif.Play()
#----------------------------------------------------------------------
app = wx.App()
frame = MyForm().Show()
app.MainLoop()

You need to stop and destroy the previous gif image before starting a new one. Like this:
import wx, wx.animate
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY)
panel = wx.Panel(self, wx.ID_ANY)
btn1 = wx.Button(self, -1, "play GIF 1",(50,10))
btn1.Bind(wx.EVT_BUTTON, self.onButton1)
btn2 = wx.Button(self, -1, "play GIF 2",(50,40))
btn2.Bind(wx.EVT_BUTTON, self.onButton2)
self.gif = None
#----------------------------------------------------------------------
def onButton1(self, event):
image='animated_1.gif'
self.animateGIF(image)
#----------------------------------------------------------------------
def onButton2(self, event):
image='animated_2.gif'
self.animateGIF(image)
#----------------------------------------------------------------------
def animateGIF(self,image):
if self.gif:
self.gif.Stop()
self.gif.Destroy()
self.gif = wx.animate.GIFAnimationCtrl(self, -1, image,pos=(50,70),size=(10,10))
self.gif.GetPlayer()
self.gif.Play()
#----------------------------------------------------------------------
app = wx.App()
frame = MyForm().Show()
app.MainLoop()
I added self.gif = None to the __init__ function and little changed the function animateGIF.

Related

wxPython append and pop message box inside panel serving like a notification center

I would like to make a wxpython program that has a notification center just like the one on windows or mac. Whenever I have a message, the message will show inside the the notification panel, and the user could close that message afterwards.
I have a sample code for illustration as follows:
import wx
import wx.lib.scrolledpanel as scrolled
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
topPanel = wx.Panel(self)
panel1 = wx.Panel(topPanel, -1)
button1 = wx.Button(panel1, -1, label="generate message")
self.panel2 = scrolled.ScrolledPanel(
topPanel, -1, style=wx.SIMPLE_BORDER)
self.panel2.SetAutoLayout(1)
self.panel2.SetupScrolling()
button1.Bind(wx.EVT_BUTTON, self.onAdd)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(panel1,-1,wx.EXPAND|wx.ALL,border=10)
sizer.Add(self.panel2,-1,wx.EXPAND|wx.ALL,border=10)
self.sizer2 = wx.BoxSizer(wx.VERTICAL)
topPanel.SetSizer(sizer)
self.panel2.SetSizer(self.sizer2)
def onAdd(self, event):
new_text = wx.TextCtrl(self.panel2, value="New Message")
self.sizer2.Add(new_text,0,wx.EXPAND|wx.ALL,border=1)
self.panel2.Layout()
self.panel2.SetupScrolling()
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'frame')
frame.Show(True)
return True
app = MyApp(0)
app.MainLoop()
In the above I code, the right panel (i.e. panel2) serves as a notification center that all the messages should shown inside it. On the left panel (i.e. panel1) I have a button to generate message just to mimic the notification behavior. Ideally the message on the right panel should be a message box that you could close (maybe a frame? Or a MessageDialog?)
Any hint or advice is much appreciated, and an example would be the best!
Thanks!
Finally figured out myself, it was easier than I initially thought.
Here is the code:
import wx
import wx.lib.scrolledpanel as scrolled
class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title)
self.number_of_panels = 0
topPanel = wx.Panel(self)
panel1 = wx.Panel(topPanel, -1)
button1 = wx.Button(panel1, -1, label="generate message")
self.panel2 = scrolled.ScrolledPanel(
topPanel, -1, style=wx.SIMPLE_BORDER)
self.panel2.SetAutoLayout(1)
self.panel2.SetupScrolling()
button1.Bind(wx.EVT_BUTTON, self.onAdd)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(panel1,0,wx.EXPAND|wx.ALL,border=5)
sizer.Add(self.panel2,1,wx.EXPAND|wx.ALL,border=5)
self.sizer2 = wx.BoxSizer(wx.VERTICAL)
topPanel.SetSizer(sizer)
self.panel2.SetSizer(self.sizer2)
def onAdd(self, event):
self.number_of_panels += 1
panel_label = "Panel %s" % self.number_of_panels
panel_name = "panel%s" % self.number_of_panels
new_panel = wx.Panel(self.panel2, name=panel_name, style=wx.SIMPLE_BORDER)
self.closeButton = wx.Button(new_panel, label='Close %s' % self.number_of_panels)
self.closeButton.panel_number = self.number_of_panels
self.closeButton.Bind(wx.EVT_BUTTON, self.OnClose)
self.sizer2.Add(new_panel,0,wx.EXPAND|wx.ALL,border=1)
self.panel2.Layout()
self.panel2.SetupScrolling()
def OnClose(self, e):
if self.panel2.GetChildren():
e.GetEventObject().GetParent().Destroy()
self.number_of_panels -= 1
self.panel2.Layout() # Reset layout after destroy the panel
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame(None, -1, 'frame')
frame.Show(True)
return True
app = MyApp(0)
app.MainLoop()
Basically I can destroy the newly created panel. I just need to know which panel it is when I click the close button. This should work very similar to the Notification Center.

wxpython: Hide one of splitterWindow panels

I have a frame containing wx.SplitterWindow with two panels as its children. I want to hide one of the panel with a button(show/hide button) click i.e, First panel should hide and the second panel should occupy the whole frame with the sash gone. Clicking the button again should show the hidden panel, and the sash back in place. Is this possible?
I have searched the documentation, and there seems to be no specific method to do this? How can this be achieved.
import wx
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, title="test", size=(800,600))
self.splitter = wx.SplitterWindow(self, wx.ID_ANY)
self.panelOne = wx.Panel(self.splitter,1)
self.panelTwo = wx.Panel(self.splitter,1)
self.panelOne.SetBackgroundColour('sky blue')
self.panelTwo.SetBackgroundColour('pink')
self.splitter.SplitHorizontally(self.panelOne, self.panelTwo)
self.splitter.SetMinimumPaneSize(20)
self.buttonpanel = wx.Panel(self, 1)
self.buttonpanel.SetBackgroundColour('white')
self.mybutton = wx.Button(self.buttonpanel,label = "Hide")
self.Bind(wx.EVT_BUTTON, self.show_hide, self.mybutton)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.splitter, 2, wx.EXPAND)
self.sizer.Add(self.buttonpanel, 0, wx.EXPAND)
self.SetSizer(self.sizer)
def show_hide(self, event):
label = self.mybutton.GetLabel()
if label == "Hide":
### How to Hide panelOne ??
self.mybutton.SetLabel("Show")
if label == "Show":
### How to Show panelOne ??
self.mybutton.SetLabel("Hide")
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
After reading the documentation for a few seconds, I noticed the Unsplit method. You can use that to take out panelOne. Then when you want to Show it again, you just split the SplitterWindow again:
import wx
class MainFrame(wx.Frame):
""""""
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, title="test", size=(800,600))
self.splitter = wx.SplitterWindow(self, wx.ID_ANY)
self.panelOne = wx.Panel(self.splitter,1)
self.panelTwo = wx.Panel(self.splitter,1)
self.panelOne.SetBackgroundColour('sky blue')
self.panelTwo.SetBackgroundColour('pink')
self.splitter.SplitHorizontally(self.panelOne, self.panelTwo)
self.splitter.SetMinimumPaneSize(20)
self.buttonpanel = wx.Panel(self, 1)
self.buttonpanel.SetBackgroundColour('white')
self.mybutton = wx.Button(self.buttonpanel,label = "Hide")
self.Bind(wx.EVT_BUTTON, self.show_hide, self.mybutton)
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.sizer.Add(self.splitter, 2, wx.EXPAND)
self.sizer.Add(self.buttonpanel, 0, wx.EXPAND)
self.SetSizer(self.sizer)
def show_hide(self, event):
label = self.mybutton.GetLabel()
if label == "Hide":
### How to Hide panelOne ??
self.mybutton.SetLabel("Show")
self.splitter.Unsplit(self.panelOne)
if label == "Show":
### How to Show panelOne ??
self.splitter.SplitHorizontally(self.panelOne, self.panelTwo)
self.mybutton.SetLabel("Hide")
if __name__ == "__main__":
app = wx.App(False)
frame = MainFrame()
frame.Show()
app.MainLoop()
Note: You had left off the call to MainLoop at the end of the code. This made your example un-runnable.

How to delete an animated gif from a frame with wxPython?

I can't for the life of me figure out how to destroy or hide a gif from a wxPython frame.
Here is the example code:
import wx
import wx.animate
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "GIF frame")
panel = wx.Panel(self, wx.ID_ANY)
btn1 = wx.Button(self, -1, "Start GIF",(50,10))
btn1.Bind(wx.EVT_BUTTON, self.onButton1)
btn2 = wx.Button(self, -1, "Stop GIF",(50,40))
btn2.Bind(wx.EVT_BUTTON, self.onButton2)
#----------------------------------------------------------------------
def onButton1(self, event):
self.animateGIF()
#----------------------------------------------------------------------
def onButton2(self, event):
self.animateGIF(False)
#----------------------------------------------------------------------
def animateGIF(self, state=True):
gif_fname = "test.gif"
gif = wx.animate.GIFAnimationCtrl(self, -1, gif_fname,pos=(50,70),size=(10,10))
gif.GetPlayer()
if state:
gif.Play()
else:
gif.Stop()
#gif.Destroy(), gif.Hide() have no effect besides cancelling the Stop() function.
#----------------------------------------------------------------------
# Run the program
app = wx.App()
frame = MyForm().Show()
app.MainLoop()
So, how do I go about deleting this gif from my frame ?
Thank you! I hope the code is clear enough.
I believe you are loading a new GIF every time you call animateGIF. I suggest the following, though I am sure this can be improved:
import wx
import wx.animate
########################################################################
class MyForm(wx.Frame):
#----------------------------------------------------------------------
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "GIF frame")
# panel not used in this example
#panel = wx.Panel(self, wx.ID_ANY)
self.btn1 = wx.Button(self, -1, "Start GIF",(50,10))
self.btn1.Bind(wx.EVT_BUTTON, self.onButton1)
self.btn2 = wx.Button(self, -1, "Stop GIF",(50,40))
self.btn2.Bind(wx.EVT_BUTTON, self.onButton2)
self.gif = None
#----------------------------------------------------------------------
def onButton1(self, event):
self.animateGIF()
#----------------------------------------------------------------------
def onButton2(self, event):
self.animateGIF(False)
#----------------------------------------------------------------------
def animateGIF(self, state=True):
if self.gif == None:
gif_fname = "test.gif"
self.gif = wx.animate.GIFAnimationCtrl(self, -1, gif_fname,pos=(50,70),size=(10,10))
# call to GetPlayer was unnecessary
#gif.GetPlayer()
if state:
self.gif.Play()
else:
self.gif.Stop()
self.gif.Destroy()
self.gif = None
#----------------------------------------------------------------------
# Run the program
app = wx.PySimpleApp()
frame = MyForm().Show()
app.MainLoop()

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
>>>

Is it possible to dock wx.auiManager panes onto tops/bottoms of another panes?

with this code:
import wx
import wx.aui
class MyFrame(wx.Frame):
def __init__(self, parent, id=-1, title='wx.aui Test',
pos=wx.DefaultPosition, size=(800, 600),
style=wx.DEFAULT_FRAME_STYLE):
wx.Frame.__init__(self, parent, id, title, pos, size, style)
self._mgr = wx.aui.AuiManager(self)
# create several text controls
text1 = wx.TextCtrl(self, -1, 'Pane 1 - sample text',
wx.DefaultPosition, wx.Size(200,150),
wx.NO_BORDER | wx.TE_MULTILINE)
text2 = wx.TextCtrl(self, -1, 'Pane 2 - sample text',
wx.DefaultPosition, wx.Size(200,150),
wx.NO_BORDER | wx.TE_MULTILINE)
info = wx.aui.AuiPaneInfo()
info.CaptionVisible(True)
info.BottomDockable(False)
info.LeftDockable(False)
info.RightDockable(False)
info.PaneBorder(False)
info.Top()
info.Row(1)
info2 = wx.aui.AuiPaneInfo()
info2.CaptionVisible(True)
info2.BottomDockable(False)
info2.LeftDockable(False)
info2.RightDockable(False)
info2.Top()
info2.Row(2)
self._mgr.AddPane(text1, info, 'Pane Number One')
self._mgr.AddPane(text2, info2, 'Pane Number Two')
self._mgr.Update()
self.Bind(wx.EVT_CLOSE, self.OnClose)
def OnClose(self, event):
self._mgr.UnInit()
self.Destroy()
app = wx.App()
frame = MyFrame(None)
frame.Show()
app.MainLoop()
the two panes that I create are docked in the Top.
The info.Row(1) and info2.Row(2) put the two panes one after another:
_TOP_
Pane1
Pane2
Now, if I move on Pane2, this docks in the Top and this situation occurs:
_TOP_
Pane1|Pane2
I want:
1. to avoid this situation (only one pane per row!)
2. if I move, dock the pane in the bottom/top of another pane
Is this possible?
Maybe the AuiNotebook wxPython sample works for you?
import wx
import wx.aui
########################################################################
class TabPanel(wx.Panel):
"""
This will be the first notebook tab
"""
#----------------------------------------------------------------------
def __init__(self, parent):
""""""
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
sizer = wx.BoxSizer(wx.VERTICAL)
txtOne = wx.TextCtrl(self, wx.ID_ANY, "")
txtTwo = wx.TextCtrl(self, wx.ID_ANY, "")
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(txtOne, 0, wx.ALL, 5)
sizer.Add(txtTwo, 0, wx.ALL, 5)
self.SetSizer(sizer)
class DemoPanel(wx.Panel):
"""
This will be the first notebook tab
"""
#----------------------------------------------------------------------
def __init__(self, parent):
""""""
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
# create the AuiNotebook instance
nb = wx.aui.AuiNotebook(self)
# add some pages to the notebook
pages = [(TabPanel(nb), "Tab 1"),
(TabPanel(nb), "Tab 2"),
(TabPanel(nb), "Tab 3")]
for page, label in pages:
nb.AddPage(page, label)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(nb, 1, wx.EXPAND)
self.SetSizer(sizer)
########################################################################
class DemoFrame(wx.Frame):
"""
Frame that holds all other widgets
"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY,
"AUI-Notebook Tutorial",
size=(600,400))
panel = DemoPanel(self)
self.Show()
#----------------------------------------------------------------------
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = DemoFrame()
app.MainLoop()

Categories

Resources