Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 12 ■ GRAPHICAL USER INTERFACES 275 The result of running the previous program should be something like what you see in Figure 12-4. Figure 12-4. A window with layout problems As you can see, something isn’t quite right about this version of the program; one button seems to be missing! Actually, it’s not missing—it’s just hiding. By placing the buttons more carefully, you should be able to uncover the hidden button. A very basic (and not very practical) method is to simply set positions and size by using the pos and size arguments to the constructors, as in the code presented in Listing 12-4. Listing 12-4. Setting Button Positions import wx app = wx.App() win = wx.Frame(None, title="Simple Editor", size=(410, 335)) win.Show() loadButton = wx.Button(win, label='Open', pos=(225, 5), size=(80, 25)) saveButton = wx.Button(win, label='Save', pos=(315, 5), size=(80, 25)) filename = wx.TextCtrl(win, pos=(5, 5), size=(210, 25)) contents = wx.TextCtrl(win, pos=(5, 35), size=(390, 260), style=wx.TE_MULTILINE | wx.HSCROLL) app.MainLoop() As you can see, both position and size are pairs of numbers. The position is a pair of x and y coordinates, while the size consists of width and height. There are another couple of new things in this piece of code: I’ve created a couple of text controls (wx.TextCtrl objects), and given one of them a custom style. The default text control is a text field, with a single line of editable text, and no scroll bar. In order to create a text area, you
276 CHAPTER 12 ■ GRAPHICAL USER INTERFACES can simply tweak the style with the style parameter. The style is actually a single integer, but you don’t have to specify it directly. Instead, you use bitwise OR (the pipe) to combine various style facets that are available under special names from the wx module. In this case, I’ve combined wx.TE_MULTILINE, to get a multiline text area (which, by default, has a vertical scroll bar), and wx.HSCROLL, to get a horizontal scroll bar. The result of running this program is shown in Figure 12-5. Figure 12-5. Properly positioned components More Intelligent Layout Although specifying the geometry of each component is easy to understand, it can be a bit tedious. Doodling a bit on graph paper may help in getting the coordinates right, but there are more serious drawbacks to this approach than having to play around with numbers. If you run the program and try to resize the window, you’ll notice that the geometries of the components don’t change. This is no disaster, but it does look a bit odd. When you resize a window, you assume that its contents will be resized and relocated as well. If you consider how I did the layout, this behavior shouldn’t really come as a surprise. I explicitly set the position and size of each component, but didn’t say anything about how they should behave when the window was resized. There are many ways of specifying this. One of the easiest ways of doing layout in wx is using sizers, and the easiest one to use is wx.BoxSizer. A sizer manages the size of contents; you simply add widgets to a sizer, together with a few layout parameters, and then give this sizer the job of managing the layout of their parent component. In our case, we’ll add a background component (a wx.Panel), create some nested wx.BoxSizers and then set the sizer of the panel with its SetSizer method, as shown in Listing 12-5. Listing 12-5. Using a Sizer import wx app = wx.App() win = wx.Frame(None, title="Simple Editor", size=(410, 335))
- 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 and 304: 272 CHAPTER 12 ■ GRAPHICAL USER I
- Page 305: 274 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
- Page 355 and 356: 324 CHAPTER 15 ■ PYTHON AND THE W
CHAPTER 12 ■ GRAPHICAL USER INTERFACES 275<br />
The result of running the previous program should be something like what you see in<br />
Figure 12-4.<br />
Figure 12-4. A window with layout problems<br />
As you can see, something isn’t quite right about this version of the program; one but<strong>to</strong>n<br />
seems <strong>to</strong> be missing! Actually, it’s not missing—it’s just hiding. By placing the but<strong>to</strong>ns more<br />
carefully, you should be able <strong>to</strong> uncover the hidden but<strong>to</strong>n. A very basic (and not very practical)<br />
method is <strong>to</strong> simply set positions and size by using the pos and size arguments <strong>to</strong> the construc<strong>to</strong>rs,<br />
as in the code presented in Listing 12-4.<br />
Listing 12-4. Setting But<strong>to</strong>n Positions<br />
import wx<br />
app = wx.App()<br />
win = wx.Frame(None, title="Simple Edi<strong>to</strong>r", size=(410, 335))<br />
win.Show()<br />
loadBut<strong>to</strong>n = wx.But<strong>to</strong>n(win, label='Open',<br />
pos=(225, 5), size=(80, 25))<br />
saveBut<strong>to</strong>n = wx.But<strong>to</strong>n(win, label='Save',<br />
pos=(315, 5), size=(80, 25))<br />
filename = wx.TextCtrl(win, pos=(5, 5), size=(210, 25))<br />
contents = wx.TextCtrl(win, pos=(5, 35), size=(390, 260),<br />
style=wx.TE_MULTILINE | wx.HSCROLL)<br />
app.MainLoop()<br />
As you can see, both position and size are pairs of numbers. The position is a pair of x and<br />
y coordinates, while the size consists of width and height.<br />
There are another couple of new things in this piece of code: I’ve created a couple of text<br />
controls (wx.TextCtrl objects), and given one of them a cus<strong>to</strong>m style. The default text control is<br />
a text field, with a single line of editable text, and no scroll bar. In order <strong>to</strong> create a text area, you