Ruben Petronio
Ruben Petronio

Reputation: 31

How to change the size of the text inside the Buttons?

Can someone help me with this issue? I spent 1d searching on the web for a decent answer but found nothing!

'''
Created on 20 gen 2020

@author: ruben
'''
from tkinter import *
import os
import random
from _sqlite3 import Row
import math
from lib2to3.pgen2.token import OP


op = "suca"
num1 = 234
num2 = 0
#CalculatorGui
gui = Tk()
gui.geometry("320x427") 
gui.title("Calculator")


#ScientificCalculator
#def RAD():


#TextArea
text = Text(gui, height='5', width='30')
text.grid(row=0, column=0, columnspan=5, ipadx=37)    
#Buttons
n1 = Button(gui, text='1', fg='black', bg='grey', height='5', width='10', command=press1)
n1.grid(row=1, column=0) 

gui.mainloop()

This is only 5% of all the code. I just need to change the size of the text inside the button n1 = Button(gui, text='1', size?

Upvotes: 0

Views: 281

Answers (2)

schwartz721
schwartz721

Reputation: 1027

Changing the font size of a widget can be done using the font configure option, either when creating the widget or using the widget's config method. The font can be provided using a tuple or a string that contains the name of the font, and optionally a size and style.

n1 = Button(gui, text='1', font=('FontName', 16, 'style'))
n1 = Button(gui, text='1', font='FontName 16 style')
n1.config(font=('FontName', 16, 'style'))
n1.config(font='FontName 16 style')

Above are four options that all accomplish the same thing. To change the size of the text like this, you are required to provide a font name before you provide a text size. This means that if you want your button's text to match the text in other buttons, you'll need to know what font is being used. If you have manually set a font for the rest of the widgets already, use that one. But if you haven't set a font, you can determine the name of the default font using the button's cget() method.

n1 = Button(gui, text='1')
font = n1.cget('font')
n1.config(font=(font, 16))

More info on styling widgets here: https://effbot.org/tkinterbook/tkinter-widget-styling.htm

Upvotes: 2

Daniel Huckson
Daniel Huckson

Reputation: 1217

Use the font argument in the button call.

Button(gui, text='1', fg='black', bg='grey', height='5', width='10', command=press1, font=("Helvetica", 20, "bold"))

Upvotes: 1

Related Questions