How do I change the color or font of a grid cell

Wxpython Color Cell

As with other controls, there are a set of properties that you can use to change the display attributes for each cell. Figure 14.5 displays some of what you can do with the attribute methods. Listing 14.6 displays the code used to create figure 14.5. Notice the use of both grid methods aimed at a specific cell and of the creation of wx.grid.Grid-CellAttr objects. Figure 14.5 A sample usage of the grid attribute methods Listing 14.6 Changing the color of grid cells wx.Frame._init_ self, None,...

How do I manage the row and column headers of a grid

In a wxPython grid control, each row and column has its own label. By default, rows are given numeric labels starting with 1 and columns are given alphabetical labels starting with A and continuing to Z, which is followed by aa, ab, and so on. If you're creating a spreadsheet, this is great, but not necessary for most other applications. For something a bit less generic, wxPython provides methods to change the labels. Figure 14.3 displays a sample grid with label headers. Figure 14.3 A sample...

Creating tree controls and adding items

Wxpython Tree Control Clear Focused Item

A tree control is an instance of the class wx.TreeCtrl. Figure 15.1 displays a sample tree control. Listing 15.1 shows the code used to generate that example. Notice that the tree is driven by an external structure stored in a file called data.py. We won't print that file here, but it is available at the book's web site. It consists of a nested list structure of the wxPython class hierarchy, a convenient data set for a tree control. Some of the mechanisms in this sample are discussed later in...

How do I create an alert box

The three simplest ways of interacting with the user via a dialog box are wx.MessageDialog, which represents an alert box, wx.TextEntryDialog, which prompts the user to enter some short text, and wx.SingleChoiceDialog, which allows the user to select from a list of available options. The next three sections discuss these simple dialogs. A message box dialog displays a short message and allows the user to press a button in response. Typically, message boxes are used to display important alerts,...

Around my wxPython application

How Remove Title Bar Wxpython

Let's assume that you've used wxPython to create a program, and your program is working, and now you'd like a better understanding of exactly how it works. You've seen the PyCrust features listed earlier in this chapter, and they look like they could be very useful in gaining insights into the functioning of your program. But you'd rather not change your program just to be able to use PyCrust. What do you do By passing the name of your program to the PyWrap utility, you can start your program...

How do I use a validator to ensure correct data

A validator object is a subclass of wx.Validator. The parent class is abstract, and isn't used directly. Although there are a couple of predefined validator classes in the C wxWidget set, in wxPython, you will need to define your own validator classes. As we've seen in other cases, your Python classes need to inherit from a Python-specific subclass, wx.PyValidator, to be able to override all the parent methods. A custom validator subclass must also override the method Clone , which should...

Creating a virtual list control

Let's assume that your wxPython application needs to display a list of all your clients. Initially you use a regular list control, and it works fine. Eventually your use of wxPython makes you more and more successful. Your client list gets longer and longer. Too many clients and your application starts to have performance problems. Perhaps it takes a longer amount of time to start up. Probably it starts using more and more memory. What can you do You can create a virtual list control. The...

How do I make a nonrectangular frame

Wxpython Demo

In most applications, frames are rectangles because rectangles have a nice regular shape and are relatively simple for an application to draw and maintain. Sometimes, though, you need to break out of the straight line straitjacket. In wxPython, you can give a frame an arbitrary shape. If an alternate shape is defined, the parts of the frame that are outside the shape are not drawn, and do not respond to mouse events as far as the user is concerned, they are not part of the frame. Figure 8.9...

A wxPython model PyGrid TableBase

Wxpython Listings

The class wx.grid.Grid is the wxPython control for a spreadsheet-style layout of rows and columns. You're probably familiar with the basic concept, but figure 5.3 shows how the wxPython version looks. The grid control has a lot of interesting features, including the ability to create custom renderers and editors on a cell-by-cell basis, as well as dragable rows and columns. Those features will be discussed in greater detail in chapter 13. In this chapter, we'll stick to the basics and show how...

How do I find a specific menu item in a menu

There are a number of ways in wxPython to find a specific menu or menu item given a label or an identifier. You often use these methods in event handlers, especially when you want to modify a menu item or display its label text in another location. Listing 10.4 augments the previous dynamic menu example by using FindltemByld to get the appropriate menu item for display. Listing 10.4 Finding a specific menu item self.txt wx.TextCtrl p, -1, new item btn wx.Button p, -1, Add Menu Item self.Bind...

How do I create a multiline or styled text control

