#!/usr/bin/env python
#
# $Id: FontChooserDialog.py,v 1.4 2001/11/03 11:05:22 doughellmann Exp $
#
# Copyright 2001 Doug Hellmann.
#
#
# All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and
# its documentation for any purpose and without fee is hereby
# granted, provided that the above copyright notice appear in all
# copies and that both that copyright notice and this permission
# notice appear in supporting documentation, and that the name of Doug
# Hellmann not be used in advertising or publicity pertaining to
# distribution of the software without specific, written prior
# permission.
#
# DOUG HELLMANN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
# INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
# NO EVENT SHALL DOUG HELLMANN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
# CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
# OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
# CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
"""A dialog class for choosing a font specification.
"""
__rcs_info__ = {
#
# Creation Information
#
'module_name' : '$RCSfile: FontChooserDialog.py,v $',
'rcs_id' : '$Id: FontChooserDialog.py,v 1.4 2001/11/03 11:05:22 doughellmann Exp $',
'creator' : 'Doug Hellmann <doug@hellfly.net>',
'project' : 'PmwContribD',
'created' : 'Sun, 01-Apr-2001 13:02:08 EDT',
#
# Current Information
#
'author' : '$Author: doughellmann $',
'version' : '$Revision: 1.4 $',
'date' : '$Date: 2001/11/03 11:05:22 $',
}
#
# Import system modules
#
from Tkinter import *
import tkFont
import Pmw
import string
#
# Import Local modules
#
#
# Module
#
class FontChooserDialog(Pmw.Dialog):
"""A dialog class for choosing a font specification.
Options
title -- Dialog title, string.
buttons -- Buttons to create, sequence of names.
defaultbutton -- Index in 'buttons' of button which should be
default.
family -- The font family of the selected font. Defaults to
'helvetica'.
bold -- Boolean indicating whether bold is enabled for the
font.
italic -- Boolean indicating whether italic is enabled for the
font.
underline -- Boolean indicating whether underline is enabled
for the font.
overstrike -- Boolean indicating whether overstrike is enabled
for the font.
size -- Integer font size specification.
"""
OK='OK'
CANCEL='Cancel'
font_options=('bold', 'italic', 'underline', 'overstrike')
def __init__(self, parent=None, **kw):
optiondefs = (
('title', 'Font Chooser', self._settitle),
('buttons', (self.OK, self.CANCEL), self._buttons),
('defaultbutton', 0, self._defaultButton),
('family', 'helvetica', None),
('bold', 0, None),
('italic', 0, None),
('underline', 0, None),
('overstrike', 0, None),
('size', 12, None),
)
self.defineoptions(kw, optiondefs)
Pmw.Dialog.__init__(self, parent)
self.createInterior()
self.createFontParametersPage()
self.initialiseoptions(FontChooserDialog)
self.withdraw()
return
def value(self):
"""Returns the font spec for the current font.
Returns a fontspec representation of the dialog option
settings as configured by the user.
"""
fontspec = (self['family'], self['size'])
for option in self.font_options:
if self[option]:
fontspec = fontspec + (option,)
return fontspec
def activate(self):
"""Show the dialog.
Display the dialog and return value() or None, depending on
button pressed.
"""
self.updateSelected()
self.updateExample()
button = Pmw.Dialog.activate(self)
if button == self.OK:
return self.value()
else:
return None
def updateSelected(self):
"Update the contents of the dialog based on selections."
lbx = self.component('family')
family_list = list(lbx.get())
idx = family_list.index(string.lower(self['family']))
lbx.selection_set(idx)
lbx.yview(idx)
for option in self.font_options:
self.option_vars[option].set(self[option])
return
def createFontParametersPage(self):
"Create the part of the dialog which allows basic font parameters to be set."
self.component('notebook').add('paramgroup',
tab_text='Font Parameters'
)
paramgroup = self.component('notebook').page('paramgroup')
#
# Set the font family
#
family = self.createcomponent(
'family',
(), None,
Pmw.ScrolledListBox,
(paramgroup,),
labelpos='nw',
label_text='Family',
listbox_exportselection=NO,
listbox_height=5,
selectioncommand=self.familyCB,
)
family_list = list(tkFont.families(self.interior()))
family_list.sort()
family.setlist(family_list)
family.pack(side=LEFT, expand=YES, fill=BOTH,)
#
# Boldness, italic, etc.
#
self.option_vars = {}
optiongroup = self.createcomponent(
'options', (), None, Pmw.Group, (paramgroup,),
tag_text='Options',
)
optionframe = optiongroup.interior()
row=0
for option_name in self.font_options:
var = BooleanVar(optionframe)
self.option_vars[option_name] = var
var.set(self[option_name])
self.createcomponent(option_name, (), None, Checkbutton, (optionframe,),
variable=var,
text=string.capwords(option_name),
command=self.optionChangedCB,
).grid(row=row, column=0, sticky='w')
row=row+1
#
# Size
#
self.sizevar = IntVar(optionframe)
self.sizevar.set(self['size'])
size = self.createcomponent(
'size', (), None, Pmw.ComboBox, (optionframe,),
labelpos='w', label_text='Size:',
entry_textvariable=self.sizevar,
entryfield_modifiedcommand=self.sizeCB,
entry_width=3,
)
for item in range(1,51):
size.component('listbox').insert('end', item)
size.grid(row=row, column=0, sticky='w')
optiongroup.pack(side=LEFT, expand=NO, fill=Y, anchor=N)
return
def createInterior(self):
"Create the interface for the dialog."
notebook = self.createcomponent(
'notebook',
(), None,
Pmw.NoteBook,
(self.interior(),),
)
notebook.pack(side=TOP, expand=YES, fill=BOTH)
grp = Pmw.Group( self.interior(), tag_text='Example' )
example = self.createcomponent(
'example',
(), None,
Pmw.ScrolledText,
(grp.interior(),),
text_width=5,
text_height=5,
text_wrap='none',
)
example.tag_configure('example', lmargin1=5, lmargin2=5)
example.insert('end', string.uppercase, 'example')
example.insert('end', '\n', 'example')
example.insert('end', string.lowercase, 'example')
example.insert('end', '\n', 'example')
example.insert('end', string.digits, 'example')
example.insert('end', '\n', 'example')
example.insert('end', '!@#$%^&*()-_=+[]{}\|,.<>/?', 'example')
example.configure(text_state='disabled')
example.pack(side=TOP, expand=YES, fill=BOTH,)
grp.pack(side=TOP, expand=YES, fill=BOTH,)
#
# Update example
#
self.updateExample()
return
def optionChangedCB(self, *args):
"Called when one of the option checkbuttons was changed."
param_dict={}
for option_name, option_var in self.option_vars.items():
param_dict[option_name] = option_var.get()
apply(self.configure, (), param_dict)
self.updateExample()
return
def familyCB(self, *args):
"The family value was changed."
list = self.component('family')
sel = list.getcurselection()
if not sel:
return
self.configure(family=sel[0])
self.updateExample()
return
def sizeCB(self, *args):
"The size value was changed."
self.configure(size=self.sizevar.get())
self.updateExample()
return
def updateExample(self):
"""Change the font displayed in the example indicator.
Get all of our parameters and set the font in
the example label.
"""
fontspec = self.value()
#self.component('example').configure(font=fontspec)
self.component('example').tag_configure(
'example',
font=fontspec,
)
return
if __name__ == '__main__':
fontspec = FontChooserDialog().activate()
print 'Result: ', fontspec
|