Sunday, April 17, 2016

Creating a Desktop SMS Application with wxPython

Hi,
Lets Create a very simple and basic desktop SMS app with wxPython for python 2. Before we continue, you will need to create an account with a Python SMS gateway providers.

Am going to use an SMS gateway call twilio, check this post to learn how to setup account with twilio SMS gateway.

Now, our wxPython code for this app is as below; if you want to learn how it was done click here.

With python2.7 and wxpython installed on your machine, copy the code below and save it in .py file and run it. You will get an interface similar to the one below;-


#!/usr/bin/python
# -*- coding: utf-8 -*-

import wx
import wx.xrc

###########################################################################
## Class MyFrame1
###########################################################################

class MyFrame1 ( wx.Frame ):
 
 def __init__( self, parent ):
  wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"SMS App by - www.UmarYusuf.com", pos = wx.DefaultPosition, size = wx.Size( 450,350 ), style = wx.CAPTION|wx.CLOSE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.TAB_TRAVERSAL )
  
  self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
  
  bSizer1 = wx.BoxSizer( wx.VERTICAL )
  
  self.m_panel1 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
  self.m_panel1.SetForegroundColour( wx.Colour( 255, 255, 255 ) )
  self.m_panel1.SetBackgroundColour( wx.Colour( 242, 174, 47 ) )
  
  bSizer2 = wx.BoxSizer( wx.VERTICAL )
  
  self.TitleText = wx.StaticText( self.m_panel1, wx.ID_ANY, u"Simple Twilio SMS App", wx.DefaultPosition, wx.DefaultSize, 0 )
  self.TitleText.Wrap( -1 )
  self.TitleText.SetFont( wx.Font( 20, 70, 90, 92, False, wx.EmptyString ) )
  
  bSizer2.Add( self.TitleText, 0, wx.ALIGN_CENTER_HORIZONTAL|wx.TOP|wx.RIGHT|wx.LEFT, 5 )
  
  self.m_staticline1 = wx.StaticLine( self.m_panel1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
  bSizer2.Add( self.m_staticline1, 0, wx.EXPAND, 5 )
  
  bSizer3 = wx.BoxSizer( wx.HORIZONTAL )
  
  LabelSizer = wx.BoxSizer( wx.VERTICAL )
  
  self.TwilioNumber = wx.StaticText( self.m_panel1, wx.ID_ANY, u"Twilio Number:", wx.DefaultPosition, wx.DefaultSize, 0 )
  self.TwilioNumber.Wrap( -1 )
  self.TwilioNumber.SetFont( wx.Font( 10, 70, 90, 92, False, wx.EmptyString ) )
  
  LabelSizer.Add( self.TwilioNumber, 0, wx.ALL|wx.EXPAND, 5 )
  
  self.SendTo = wx.StaticText( self.m_panel1, wx.ID_ANY, u"Send To Number:", wx.DefaultPosition, wx.DefaultSize, 0 )
  self.SendTo.Wrap( -1 )
  self.SendTo.SetFont( wx.Font( 10, 70, 90, 92, False, wx.EmptyString ) )
  
  LabelSizer.Add( self.SendTo, 0, wx.ALL|wx.EXPAND, 5 )
  
  
  LabelSizer.AddSpacer( ( 0, 70), 0, wx.EXPAND, 5 )
  
  self.Message = wx.StaticText( self.m_panel1, wx.ID_ANY, u"Type Message:", wx.DefaultPosition, wx.DefaultSize, 0 )
  self.Message.Wrap( -1 )
  self.Message.SetFont( wx.Font( 10, 70, 90, 92, False, wx.EmptyString ) )
  
  LabelSizer.Add( self.Message, 0, wx.ALL|wx.EXPAND, 5 )
  
  
  bSizer3.Add( LabelSizer, 0, wx.EXPAND, 5 )
  
  ControlSizer = wx.BoxSizer( wx.VERTICAL )
  
  self.TwilioNumberBox = wx.TextCtrl( self.m_panel1, wx.ID_ANY, u"+19253018778", wx.DefaultPosition, wx.DefaultSize, 0 )
  ControlSizer.Add( self.TwilioNumberBox, 0, wx.EXPAND|wx.TOP|wx.RIGHT|wx.LEFT, 5 )
  
  self.SendToNumberBox = wx.TextCtrl( self.m_panel1, wx.ID_ANY, u"+2348039508010", wx.DefaultPosition, wx.DefaultSize, 0 )
  ControlSizer.Add( self.SendToNumberBox, 0, wx.EXPAND|wx.TOP|wx.RIGHT|wx.LEFT, 5 )
  
  self.MessageBox = wx.TextCtrl( self.m_panel1, wx.ID_ANY, u"Hello world!\nMy name is Umar Yusuf - www.UmarYusuf.com", wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.TE_MULTILINE )
  ControlSizer.Add( self.MessageBox, 1, wx.EXPAND|wx.TOP|wx.RIGHT|wx.LEFT, 5 )
  
  self.SendButton = wx.Button( self.m_panel1, wx.ID_ANY, u"Send Message", wx.DefaultPosition, wx.DefaultSize, 0 )
  self.SendButton.SetFont( wx.Font( 12, 70, 90, 90, False, wx.EmptyString ) )
  
  ControlSizer.Add( self.SendButton, 0, wx.ALL|wx.ALIGN_RIGHT|wx.EXPAND, 5 )
  
  
  bSizer3.Add( ControlSizer, 1, wx.EXPAND, 5 )
  
  
  bSizer2.Add( bSizer3, 1, wx.EXPAND, 5 )
  
  
  self.m_panel1.SetSizer( bSizer2 )
  self.m_panel1.Layout()
  bSizer2.Fit( self.m_panel1 )
  bSizer1.Add( self.m_panel1, 1, wx.EXPAND|wx.ALL, 5 )
  
  
  self.SetSizer( bSizer1 )
  self.Layout()
  self.m_menubar1 = wx.MenuBar( 0 )
  self.m_menu1 = wx.Menu()
  self.AboutMenu = wx.MenuItem( self.m_menu1, wx.ID_ANY, u"About", wx.EmptyString, wx.ITEM_NORMAL )
  self.m_menu1.AppendItem( self.AboutMenu )
  
  self.m_menu1.AppendSeparator()
  
  self.TwilioMenu = wx.MenuItem( self.m_menu1, wx.ID_ANY, u"Twilio", wx.EmptyString, wx.ITEM_NORMAL )
  self.m_menu1.AppendItem( self.TwilioMenu )
  
  self.m_menubar1.Append( self.m_menu1, u"Project" ) 
  
  self.SetMenuBar( self.m_menubar1 )
  
  
  self.Centre( wx.BOTH )
  
  # Connect Events
  self.SendButton.Bind( wx.EVT_BUTTON, self.sendFunc )
  self.Bind( wx.EVT_MENU, self.aboutFunc, id = self.AboutMenu.GetId() )
  self.Bind( wx.EVT_MENU, self.twilioFunc, id = self.TwilioMenu.GetId() )
 
 def __del__( self ):
  pass
 
 
 # Virtual event handlers, overide them in your derived class
 def sendFunc( self, event ):
  to = self.SendToNumberBox.GetValue()
  from_ = self.TwilioNumberBox.GetValue()
  body = self.MessageBox.GetValue()

  from twilio.rest import TwilioRestClient
  # Find these values at https://twilio.com/user/account
  account_sid = "Axx9526a3afcnjjkkksswkek35bf13xx09"
  auth_token = "06xx0f3cxxf7b18xx7c5bf4f92bxxxf"
  client = TwilioRestClient(account_sid, auth_token)
  message = client.messages.create(to=to, from_=from_, body=body)

 
 def aboutFunc( self, event ):
  about = MyDialog1(None).ShowModal()

 
 def twilioFunc( self, event ):
  import webbrowser
  webbrowser.open('http://www.UmarYusuf.com')
  import os
  os.startfile('http://www.twilio.com')

###########################################################################
## Class MyDialog1
###########################################################################

class MyDialog1 ( wx.Dialog ):
 
 def __init__( self, parent ):
  wx.Dialog.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 300,300 ), style = wx.DEFAULT_DIALOG_STYLE|wx.STAY_ON_TOP )
  
  self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )
  
  bSizer6 = wx.BoxSizer( wx.VERTICAL )
  
  self.m_panel2 = wx.Panel( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
  self.m_panel2.SetForegroundColour( wx.Colour( 255, 255, 255 ) )
  self.m_panel2.SetBackgroundColour( wx.Colour( 242, 174, 47 ) )
  
  bSizer7 = wx.BoxSizer( wx.VERTICAL )
  
  self.AboutDialogText = wx.StaticText( self.m_panel2, wx.ID_ANY, u"This is a simple desktop GUI app, setup to test run the trial twilio.com SMS account using its Python module.\n\nDeployed as part of blog post on: http://umar-yusuf.blogspot.com", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTRE )
  self.AboutDialogText.Wrap( -1 )
  self.AboutDialogText.SetFont( wx.Font( 12, 70, 90, 90, False, wx.EmptyString ) )
  
  bSizer7.Add( self.AboutDialogText, 1, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL, 5 )
  
  self.AboutDialogText1 = wx.StaticText( self.m_panel2, wx.ID_ANY, u"Created by: Umar Yusuf\nWebsite: www.UmarYusuf.com\nTel: +2348039508010\n", wx.DefaultPosition, wx.DefaultSize, wx.ALIGN_CENTRE )
  self.AboutDialogText1.Wrap( -1 )
  self.AboutDialogText1.SetFont( wx.Font( wx.NORMAL_FONT.GetPointSize(), 70, 90, 92, False, wx.EmptyString ) )
  
  bSizer7.Add( self.AboutDialogText1, 0, wx.ALL|wx.EXPAND, 5 )
  
  self.m_staticline2 = wx.StaticLine( self.m_panel2, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.LI_HORIZONTAL )
  bSizer7.Add( self.m_staticline2, 0, wx.EXPAND |wx.ALL, 5 )
  
  self.okbutton = wx.Button( self.m_panel2, wx.ID_ANY, u"OK", wx.DefaultPosition, wx.DefaultSize, 0 )
  bSizer7.Add( self.okbutton, 0, wx.ALL|wx.ALIGN_RIGHT, 5 )
  
  
  self.m_panel2.SetSizer( bSizer7 )
  self.m_panel2.Layout()
  bSizer7.Fit( self.m_panel2 )
  bSizer6.Add( self.m_panel2, 1, wx.EXPAND|wx.ALL, 5 )
  
  
  self.SetSizer( bSizer6 )
  self.Layout()
  
  self.Centre( wx.BOTH )
  
  # Connect Events
  self.okbutton.Bind( wx.EVT_BUTTON, self.okFunc )
 
 def __del__( self ):
  pass
 
 # Virtual event handlers, overide them in your derived class
 def okFunc( self, event ):
   self.Destroy()
  

if __name__ == '__main__':
  app = wx.App()
  frame = MyFrame1(None).Show()
  app.MainLoop() 

Note: If you intend to use the GUI script above, you have change the "from_", "account_sid" and "auth_token" variables to match that of your twilio account.


When the "Send Message" button is pressed, the app collects the GUI details and substitute them into the twilio SMS script defined by the "sendFunc" method/function in the script above.

You should now receive a text/SMS on the number provided.

That is it!
Thanks for reading.

No comments:

Post a Comment