How to change font and size of buttons and frame in tkinter using python?
Asked Answered
T

4

21

This is the code that i used to generate a simple text box and a button in tkinter.

What should be the parameters to have a better look of the frame and buttons?

 root = Tk.Tk()

 def submit():
    query = entry.get()
    retrieve(query)
    entry = Tk.Entry(root)
    entry.pack()
    button = Tk.Button(root, text='submit', command=submit)
    button.pack()
    root.mainloop()             
Toliver answered 14/12, 2013 at 21:22 Comment(0)
T
20

UPDATE: The New Mexico Tech tkinter website has been archived on GitHub.

First the best reference for Tkinter is this New Mexico Tech website. In the toc you will find a section on fonts, and in the section on Button widgets you'll find the option font.

you must have a Tkinter object to create a font

Python-2

Support for Python-2 has officially ended as of Jan 1, 2020

from Tkinter import *  # Note: UPPER case "T" in Tkinter
import tkFont
root = Tk()

Python-3

Python-3 Tk wrappers differ from Python-2

from tkinter import *  # Note: lower case "t" in tkinter
from tkinter import font as tkFont  # for convenience
root = Tk()

create a font like the example from New Mexico Tech website

helv36 = tkFont.Font(family='Helvetica', size=36, weight='bold')
# you don't have to use Helvetica or bold, this is just an example

(Note: recall for Python-3 font was imported as tkFont for convenience)

now you can set the font for button created from Button in the original post

button['font'] = helv36

The size of the button will depend on your geometry manager, EG: grid or pack. Only the grid method is covered in the layouts section by New Mexico Tech site, but effbot.org is also a great reference and he covers pack pretty well.

try:  # Python-2
    from Tkinter import *
    import tkFont
except ImportError:  # Python-3
    from tkinter import *
    from tkinter import font as tkFont
# using grid
# +------+-------------+
# | btn1 |    btn2     |
# +------+------+------+
# | btn3 | btn3 | btn4 |
# +-------------+------+
root = Tk()
# tkFont.BOLD == 'bold'
helv36 = tkFont.Font(family='Helvetica', size=36, weight=tkFont.BOLD)
btn1 = Button(text='btn1', font=helv36)
btn2 = Button(text='btn2', font=helv36)
btn3 = Button(text='btn3', font=helv36)
btn4 = Button(text='btn4', font=helv36)
btn5 = Button(text='btn5', font=helv36)
root.rowconfigure((0,1), weight=1)  # make buttons stretch when
root.columnconfigure((0,2), weight=1)  # when window is resized
btn1.grid(row=0, column=0, columnspan=1, sticky='EWNS')
btn2.grid(row=0, column=1, columnspan=2, sticky='EWNS')
btn3.grid(row=1, column=0, columnspan=1, sticky='EWNS')
btn4.grid(row=1, column=1, columnspan=1, sticky='EWNS')
btn5.grid(row=1, column=2, columnspan=1, sticky='EWNS')

Tkinter Button fonts

Also try ttk.

Typical answered 14/12, 2013 at 22:16 Comment(3)
ModuleNotFoundError: No module named 'tkFont' in line 1 :(Duckett
Sorry @AbdullahSaid My response written nearly 6 years ago was for Python-2.7, so I've updated it for Python-. When using Python-3 you must import tkinter.font instead of tkfont.Typical
thanks for the reply @MarkMikofski, sorry I didn't realize this posted 6 years ago haha. I was really confused yesterday but yeah I figure it out already. however, some of the stylings are different between Windows and OSX, especially about buttons inside the grid. I've tried it on both operating machines (Windows and OSX) and result in a huge difference between each other. it's way harder to configure in OSXDuckett
D
8

tkdocs tutorial recommends using named fonts and styles if you want to tweak the appearences:

import random
try:
    import tkinter as Tk
    import tkinter.ttk as ttk
    import tkinter.font as font
except ImportError: # Python 2
    import Tkinter as Tk
    import ttk
    import tkFont as font

def change_font_family(query, named_font):
    named_font.configure(family=random.choice(font.families()))

root = parent = Tk.Tk()
root.title("Change font demo")

# standard named font (everything that uses it will change)
font.nametofont('TkDefaultFont').configure(size=5) # tiny

# you can use your own font
MyFont = font.Font(weight='bold')

query = Tk.StringVar()
ttk.Entry(parent, textvariable=query, font=MyFont).grid() # set font directly
ttk.Button(parent, text='Change Font Family',  style='TButton', # or use style
           command=lambda: change_font_family(query, MyFont)).grid()
query.set("The quick brown fox...")

# change font that widgets with 'TButton' style use
root.after(3000, lambda: ttk.Style().configure('TButton', font=MyFont))
# change font size for everything that uses MyFont
root.after(5000, lambda: MyFont.configure(size=48)) # in 5 seconds
root.mainloop()
Dulci answered 14/12, 2013 at 22:56 Comment(4)
This worked but how can i change the frame size or re-position the button in the frame?Is there any drag and drop interface that works properly over python with Tkinter?Toliver
@AbhijathBenhur: It is a job for a layout manager. See example in @Mark Mikofski's answer. I've never used a GUI builder for tkinter interfaces. You could ask about it as a separate question.Dulci
Use Qt with the Python binding PyQt4. It includes a GUI builder called Qt Designer. There are also help and locale builders (Qt Assistance an Qt Linguist). Note Qt Creator is an IDE for C/C++ and Java. Qt is a mainstream, modern UI framework that abstracts the GUI, help and localization from the backend by using a markup language, called QML, which Qt Designer generates automatically.Typical
ttk which uses style is definitely the way to go if sticking with Tkinter. ttk will look much nicer than the Tkinter widgets, because it's designed to match the OS style. EG: progress bar looks like a Windows 7 progress bar on Windows 7, an XP progress bar on Windows XP and a Gnome progress bar on a Linux with a Gnome desktop, etc. IMHO do not use tix; it hasn't been updated in 5 years.Typical
E
1

use the command font = font.Font(size = 20) to change the looking of text in button

import tkinter.font as fnt
import tkinter as tk
r=tk.Tk()
tk.Button(r,text = "Test", font = fnt.Font(size = 20))
r.mainloop()
Epirus answered 27/4, 2021 at 11:31 Comment(0)
G
0

I struggled with this one as well. It took lots of research.

But I found one way, maybe the easiest way, maybe the only way, probably the most direct way:

Use styling.

In your window class:

     style = ttk.Style(self)
     style.configure('TButton', font = ('Helvetica', 20)

That should do it.

Graver answered 27/2 at 2:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.