开发者

Python Tkinter Font Chooser

I'm trying to w开发者_运维百科rite 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.


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


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)


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