3

When I increase the size of the font using following code, it also increase the size of the widget. Is it possitlbe to increase font size by keeping the size of the text widget constant? Thank You

  A11 = tkinter.Text(top, height=28, width=70,background = "#02e0a1")
  labelfont = ('times', 20, 'bold')
  A11.config(font = labelfont)
bibek
  • 167
  • 1
  • 4
  • 12
  • If you increase the size of the font, but keep the widget the same, letters will be cut off unable to read. Why would you want this? – Gumboy Nov 06 '17 at 02:46
  • @Gumboy: not necessarily. The Text widget has the ability to wrap long lines of text, which it does by default unless you turn it off. – Bryan Oakley Nov 06 '17 at 04:41
  • Maybe I didn't under the question properly, I was thinking if you have a 4x4 box and letters are 6X5 the dimension would not allow at the text to show. So I am now assuming the @bibek wants the widget to follow the size of the text. – Gumboy Nov 06 '17 at 04:57
  • Why would you assume the OP wants the widget to follow the size of the text when the question explicitly says _"... by keeping the size of the text widget constant"_? – Bryan Oakley Nov 06 '17 at 14:42

2 Answers2

7

If you force the GUI window to be a specific size, then changing the font of a text widget won't cause the text widget to grow*. It usually helps to set the width and height of the text widget to 1 (one) so that it won't even try to grow when you change the font.

  • well, the widget will try to grow, but the size constraint on the window will prevent the widget from getting too big.

Here's a simple contrived example.

import tkinter as tk
import tkinter.font as tkFont

class Example(object):
    def __init__(self):
        root = tk.Tk()
        self.font = tkFont.Font(family="helvetica", size=18)
        text = tk.Text(root, width=1, height=1, font=self.font)
        button = tk.Button(root, text="Bigger", command=self.bigger)

        button.pack(side="top")
        text.pack(side="top", fill="both", expand=True)

        text.insert("end", "Hello, world!")

        # force the widow to a specific size after it's created
        # so that it won't change size when we change the font
        root.geometry("800x400")

    def start(self):
        tk.mainloop()

    def bigger(self):
        size = int(self.font.cget("size"))
        size += 2
        self.font.configure(size=size)


app = Example()
app.start()

The same technique can work on a smaller scale by putting the size constraint on a frame rather than the root window. If you put the text widget inside a frame, turn geometry propagation off, and then give the frame a fixed size, the widget will not grow. This is one of the few times when turning geometry propagation off can be useful.

Here's a modification of the above example, using this technique:

import tkinter as tk
import tkinter.font as tkFont

class Example(object):
    def __init__(self):
        root = tk.Tk()
        self.font = tkFont.Font(family="helvetica", size=18)
        button = tk.Button(root, text="Bigger", command=self.bigger)

        # create a frame for the text widget, and let it control the
        # size by turning geometry propagation off
        text_frame = tk.Frame(root, width=800, height=400)
        text_frame.pack_propagate(False)
        text = tk.Text(text_frame, width=1, height=1, font=self.font)
        text.pack(side="top", fill="both", expand=True)

        button.pack(side="top")
        text_frame.pack(side="top", fill="both", expand=True)

        text.insert("end", "Hello, world!")

    def start(self):
        tk.mainloop()

    def bigger(self):
        size = int(self.font.cget("size"))
        size += 2
        self.font.configure(size=size)


app = Example()
app.start()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • 1
    Thanks! This really helped me, since my Text widget kept getting really big with increased font size. I just set its height and width to 1 as Bryan said in his answer, and then packed it with fill set to BOTH and expand to True. – redbeam_ Sep 27 '19 at 23:08
0

Widget size is determined by font size, so width=10 for a small font is smaller than width=10 for a large font. The following code only changes the font size.

import sys
if sys.version_info[0] < 3:
    import Tkinter as tk     ## Python 2.x
else:
    import tkinter as tk     ## Python 3.x

class DifferentFonts():
    def __init__(self):
        self.top=tk.Tk()

        tk.Label(self.top, text="Small Font", width=10, bg="lightblue",
                font=('DejaVuSansMono', 10)).grid(row=1)
        tk.Label(self.top, text="Large Font", width=10, bg="lightyellow",
                 font=('DejaVuSansMono', 30)).grid(row=2)

        tk.Button(self.top, text="Quit", bg="orange",
               command=self.top.quit).grid(row=20)

        self.top.mainloop()

DifferentFonts()