Painel autodestrutivo em wxPython

 

Se você não tem nada a ver com programação, nem tem interesse no assunto, simplesmente ignore este artigo.


Mike Driscoll tem um blog dedicado a Python chamado The Mouse vs. the Python e eventualmente há excelentes artigos por lá.

Esta é uma tradução livre do artigo wxPython: Making a panel self-destruct e modifiquei sutilmente algumas coisas, incluindo os exemplos.


Outro dia vi uma pergunta no StackOverflow sobre como destruir e criar dinamicamente painéis depois de passado um certo tempo. Eu sugeri ao questionador que usasse os exemplos dos artigos do meu blog, onde eu destruia e criava botões, mas ele não entendeu. Desta forma, escrevi um exemplo simples onde o painel mostra um contador regressivo e então se destrói e é substituído por um outro painel.

Aqui está o código para o seu deleite:

# coding: utf-8
import wx
 
########################################################################
class PanelOne(wx.Panel):
    """"""
 
    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
 
        self.countdown = wx.StaticText(self, label="Esta mensagem se autodestruirá em 10 segundos")
 
 
########################################################################
class PanelTwo(wx.Panel):
    """"""
 
    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
 
        txt = wx.StaticText(self, label="Pow!")
 
 
########################################################################
class MainFrame(wx.Frame):
    """"""
 
    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Painel do Inspetor Bugiganga")
        self.panelOne = PanelOne(self)
        self.time2die = 10
 
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.update, self.timer)
        self.timer.Start(1000)
 
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.panelOne, 1, wx.EXPAND)
        self.SetSizer(self.sizer)
 
    #----------------------------------------------------------------------
    def update(self, event):
        """"""
        if self.time2die < 0:
            self.panelOne.Destroy()
            self.panelTwo = PanelTwo(self)
            self.sizer.Add(self.panelTwo, 1, wx.EXPAND)
            self.Layout()
            self.timer.Stop()
        else:
            msg = "Esta mensagem se autodestruirá em %s segundos" % self.time2die
            self.panelOne.countdown.SetLabel(msg)
        self.time2die -= 1
 
if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    frame.Show()
    app.MainLoop()

Quando você executar este código, você verá algo parecido com isso:

Tela em contagem

A contagem durará 10 segundos, então você verá isso:

Tela após a contagem

Se você quiser aprender mais sobre contadores, eu escrevi um artigo que trata do assunto. Divirta-se!

Special: 
Avalie: 
Average: 3.5 (2 votes)

Comentar


Warning: PHP Startup: Unable to load dynamic library '/opt/php56/lib/php/extensions/no-debug-non-zts-20131226/pdo.so' - /opt/php56/lib/php/extensions/no-debug-non-zts-20131226/pdo.so: cannot open shared object file: No such file or directory in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library '/opt/php56/lib/php/extensions/no-debug-non-zts-20131226/pdo_mysql.so' - /opt/php56/lib/php/extensions/no-debug-non-zts-20131226/pdo_mysql.so: cannot open shared object file: No such file or directory in Unknown on line 0

Warning: PHP Startup: Unable to load dynamic library '/opt/php56/lib/php/extensions/no-debug-non-zts-20131226/php_pdo_odbc.dll' - /opt/php56/lib/php/extensions/no-debug-non-zts-20131226/php_pdo_odbc.dll: cannot open shared object file: No such file or directory in Unknown on line 0