Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 12 ■ GRAPHICAL USER INTERFACES 281 Example To illustrate the various packages, I’ve created a simple example—simpler, even, than the editor example used earlier in the chapter. It’s just a single window containing a single button with the label “Hello” filling the window. When you click the button, it prints out the words “Hello, world!” A simple wxPython version is shown in Listing 12-7. Listing 12-7. A Simple GUI Example import wx def hello(event): print "Hello, world!" app = wx.App() win = wx.Frame(None, title="Hello, wxPython!", size=(200, 100)) button = wx.Button(win, label="Hello") button.Bind(wx.EVT_BUTTON, hello) win.Show() app.MainLoop() In the interest of simplicity, I’m not using any fancy layout features here. The resulting window is shown in Figure 12-6. Figure 12-6. A simple GUI example Using Tkinter Tkinter is an old-timer in the Python GUI business. It is a wrapper around the Tk GUI toolkit (associated with the programming language Tcl). It is included by default in the Windows distribution. The following URLs may be useful: • http://www.ibm.com/developerworks/linux/library/l-tkprg • http://www.nmt.edu/tcc/help/lang/python/tkinter.pdf
282 CHAPTER 12 ■ GRAPHICAL USER INTERFACES Here is the GUI example implemented with Tkinter: from Tkinter import * def hello(): print 'Hello, world' win = Tk() # Tkinter's 'main window' win.title('Hello, Tkinter! ') win.geometry('200x100') # Size 200, 200 Example btn = Button(win, text='Hello ', command=hello) btn.pack(expand=YES, fill=BOTH) mainloop() Using Jython and Swing If you’re using Jython (the Java implementation of Python), packages such as wxPython and Tkinter aren’t available. The only GUI toolkits that are readily available are the Java standard library packages AWT and Swing (Swing is the most recent and considered the standard Java GUI toolkit). The good news is that both of these are automatically available so you don’t have to install them separately. For more information, visit the Jython Web site and look into the Swing documentation written for Java: • http://www.jython.org • http://java.sun.com/docs/books/tutorial/uiswing Example Here is the GUI example implemented with Jython and Swing: from javax.swing import * import sys def hello(event): print 'Hello, world! ' btn = JButton('Hello') btn.actionPerformed = hello win = JFrame('Hello, Swing!') win.contentPane.add(btn) def closeHandler(event): sys.exit() win.windowClosing = closeHandler
- Page 262 and 263: CHAPTER 10 ■ BATTERIES INCLUDED 2
- 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: 280 CHAPTER 12 ■ GRAPHICAL USER I
- Page 316 and 317: CHAPTER 13 ■ ■ ■ Database Sup
- Page 318 and 319: CHAPTER 13 ■ DATABASE SUPPORT 287
- Page 320 and 321: CHAPTER 13 ■ DATABASE SUPPORT 289
- Page 322 and 323: CHAPTER 13 ■ DATABASE SUPPORT 291
- Page 324 and 325: CHAPTER 13 ■ DATABASE SUPPORT 293
- Page 326: CHAPTER 13 ■ DATABASE SUPPORT 295
- Page 329 and 330: 298 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 331 and 332: 300 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 333 and 334: 302 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 335 and 336: 304 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 337 and 338: 306 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 339 and 340: 308 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 341 and 342: 310 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 343 and 344: 312 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 345 and 346: 314 CHAPTER 15 ■ PYTHON AND THE W
- Page 347 and 348: 316 CHAPTER 15 ■ PYTHON AND THE W
- Page 349 and 350: 318 CHAPTER 15 ■ PYTHON AND THE W
- Page 351 and 352: 320 CHAPTER 15 ■ PYTHON AND THE W
- Page 353 and 354: 322 CHAPTER 15 ■ PYTHON AND THE W
- Page 355 and 356: 324 CHAPTER 15 ■ PYTHON AND THE W
- Page 357 and 358: 326 CHAPTER 15 ■ PYTHON AND THE W
- Page 359 and 360: 328 CHAPTER 15 ■ PYTHON AND THE W
- Page 361 and 362: 330 CHAPTER 15 ■ PYTHON AND THE W
282 CHAPTER 12 ■ GRAPHICAL USER INTERFACES<br />
Here is the GUI example implemented with Tkinter:<br />
from Tkinter import *<br />
def hello(): print 'Hello, world'<br />
win = Tk() # Tkinter's 'main window'<br />
win.title('Hello, Tkinter! ')<br />
win.geometry('200x100') # Size 200, 200<br />
Example<br />
btn = But<strong>to</strong>n(win, text='Hello ', command=hello)<br />
btn.pack(expand=YES, fill=BOTH)<br />
mainloop()<br />
Using Jython and Swing<br />
If you’re using Jython (the Java implementation of <strong>Python</strong>), packages such as wx<strong>Python</strong> and<br />
Tkinter aren’t available. The only GUI <strong>to</strong>olkits that are readily available are the Java standard<br />
library packages AWT and Swing (Swing is the most recent and considered the standard Java<br />
GUI <strong>to</strong>olkit). The good news is that both of these are au<strong>to</strong>matically available so you don’t have<br />
<strong>to</strong> install them separately. For more information, visit the Jython Web site and look in<strong>to</strong> the<br />
Swing documentation written for Java:<br />
• http://www.jython.org<br />
• http://java.sun.com/docs/books/tu<strong>to</strong>rial/uiswing<br />
Example<br />
Here is the GUI example implemented with Jython and Swing:<br />
from javax.swing import *<br />
import sys<br />
def hello(event): print 'Hello, world! '<br />
btn = JBut<strong>to</strong>n('Hello')<br />
btn.actionPerformed = hello<br />
win = JFrame('Hello, Swing!')<br />
win.contentPane.add(btn)<br />
def closeHandler(event): sys.exit()<br />
win.windowClosing = closeHandler