Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 28 ■ ■ ■ Project 9: File Sharing II— Now with GUI! This is a relatively short project because much of the functionality you need has already been written—in Chapter 27. In this chapter, you see how easy it can be to add a graphical user interface (GUI) to an existing Python program. What’s the Problem? In this project, you expand the file sharing system developed in Project 8, presented in Chapter 27, with a GUI client. This will make the program much easier to use, which means that more people might choose to use it—and if there is to be any point in a file sharing program, there should be more than one user. A secondary goal of this project is to show that a program that has a sufficiently modular design can be quite easy to extend (one of the arguments for using object-oriented programming). Specific Goals The GUI client should satisfy the following requirements: • It should allow you to enter a file name and submit it to the server’s fetch method. • It should list the files currently available in the server’s file directory. That’s it. Because you already have much of the system working, the GUI part is a relatively simple extension. Useful Tools In addition to the tools used in Chapter 27, you will need the wxPython toolkit. For more information about (and installation instructions for) wxPython, see Chapter 12. The code in this chapter was developed using wxPython version 2.6. If you want to use another GUI toolkit, feel free to do so. The implementation in this chapter will give you the general idea of how you can build your own, with your favorite tools. (Chapter 12 describes several GUI toolkits.) 517
518 CHAPTER 28 ■ PROJECT 9: FILE SHARING II—NOW WITH GUI! Preparations Before you begin this project, you should have Project 8 (from Chapter 27) in place, and a usable GUI toolkit installed, as mentioned in the previous section. Beyond that, no significant preparations are necessary for this project. First Implementation If you want to take a peek at the full source code for the first implementation, it can be found in Listing 28-1 later in this section. Much of the functionality is quite similar to that of the project in the preceding chapter. The client presents an interface (the fetch method) through which the user may access the functionality of the server. Let’s review the GUI-specific parts of the code. The client in Chapter 27 was a subclass of cmd.Cmd; the Client described in this chapter subclasses wx.App. While you’re not required to subclass wx.App (you could create a completely separate Client class), it can be a natural way of organizing your code. The GUI-related setup is placed in a separate method, called OnInit, which is called automatically after the App object has been created. It performs the following steps: 1. It creates a window with the title “File Sharing Client.” 2. It creates a text field and assigns it to the attribute self.input (and, for convenience, to the local variable input). It also creates a button with the text “Fetch.” It sets the size of the button and binds an event handler to it. Both the text field and the button have the panel bkg as their parent. 3. It adds the text field and button to the window, laying them out using box sizers. Feel free to use another layout mechanism if you want to. 4. It shows the window, and returns True, to indicate that OnInit was successful. The event handler is quite similar to the handler do_fetch from Chapter 27. It retrieves the query from self.input (the text field). It then calls self.server.fetch inside a try/except statement. Note that the event handler receives an event object as its only argument. Except for the relatively simple code explained previously, the GUI client works just like the text-based client in Chapter 27. You can run it in the same manner, too. This implementation only performs part of the job it’s supposed to, though. It should also list the files available in the server’s file directory. To do that, the server (Node) itself must be extended. The source code for the first implementation is shown in Listing 28-1.
- Page 498 and 499: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 500 and 501: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 502 and 503: CHAPTER 24 ■ PROJECT 5: A VIRTUAL
- Page 504 and 505: CHAPTER 25 ■ ■ ■ Project 6: R
- Page 506 and 507: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 508 and 509: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 510 and 511: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 512: CHAPTER 25 ■ PROJECT 6: REMOTE ED
- Page 515 and 516: 484 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 517 and 518: 486 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 519 and 520: 488 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 521 and 522: 490 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 523 and 524: 492 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 525 and 526: 494 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 527 and 528: 496 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 529 and 530: 498 CHAPTER 26 ■ PROJECT 7: YOUR
- Page 531 and 532: 500 CHAPTER 27 ■ PROJECT 8: FILE
- Page 533 and 534: 502 CHAPTER 27 ■ PROJECT 8: FILE
- Page 535 and 536: 504 CHAPTER 27 ■ PROJECT 8: FILE
- Page 537 and 538: 506 CHAPTER 27 ■ PROJECT 8: FILE
- Page 539 and 540: 508 CHAPTER 27 ■ PROJECT 8: FILE
- Page 541 and 542: 510 CHAPTER 27 ■ PROJECT 8: FILE
- Page 543 and 544: 512 CHAPTER 27 ■ PROJECT 8: FILE
- Page 545 and 546: 514 CHAPTER 27 ■ PROJECT 8: FILE
- Page 547: 516 CHAPTER 27 ■ PROJECT 8: FILE
- Page 551 and 552: 520 CHAPTER 28 ■ PROJECT 9: FILE
- Page 553 and 554: 522 CHAPTER 28 ■ PROJECT 9: FILE
- Page 555 and 556: 524 CHAPTER 28 ■ PROJECT 9: FILE
- Page 558 and 559: CHAPTER 29 ■ ■ ■ Project 10:
- Page 560 and 561: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 562 and 563: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 564 and 565: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 566 and 567: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 568 and 569: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 570 and 571: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 572 and 573: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 574 and 575: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 576 and 577: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 578 and 579: APPENDIX A ■ ■ ■ The Short Ve
- Page 580 and 581: APPENDIX A ■ THE SHORT VERSION 54
- Page 582 and 583: APPENDIX A ■ THE SHORT VERSION 55
- Page 584 and 585: APPENDIX A ■ THE SHORT VERSION 55
- Page 586: APPENDIX A ■ THE SHORT VERSION 55
- Page 589 and 590: 558 APPENDIX B ■ PYTHON REFERENCE
- Page 591 and 592: 560 APPENDIX B ■ PYTHON REFERENCE
- Page 593 and 594: 562 APPENDIX B ■ PYTHON REFERENCE
- Page 595 and 596: 564 APPENDIX B ■ PYTHON REFERENCE
- Page 597 and 598: 566 APPENDIX B ■ PYTHON REFERENCE
CHAPTER 28<br />
■ ■ ■<br />
Project 9: File Sharing II—<br />
Now with GUI!<br />
This is a relatively short project because much of the functionality you need has already been<br />
written—in Chapter 27. In this chapter, you see how easy it can be <strong>to</strong> add a graphical user interface<br />
(GUI) <strong>to</strong> an existing <strong>Python</strong> program.<br />
What’s the Problem?<br />
In this project, you expand the file sharing system developed in Project 8, presented in Chapter 27,<br />
with a GUI client. This will make the program much easier <strong>to</strong> use, which means that more<br />
people might choose <strong>to</strong> use it—and if there is <strong>to</strong> be any point in a file sharing program, there<br />
should be more than one user. A secondary goal of this project is <strong>to</strong> show that a program that<br />
has a sufficiently modular design can be quite easy <strong>to</strong> extend (one of the arguments for using<br />
object-oriented programming).<br />
Specific Goals<br />
The GUI client should satisfy the following requirements:<br />
• It should allow you <strong>to</strong> enter a file name and submit it <strong>to</strong> the server’s fetch method.<br />
• It should list the files currently available in the server’s file direc<strong>to</strong>ry.<br />
That’s it. Because you already have much of the system working, the GUI part is a relatively<br />
simple extension.<br />
Useful Tools<br />
In addition <strong>to</strong> the <strong>to</strong>ols used in Chapter 27, you will need the wx<strong>Python</strong> <strong>to</strong>olkit. For more information<br />
about (and installation instructions for) wx<strong>Python</strong>, see Chapter 12. The code in this<br />
chapter was developed using wx<strong>Python</strong> version 2.6.<br />
If you want <strong>to</strong> use another GUI <strong>to</strong>olkit, feel free <strong>to</strong> do so. The implementation in this chapter<br />
will give you the general idea of how you can build your own, with your favorite <strong>to</strong>ols. (Chapter 12<br />
describes several GUI <strong>to</strong>olkits.)<br />
517