Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

tkinter

tk

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()             
like image 602
Abkb Avatar asked Dec 14 '13 21:12

Abkb


People also ask

How do I change the font size in a button in Python?

Build A Paint Program With TKinter and Python In order to add styling in the button widgets, first create an instance of Button widget using a variable. Then, add some property like fontfamily, font-size, padding, etc. into it. The most general way to resize the button is by resizing the text in it.


1 Answers

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.

like image 73
Mark Mikofski Avatar answered Oct 14 '22 14:10

Mark Mikofski