16.01.2014 Views

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

520 CHAPTER 28 ■ PROJECT 9: FILE SHARING II—NOW WITH GUI!<br />

def OnInit(self):<br />

"""<br />

Sets up the GUI. Creates a window, a text field, and a but<strong>to</strong>n, and<br />

lays them out. Binds the submit but<strong>to</strong>n <strong>to</strong> self.fetchHandler.<br />

"""<br />

win = wx.Frame(None, title="File Sharing Client", size=(400, 45))<br />

bkg = wx.Panel(win)<br />

self.input = input = wx.TextCtrl(bkg);<br />

submit = wx.But<strong>to</strong>n(bkg, label="Fetch", size=(80, 25))<br />

submit.Bind(wx.EVT_BUTTON, self.fetchHandler)<br />

hbox = wx.BoxSizer()<br />

hbox.Add(input, proportion=1, flag=wx.ALL | wx.EXPAND, border=10)<br />

hbox.Add(submit, flag=wx.TOP | wx.BOTTOM | wx.RIGHT, border=10)<br />

vbox = wx.BoxSizer(wx.VERTICAL)<br />

vbox.Add(hbox, proportion=0, flag=wx.EXPAND)<br />

bkg.SetSizer(vbox)<br />

win.Show()<br />

return True<br />

def fetchHandler(self, event):<br />

"""<br />

Called when the user clicks the 'Fetch' but<strong>to</strong>n. Reads the<br />

query from the text field, and calls the fetch method of the<br />

server Node. If the query is not handled, an error message is<br />

printed.<br />

"""<br />

query = self.input.GetValue()<br />

try:<br />

self.server.fetch(query, self.secret)<br />

except Fault, f:<br />

if f.faultCode != UNHANDLED: raise<br />

print "Couldn't find the file", query

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

Saved successfully!

Ooh no, something went wrong!