tkfontchooser¶
A simple font chooser for Tkinter that allow the user to select the font family among the fonts available on his/her system. The size and style (bold, italic, underline, strikethrough) of the text can be set too.
This module contains a FontChooser
class which implements the font
chooser and an askfont
function that displays the font chooser and
returns the chosen font when the user closes the font chooser. The font
is returned as a dictionary like the one returned by the function
tkFont.Font.actual
.
Installation¶
Requirements¶
- Linux, Windows, Mac
- Python 2 or 3 with tkinter and ttk
Install¶
Ubuntu: use the PPA ppa:j-4321-i/ppa
$ sudo add-apt-repository ppa:j-4321-i/ppa $ sudo apt-get update $ sudo apt-get install python(3)-tkfontchooser
Archlinux:
the package is available in AUR
With pip:
$ pip install tkfontchooser
Documentation¶
askfont¶
-
tkfontchooser.
askfont
(master=None, text='Abcd', title='Font Chooser', **font_args)[source]¶ Open the font chooser and return a dictionary of the font properties.
General Arguments:
- master : Tk or Toplevel instance
- master window
- text : str
- sample text to be displayed in the font chooser
- title : str
- dialog title
Font arguments:
- family : str
- font family
- size : int
- font size
- slant : str
- “roman” or “italic”
- weight : str
- “normal” or “bold”
- underline : bool
- whether the text is underlined
- overstrike : bool
- whether the text is overstriked
Output:
dictionary is similar to the one returned by the
actual
method of a tkinterFont
object:{'family': str, 'size': int, 'weight': 'bold'/'normal', 'slant': 'italic'/'roman', 'underline': bool, 'overstrike': bool}
FontChooser¶
FontChooser
inherits from all Toplevel
methods.
-
class
tkfontchooser.
FontChooser
(master, font_dict={}, text='Abcd', title='Font Chooser', **kwargs)[source]¶ Font chooser dialog.
-
__init__
(master, font_dict={}, text='Abcd', title='Font Chooser', **kwargs)[source]¶ Create a new FontChooser instance.
Arguments:
- master : Tk or Toplevel instance
- master window
- font_dict : dict
dictionnary, like the one returned by the
actual
method of aFont
object:{'family': str, 'size': int, 'weight': 'bold'/'normal', 'slant': 'italic'/'roman', 'underline': bool, 'overstrike': bool}
- text : str
- text to be displayed in the preview label
- title : str
- window title
- kwargs : dict
- additional keyword arguments to be passed to
Toplevel.__init__
-
Example¶
try:
from tkinter import Tk
from tkinter.ttk import Style, Button, Label
except ImportError:
from Tkinter import Tk
from ttk import Style, Button, Label
from sys import platform
from tkfontchooser import askfont
# create main window
root = Tk()
style = Style(root)
if "win" == platform[:3]:
style.theme_use('vista')
elif "darwin" in platform:
style.theme_use('clam')
else:
style.theme_use('clam')
bg = style.lookup("TLabel", "background")
root.configure(bg=bg)
label = Label(root, text='Chosen font: ')
label.pack(padx=10, pady=(10,4))
def callback():
# open the font chooser and get the font selected by the user
font = askfont(root)
# font is "" if the user has cancelled
if font:
# spaces in the family name need to be escaped
font['family'] = font['family'].replace(' ', '\ ')
font_str = "%(family)s %(size)i %(weight)s %(slant)s" % font
if font['underline']:
font_str += ' underline'
if font['overstrike']:
font_str += ' overstrike'
label.configure(font=font_str, text='Chosen font: ' + font_str.replace('\ ', ' '))
Button(root, text='Font Chooser', command=callback).pack(padx=10, pady=(4,10))
root.mainloop()
Changelog¶
tkfontchooser 2.0.1¶
- Fix bug when font size not in list (when navigating with up/down arrows)
- Fix bug when font family does not exists
tkfontchooser 2.0.0¶
- Change module name to lowercase to respect PEP 8 naming guidelines
tkFontChooser 1.0.3¶
- Close #1: Example in README.rst doesn’t work
- Add title option in FontChooser and askfont
tkFontChooser 1.0.2¶
- Entry content is updated when font is selected by keypress navigation
tkFontChooser 1.0.1¶
- Improve navigation with keyboard
- Bind Ctrl+A to “select all” instead of “go to beginning”
tkFontChooser 1.0.0¶
- Initial version