0

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?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • As you can [see](https://web.archive.org/web/20190511024508id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/button.html) tkinter `Buttons` have a `font=` option, which you can use to control the type face and size of the [font](https://web.archive.org/web/20190525053857id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/fonts.html) that they use. – martineau Jan 28 '20 at 02:16
  • Does this answer your question? [Modify the default font in Python Tkinter](https://stackoverflow.com/questions/15462647/modify-the-default-font-in-python-tkinter) – stovfl Jan 28 '20 at 08:35
  • Does this answer your question? [Change font size without messing with Tkinter button size](https://stackoverflow.com/questions/42840070/change-font-size-without-messing-with-tkinter-button-size) – DaniyalAhmadSE Jan 28 '20 at 08:45
  • When you ask how to change the size, are you asking how to set it to a specific font when you create the widget, or how to change it once it has already been set? – Bryan Oakley Jan 28 '20 at 13:53

2 Answers2

2

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

schwartz721
  • 767
  • 7
  • 19
  • The line `n1.config(font=(font, 16))` doesn't do what you think it does. For example, if the font was previously set to `"Courier 24"`, then `n1.config(font=(font, 16))` will be the same as `n1.config(font=("Courier 24" 16))`, and since "Courier 24" isn't the name of a valid font, tkinter will substituted the default font. – Bryan Oakley Jan 28 '20 at 02:15
  • @BryanOakley Don't fonts default to something like TkDefaultFont? For the font to be Courier 24, Ruben would have had to manually choose that font, which would mean he wouldn't need to use `cget('font')` to find the name of the matching font. I recommended he use `cget('font')` to find the name of the default, because it might be different depending on the widget or platform. Please correct me if I'm wrong. – schwartz721 Jan 28 '20 at 07:51
  • Yes, the font likely (though not definitely) defaults to `TkDefaultFont`, but there's no guarantee that it hasn't been changed. This is not a good general-purpose solution because it will only work once, and only under certain conditions. After you've set the font to something, it will no longer work. – Bryan Oakley Jan 28 '20 at 13:51
1

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"))
Daniel Huckson
  • 1,157
  • 1
  • 13
  • 35