analitics

Pages

Sunday, February 26, 2012

The wx module - tutorial 02 - class and button function

In this tutorial I will show how to use wx using one class example.

This tutorial is based on some code from a previous tutorial.

Let's see the source code:

import wx

class my_gui_form(wx.Frame):
 def __init__(self):
  wx.Frame.__init__(self, None, -1, 'Example windows wx with Python',size=(300,150))
  wx.StaticText(self, -1, 'User', (10, 20))
  user = wx.TextCtrl(self, -1, '',  (110, 20), (160, -1))
  wx.StaticText(self, -1, 'Password', (10, 50))
  passwd = wx.TextCtrl(self, -1, "", (110,50),(160, -1), style=wx.TE_PASSWORD)
  conn = wx.Button(self, -1,'Connect',(10,80),(260, -1))
  conn.Bind(wx.EVT_BUTTON, self.onButton)
  
 def onButton(self, event):
  button = event.GetEventObject()
  print "The button's label is: " + button.GetLabel()
  print "The button's name is: " + button.GetName()

if __name__ == "__main__":
 app1 = wx.App(False)
 frame = my_gui_form()
 frame.Show()
 app1.MainLoop()

First , I make a class named my_gui_form.

Also I add the self on each function from this class.

I make the onButton function to working with the button Connect.

I read the event of the button with event.GetEventObject.

The link with this function is make by:

conn.Bind(wx.EVT_BUTTON, self.onButton)

Source Code Optimization:

The -1 from is can replace with wx.ID_ANY.