Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter Font Chooser

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.

like image 577
SaulTigh Avatar asked May 03 '11 14:05

SaulTigh


2 Answers

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

like image 107
Carlos Tasada Avatar answered Oct 12 '22 11:10

Carlos Tasada


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)

like image 31
scotty3785 Avatar answered Oct 12 '22 11:10

scotty3785