Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 12 ■ GRAPHICAL USER INTERFACES 273 Because there are no windows the user can interact with, the program exits immediately. ■Note As you can see from this example, the methods in the wx package are all capitalized, contrary to common practice in Python. The reason for this is that the method names mirror method names from the underlying C++ package, wxWidgets. Even though there is no formal rule against capitalized method or function names, the norm is to reserve such names for classes. Creating Windows and Components Windows, also known as frames, are simply instances of the wx.Frame class. Widgets in the wx framework are created with their parent as the first argument to their constructor. If you’re creating an individual window, there will be no parent to consider, so simply use None, as you see in Listing 12-1. Also, make sure you call the window’s Show method before you call app.MainLoop—otherwise it will remain hidden. (You could also call win.Show in an event handler; I discuss events a bit later.) Listing 12-1. Creating and Showing a Frame import wx app = wx.App() win = wx.Frame(None) win.Show() app.MainLoop() If you run this program, you should see a single window appear, similar to that in Figure 12-2. Figure 12-2. A GUI program with only one window Adding a button to this frame is about as simple as it can be—simply instantiate wx.Button, using win as the parent argument (see Listing 12-2).
274 CHAPTER 12 ■ GRAPHICAL USER INTERFACES Listing 12-2. Adding a Button to a Frame import wx app = wx.App() win = wx.Frame(None) btn = wx.Button(win) win.Show() app.MainLoop() This will give you a window with a single button, as shown in Figure 12-3. Figure 12-3. The program after adding a button There are certainly a few rough corners still; the window has no title, the button has no label, and you probably don’t want the button to cover the entire window in this way . . . Labels and Positions You can set the labels of widgets when you create them, by using the label argument of the constructor. Similarly, you can set the title of frames by using the title argument. I find it most practical to use keyword arguments with the wx constructors, so I don’t have to remember their order. You can see an example of this in Listing 12-3. Listing 12-3. Adding Labels and Titles with Keyword Arguments import wx app = wx.App() win = wx.Frame(None, title="Simple Editor") loadButton = wx.Button(win, label='Open') saveButton = wx.Button(win, label='Save') win.Show() app.MainLoop()
- Page 254 and 255: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 256 and 257: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 258 and 259: CHAPTER 10 ■ BATTERIES INCLUDED 2
- Page 260 and 261: CHAPTER 10 ■ BATTERIES INCLUDED 2
- 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: 272 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 and 314: 282 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
274 CHAPTER 12 ■ GRAPHICAL USER INTERFACES<br />
Listing 12-2. Adding a But<strong>to</strong>n <strong>to</strong> a Frame<br />
import wx<br />
app = wx.App()<br />
win = wx.Frame(None)<br />
btn = wx.But<strong>to</strong>n(win)<br />
win.Show()<br />
app.MainLoop()<br />
This will give you a window with a single but<strong>to</strong>n, as shown in Figure 12-3.<br />
Figure 12-3. The program after adding a but<strong>to</strong>n<br />
There are certainly a few rough corners still; the window has no title, the but<strong>to</strong>n has no<br />
label, and you probably don’t want the but<strong>to</strong>n <strong>to</strong> cover the entire window in this way . . .<br />
Labels and Positions<br />
You can set the labels of widgets when you create them, by using the label argument of the<br />
construc<strong>to</strong>r. Similarly, you can set the title of frames by using the title argument. I find it most<br />
practical <strong>to</strong> use keyword arguments with the wx construc<strong>to</strong>rs, so I don’t have <strong>to</strong> remember their<br />
order. You can see an example of this in Listing 12-3.<br />
Listing 12-3. Adding Labels and Titles with Keyword Arguments<br />
import wx<br />
app = wx.App()<br />
win = wx.Frame(None, title="Simple Edi<strong>to</strong>r")<br />
loadBut<strong>to</strong>n = wx.But<strong>to</strong>n(win, label='Open')<br />
saveBut<strong>to</strong>n = wx.But<strong>to</strong>n(win, label='Save')<br />
win.Show()<br />
app.MainLoop()