Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Simple font chooser for Tkinter

Project Description

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.

Requirements

  • Linux, Windows, Mac
  • Python 2 or 3 with tkinter + ttk (default for Windows but not for Linux)

Installation

  • 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 on AUR

  • With pip:

    $ pip install tkfontchooser
    

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()
Release History

Release History

This version
History Node

2.0.1

History Node

2.0.0

Download Files

Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
tkfontchooser-2.0.1-py2.7.egg (26.0 kB) Copy SHA256 Checksum SHA256 2.7 Egg Oct 1, 2017
tkfontchooser-2.0.1-py2.py3-none-any.whl (14.1 kB) Copy SHA256 Checksum SHA256 py2.py3 Wheel Oct 1, 2017
tkfontchooser-2.0.1-py3.6.egg (26.1 kB) Copy SHA256 Checksum SHA256 3.6 Egg Oct 1, 2017
tkfontchooser-2.0.1.tar.gz (21.4 kB) Copy SHA256 Checksum SHA256 Source Oct 1, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting