Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 12 ■ GRAPHICAL USER INTERFACES 283 btn.size = win.size = 200, 100 win.show() Note that one additional event handler has been added here (closeHandler) because the Close button doesn’t have any useful default behavior in Java Swing. Also note that you don’t have to explicitly enter the main event loop because it’s running in parallel with the program (in a separate thread). Using Something Else The basics of most GUI toolkits are the same; unfortunately, however, when learning how to use a new package, it takes time to find your way through all the details that enable you to do exactly what you want. So you should take your time before deciding which package you want to work with (the section “A Plethora of Platforms” earlier in this chapter should give you some idea of where to start), and then immerse yourself in its documentation and start writing code. I hope this chapter has provided the basic concepts you need to make sense of that documentation. A Quick Summary Once again, let’s review what we’ve covered in this chapter: Graphical user interfaces. Graphical user interfaces are useful in making your programs more user-friendly. Not all programs need them, but whenever your program interacts with a user, a GUI is probably helpful. GUI platforms for Python. Many GUI platforms are available to the Python programmer. Although this richness is definitely a boon, the choice can sometimes be difficult. wxPython. wxPython is a mature and feature-rich cross-platform GUI toolkit for Python. Layout. You can position components quite simply by specifying their geometry directly. However, to make them behave properly when their containing window is resized, you will have to use some sort of layout manager. One common layout mechanism in wxPython is sizers. Event handling. Actions performed by the user trigger events in the GUI toolkit. To be of any use, your program will probably be set up to react to some of these events; otherwise the user won’t be able to interact with it. In wxPython, event handlers are added to components with the Bind method. What Now? That’s it. You now know how to write programs that can interact with the outside world through files and GUIs. In the next chapter you learn about another important component of many program systems: Databases.
- Page 264 and 265: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 266 and 267: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 268 and 269: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 270 and 271: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 272 and 273: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 274 and 275: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 276 and 277: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 278 and 279: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 280 and 281: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 282 and 283: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 284: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 287 and 288: 256 CHAPTER 11 ■ FILES AND STUFF
- Page 289 and 290: 258 CHAPTER 11 ■ FILES AND STUFF
- Page 291 and 292: 260 CHAPTER 11 ■ FILES AND STUFF
- Page 293 and 294: 262 CHAPTER 11 ■ FILES AND STUFF
- Page 295 and 296: 264 CHAPTER 11 ■ FILES AND STUFF
- Page 297 and 298: 266 CHAPTER 11 ■ FILES AND STUFF
- Page 299 and 300: 268 CHAPTER 11 ■ FILES AND STUFF
- Page 301 and 302: 270 CHAPTER 12 ■ GRAPHICAL USER I
- Page 303 and 304: 272 CHAPTER 12 ■ GRAPHICAL USER I
- Page 305 and 306: 274 CHAPTER 12 ■ GRAPHICAL USER I
- Page 307 and 308: 276 CHAPTER 12 ■ GRAPHICAL USER I
- Page 309 and 310: 278 CHAPTER 12 ■ GRAPHICAL USER I
- Page 311 and 312: 280 CHAPTER 12 ■ GRAPHICAL USER I
- Page 313: 282 CHAPTER 12 ■ GRAPHICAL USER I
- Page 317 and 318: 286 CHAPTER 13 ■ DATABASE SUPPORT
- Page 319 and 320: 288 CHAPTER 13 ■ DATABASE SUPPORT
- Page 321 and 322: 290 CHAPTER 13 ■ DATABASE SUPPORT
- Page 323 and 324: 292 CHAPTER 13 ■ DATABASE SUPPORT
- Page 325 and 326: 294 CHAPTER 13 ■ DATABASE SUPPORT
- Page 328 and 329: CHAPTER 14 ■ ■ ■ Network Prog
- Page 330 and 331: CHAPTER 14 ■ NETWORK PROGRAMMING
- Page 332 and 333: CHAPTER 14 ■ NETWORK PROGRAMMING
- Page 334 and 335: CHAPTER 14 ■ NETWORK PROGRAMMING
- Page 336 and 337: CHAPTER 14 ■ NETWORK PROGRAMMING
- Page 338 and 339: CHAPTER 14 ■ NETWORK PROGRAMMING
- Page 340 and 341: CHAPTER 14 ■ NETWORK PROGRAMMING
- Page 342 and 343: CHAPTER 14 ■ NETWORK PROGRAMMING
- Page 344 and 345: CHAPTER 15 ■ ■ ■ Python and t
- Page 346 and 347: CHAPTER 15 ■ PYTHON AND THE WEB 3
- Page 348 and 349: CHAPTER 15 ■ PYTHON AND THE WEB 3
- Page 350 and 351: CHAPTER 15 ■ PYTHON AND THE WEB 3
- Page 352 and 353: CHAPTER 15 ■ PYTHON AND THE WEB 3
- Page 354 and 355: CHAPTER 15 ■ PYTHON AND THE WEB 3
- Page 356 and 357: CHAPTER 15 ■ PYTHON AND THE WEB 3
- Page 358 and 359: CHAPTER 15 ■ PYTHON AND THE WEB 3
- Page 360 and 361: CHAPTER 15 ■ PYTHON AND THE WEB 3
- Page 362 and 363: CHAPTER 15 ■ PYTHON AND THE WEB 3
CHAPTER 12 ■ GRAPHICAL USER INTERFACES 283<br />
btn.size = win.size = 200, 100<br />
win.show()<br />
Note that one additional event handler has been added here (closeHandler) because the Close but<strong>to</strong>n doesn’t<br />
have any useful default behavior in Java Swing. Also note that you don’t have <strong>to</strong> explicitly enter the main event loop<br />
because it’s running in parallel with the program (in a separate thread).<br />
Using Something Else<br />
The basics of most GUI <strong>to</strong>olkits are the same; unfortunately, however, when learning how <strong>to</strong><br />
use a new package, it takes time <strong>to</strong> find your way through all the details that enable you <strong>to</strong> do<br />
exactly what you want. So you should take your time before deciding which package you want<br />
<strong>to</strong> work with (the section “A Plethora of Platforms” earlier in this chapter should give you some<br />
idea of where <strong>to</strong> start), and then immerse yourself in its documentation and start writing code.<br />
I hope this chapter has provided the basic concepts you need <strong>to</strong> make sense of that documentation.<br />
A Quick Summary<br />
Once again, let’s review what we’ve covered in this chapter:<br />
Graphical user interfaces. Graphical user interfaces are useful in making your programs<br />
more user-friendly. Not all programs need them, but whenever your program interacts<br />
with a user, a GUI is probably helpful.<br />
GUI platforms for <strong>Python</strong>. Many GUI platforms are available <strong>to</strong> the <strong>Python</strong> programmer.<br />
Although this richness is definitely a boon, the choice can sometimes be difficult.<br />
wx<strong>Python</strong>. wx<strong>Python</strong> is a mature and feature-rich cross-platform GUI <strong>to</strong>olkit for <strong>Python</strong>.<br />
Layout. You can position components quite simply by specifying their geometry directly.<br />
However, <strong>to</strong> make them behave properly when their containing window is resized, you will<br />
have <strong>to</strong> use some sort of layout manager. One common layout mechanism in wx<strong>Python</strong><br />
is sizers.<br />
Event handling. Actions performed by the user trigger events in the GUI <strong>to</strong>olkit. To be of<br />
any use, your program will probably be set up <strong>to</strong> react <strong>to</strong> some of these events; otherwise<br />
the user won’t be able <strong>to</strong> interact with it. In wx<strong>Python</strong>, event handlers are added <strong>to</strong> components<br />
with the Bind method.<br />
What Now?<br />
That’s it. You now know how <strong>to</strong> write programs that can interact with the outside world<br />
through files and GUIs. In the next chapter you learn about another important component<br />
of many program systems: Databases.