You can create a multi-line text control using the wx.te_multiline style flag. If the native widget has support for styles, you can change font and color styles within the text managed by the control, which is sometimes called rich text. For other platforms, the calls to set styles are simply ignored. Figure 7.3 displays an example of multi-line text controls. Listing 7.3 contains the code used to create figure 7.3. Typically, creating a multi-line control is handled by setting the...

How do I make a slider

Tkinter Slider

A slider is a widget that allows the user to select a number from within a range by dragging a marker across the width or height of the control. In wxPython, the control class is wx.Slider, which includes a read-only text display of the current value of the slider. Figure 7.7 displays examples of a vertical and horizontal slider. Basic slider use is fairly straightforward, but there are a number of events you can add. Figure 7.7 A vertical wx.Slider and a horizontal wx.Slider, which use the...

Whats a miniframe and why would I use it

A mini-frame is just like a regular frame with two primary exceptions it has a smaller title area, and it doesn't display in the window task bar under MS Windows or GTK. Figure 8.8 displays an example of a smaller title area. The code for creating the mini-frame is almost identical to creating a regular frame, the only difference is that the parent class is now wx.MiniFrame. Listing 8.9 displays the code. size 3 00, 100 panel wx.Panel self, -1, size 300, button wx.Button panel, -1, Close Me,...

How can I use a file picker

Wxpython File Dialog

File-choosing dialogs tend to be consistent from application to application. In wxPython, the wx.FileDialog uses the native OS dialog for the major platforms, and uses non-native look-alikes for other operating systems. The MS Windows version is displayed in figure 9.6. You can set up the file dialog to start in any directory, and you can also pass it a wildcard filter to limit the display to only certain file types. Listing 9.6 displays a basic example. wildcard Python source .py .py Compiled...

Working with modal dialogs

Modal dialogs are used for quick interactions with the user or for any time that information in a dialog absolutely must be entered before the user can move forward in the program. Within wxPython, there are several standard functions to display basic modal dialogs. These dialogs include alert boxes, one line of text entry, and choosing from a list. In the following sections, we'll show you what these dialogs look like, and how using the predefined functions will make your life easier.

PyCrust sets the standard for a Python shell

When you work with Python interactively, you work in an environment that is called the Python shell which is similar to other shell environments, such as the DOS window on Microsoft platforms, or the bash command line on Unix-based systems. The most basic of all the Python shells is the one in listing 4.1, which you see when you launch Python from the command line. While it is a useful shell, it is strictly text-based, rather than graphical, and it doesn't provide all the shortcuts or helpful...

Creating a splitter window

Wxsplitterwindow

In wxPython, a splitter window is an instance of the class wx.SplitterWindow. Unlike most other wxPython widgets, splitter windows require further initialization after they are created before they can be used. The constructor is pretty straightforward. Figure 8.10 A sample splitter window after initialization Figure 8.10 A sample splitter window after initialization wx.SplitterWindow parent, id -1, pos wx.DefaultPosition, size wx.DefaultSize, style wx.SP_3D, name splitterWindow The parameters...

How do I create a grid with a grid table

Wxpython Table Grid

In more complicated cases, you can keep your data in a grid table, which is a separate class that stores the data and interacts with the grid control to display the data. The grid table is especially recommended where the data from the grid is complex the data is already stored in other objects in your system the grid is large enough that it should not all be stored in memory at once In chapter 5, we discussed grid tables in the context of the MVC design pattern, along with different ways of...

How do I bind an event to a handler

Event binders consist of instances of the class wx.PyEventBinder. A predefined instance of wx.PyEventBinder is provided for all of the event types supported, and you can create your own event binders for your custom event types when needed. There is one event binder instance for each event type, which means that multiple binders may correspond to any one wx.Event subclass. This is because event types are more detailed than wx.Event subclasses. For example, the wx.Mouse-Event class has fourteen...

How do I associate a menu item with a keyboard shortcut

In wxPython you can set up keyboard navigation and shortcuts for menu items. Figure 10.3 displays a sample menu with keyboard decoration added. Notice that the menu names have an underlined character, and that next to the item labeled Accelerated is a Ctrl-A. Studies have shown that keyboard shortcuts are not always the time saver that you think they are. However, they are standard interface elements, and your users will expect them to be there. Shortcuts are also helpful for users with...

What is a device context and how can I create one

There are ten subclasses of wx.DC in wxPython, split into the following three groups Contexts used for drawing to a screen Contexts used for drawing to a location other than a screen Contexts used to buffer a device context until you are ready to draw it to a screen The first group represents device contexts that draw to the screen. You'd think there would only need to be one of these, but in fact wxPython provides four of them, depending on exactly where you need to draw, and exactly when you...