5

I'm trying to write simple notepad with Tkinter. And I need some font chooser. So my question is: is there included one? and if there isn't, where can I get one?

Thanks.

SaulTigh
  • 913
  • 2
  • 14
  • 27

4 Answers4

4

Tk (and Tkinter) doesn't have any font chooser in the default distribution. You'll need to create your own. Here is an example I found: Tkinter FontChooser

Note: Tk 8.6 will have a build in font chooser dialog: FontChooser

patthoyts
  • 32,320
  • 3
  • 62
  • 93
Carlos Tasada
  • 4,438
  • 1
  • 23
  • 26
  • 1
    Although it doesn't have a font dialog, it does have a function that returns all the installed fonts (in tuple form), which you can use to make a custom dialog. That is what code at the the webpage that Carlos Tasada posted does. The function is found in the `tkinter.font` module in Python 3.x, or the `tkFont` module in Python 2.x. The function is called families. – Brōtsyorfuzthrāx Jun 07 '14 at 01:01
  • That site doesn't exist or it just wont display – Hippolippo Feb 06 '18 at 17:23
  • The first link in this answer is now dead. – Bryan Oakley Sep 09 '20 at 23:59
1

The basis for a simple font selector can be found below

import tkinter as tk
from tkinter import font


def changeFont(event):
    selection = lbFonts.curselection()
    laExample.config(font=(available_fonts[selection[0]],"16"))

root = tk.Tk()
available_fonts = font.families()


lbFonts = tk.Listbox(root)
lbFonts.grid()

for font in available_fonts:
    lbFonts.insert(tk.END, font)

lbFonts.bind("<Double-Button-1>", changeFont)

laExample = tk.Label(root,text="Click Font")
laExample.grid()



root.mainloop()

Double clicking on a font in the list will change the example text below to that font. You can scroll down through the list of fonts by using a mouse wheel (or you can add a scroll bar to it)

scotty3785
  • 6,763
  • 1
  • 25
  • 35
1

There is a font chooser window in tkinter but it has been discontinued in the newer versions but you can still access it.

l = ttk.Label(root, text="Hello World", font="helvetica 24")
l.grid(padx=10, pady=10)

def font_changed(font):
    l['font'] = font

root.tk.call('tk', 'fontchooser', 'configure', '-font', 'helvetica 24', '-command', root.register(font_changed))
root.tk.call('tk', 'fontchooser', 'show')

Check this link for more details: https://tkdocs.com/tutorial/windows.html

peter
  • 104
  • 8
0

Use rvfont module for font chooser It is easy to use

pip install rvfont

code:

from rvfont.rvfontchooser import FontDialog
fonts=FontDialog()

It will give fonts option in dictionary

svlasov
  • 9,923
  • 2
  • 38
  • 39