How can I use a font picker

The font picker dialog in wxPython is different than the file dialog, because it uses a separate helper class to manage the information it presents. Figure 9.8 displays the MS Windows version of the font dialog.

Listing 9.8 displays the code used to generate figure 9.8, and should look somewhat different than previous dialog examples.

Listing 9.8 Sample code for a font picker dialog box import wx if __name__ == "__main__": app = wx.PySimpleApp()

dialog = wx.FontDialog(None, wx.FontData()) if dialog.ShowModal() == wx.ID_OK: data = dialog.GetFontData() font = data.GetChosenFont() colour = data.GetColour()

print 'You selected: "%s", %d points\n' % (

font.GetFaceName(), font.GetPointSize()) dialog.Destroy()

Figure 9.8

A sample font picker dialog

The constructor for wx.FontDialog is much simpler than the previous constructors.

wx.FontDialog(parent, data)

You cannot set a message or caption for this dialog, and the information that is ordinarily passed as style flags is instead contained in the data argument, which is of the class wx.FontData. The wx.FontDialog class has only one useful method of its own, which is GetFontData(), returning the font data instance.

The wx.FontData instance allows you to set the values that govern the display of the font dialog, and also contains the information entered by the user. For example, in listing 9.8 the code calls two getter methods of the wx.FontData instance to determine the details of the selected font. The constructor for wx.FontData takes no arguments—all properties must be set by using the methods in table 9.4

Table 9.4 Methods of wx.FontData

Method

Description

GetAllowSymbolsO SetAllowSymbols(allowSymbols)

Determines whether symbol-only fonts (like dingbats) are displayed in the dialog. The argument is a Boolean. Only meaningful in Windows systems. The initial value of this property is True.

GetChosenFont() SetChosenFont(font)

Returns the font that the user has chosen as a wx.Font object. You should never need to call the setter for this property. If the user has selected Cancel, this property is None. The wx.Font class will be discussed in more detail in Chapter 12.

Table 9.4 Methods of wx.FontData (continued)

Method

Description

GetColour() SetColour(colour)

Returns the color selected in the color portion of the dialog. The setter allows you to preset the default value. The getter returns a wx.Colour instance. The setter can take one of those, or a string with the name of a color. The initial value of this property is black.

GetEnableEffectsO EnableEffects(enable)

in the MS Windows version of the dialog, this property controls the appearance or nonappearance of controls to select color, strikeout, and underline features of the font.

GetinitialFontO SetInitialFont(font)

Returns the font which is the initial value of the dialog (i.e., the current application font). This property should be explicitly set by the application before the dialog is displayed. its initial value is None.

SetRange(min, max)

Sets the available range for the point size of the font. Only used on MS Windows systems. The initial values are 0 and 0, which means there are no limits on the range.

GetShowHelp() SetShowHelp()

if True, the MS Windows version of this dialog will display a Help button. The initial value is False.

A convenience function for the font dialog, which helpfully sidesteps the whole wx.FontData class, is as follows.

wx.GetFontFromUser(parent, fontInit)

The fontInit argument is an instance of wx.Font that is used as the initial value of the dialog. The return value of the function is a wx.Font instance. If the user closes the dialog with an OK, the method wx.Font.Ok() returns True, otherwise, it returns False.

Was this article helpful?

0 0

Responses

  • luana
    How to use font picker tkinter python?
    9 months ago

Post a comment