#!/usr/bin/env python ## Network Programming Project 3 ## zip code lookup ## see - http://zip4.usps.com/zip4/citytown_zip.jsp ## The resulting URL from clicking submit on the above form is like: ## http://zip4.usps.com/zip4/zcl_3_results.jsp?zip5=67401 import urllib, urllib2 import wx import wx.html class ZipPanel(wx.Panel): """ class ZipPanel inherits wx.Panel and adds HtmlWindow """ def __init__(self, parent, id): # default pos is (0, 0) and size is (-1, -1) which fills the frame wx.Panel.__init__(self, parent, id) self.SetBackgroundColour("purple") self.html1 = wx.html.HtmlWindow(self, id, pos=(0,30), size=(610,370)) self.btn2 = wx.Button(self, -1, "Zip Code", pos=(0,0)) self.btn2.Bind(wx.EVT_BUTTON, self.OnNewZip) def OnNewZip(self, event): dlg = wx.TextEntryDialog(self, "Enter a Zip Code", "Zip Code for City, State Lookup", style=wx.OK | wx.CANCEL ) if dlg.ShowModal() == wx.ID_OK: self.load_zip(dlg.GetValue()) dlg.Destroy() def load_zip(self, zipcode=None): if zipcode: if len(zipcode) == 5 and zipcode.isdigit(): self.zipcode = zipcode self.html1.SetPage(getzip(self.zipcode)) else: dlg = wx.MessageDialog(self, "The Zip Code must be consist of 5 digits", "Invalid Zip Code", style = wx. OK) dlg.ShowModal() dlg.Destroy() return def getzip(zipcode='67401'): url = "http://zip4.usps.com/zip4/zcl_3_results.jsp" data = urllib.urlencode([('zip5', zipcode)]) req = urllib2.Request(url) html = urllib2.urlopen(req, data).read() #print html return html if __name__ == '__main__': app = wx.PySimpleApp() # create a window/frame, no parent, -1 is default id, title, size frame = wx.Frame(None, -1, "Zip Code Lookup", size=(610, 400)) # call the derived class, -1 is default id win = ZipPanel(frame,-1) # show the frame frame.Show(True) # start the event loop app.MainLoop()