21.12.2022 Views

python_para_desenvolvedores_2ed

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Interface Gráfica 231

import wx

class Main(wx.Frame):

def __init__(self, parent, id, title):

# Cria janela

wx.Frame.__init__(self, parent, id, title, size=(300, 150))

self.Centre()

self.Show(True)

# Cria um texto estático

self.text = wx.StaticText(self, label='Entre com uma expressão:',

pos=(10, 10))

# Cria uma caixa de edição de texto

self.edit = wx.TextCtrl(self, size=(250, -1), pos=(10, 30))

# Cria um botão

self.button = wx.Button(self, label='ok', pos=(10, 60))

# Conecta um método ao botão

self.button.Bind(wx.EVT_BUTTON, self.on_button)

def on_button(self, event):

# Pega o valor da caixa de texto

txt = self.edit.GetValue()

# Tenta resolver e apresentar a expressão

try:

wx.MessageBox(txt + ' = ' + str(eval(txt)), 'Resultado')

# Se algo inesperado ocorrer

except:

wx.MessageBox('Expressão inválida', 'Erro')

app = wx.App()

Main(None, -1, 'Teste de MessageBox')

app.MainLoop()

Janela principal e caixa de mensagem:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!