1

As a Python newbie I have a problem with the text widget.

Whenever I try to increase the font size of the text widget , the width and height of it changes too.

Here's the code:

from tkinter import *
from tkinter import font

root = Tk()

fontsize = 16

def increase_font():
    global fontsize
    fontsize += 2
    textfont.config(size = fontsize)

textfont = font.Font(family = "consolas" , size = fontsize)
my_text = Text(root , width = 65 , height = 18 , font = textfont)
my_text.grid(row = 0 , column = 0)
my_text.insert(1.0 , "There is a problem")

my_button = Button(root , text = "Increase Font" , width = 13 , font = "arial 11" , command = increase_font)
my_button.grid(row = 1 , column = 0 , pady = 5)

mainloop()

However , this problem does not seem to occur when I:
1) Use tags in my program
2) Change the font of my text

For example:

def increase_font():
    global fontsize
    fontsize += 2
    my_text.tag_add("increase_size" , 1.0 , END) # using tags
    my_text.tag_config("increase_size" , font = f"consolas {fontsize}") # changing the font
    
    # Problem does not occur

Or

def increase_font():
    global fontsize
    fontsize += 2
    my_text.tag_add("increase_size", 1.0 , END) # using tags
    temp_font = textfont.copy() # creating a new font
    temp_font.config(size = fontsize)
    my_text.tag_config("increase_size" , font=temp_font) # changing the font

    # Problem does not occur

The thing is that I don't want to change the font of the text or create a new font , as it causes problems when I implement it on my original program. All I want is to set a fixed width and height to my text widget(and it should never change no matter what I do with it).

This problem really frustrates me a lot and It would be a great help if anyone could fix this problem.

1 Answer 1

2

You can pack the Text widget in a Frame with fixed width and height and pack_propogate(0):

from tkinter import *
from tkinter import font

root = Tk()

fontsize = 16

def increase_font():
    global fontsize
    fontsize += 2
    textfont.config(size=fontsize)

# frame for the text widget
container = Frame(root, width=600, height=400)
container.pack(fill="both", expand=1)
container.pack_propagate(0)

textfont = font.Font(family = "consolas" , size = fontsize)
# set the width and height of the Text widget to smallest values
# and let the layout manager to expand it
my_text = Text(container , width=1 , height=1 , font=textfont)
my_text.pack(fill="both", expand=1) # fill the parent frame
my_text.insert(1.0 , "There is a problem")

my_button = Button(root , text="Increase Font" , width=13 , font="arial 11" , command=increase_font)
my_button.pack(pady=5)

mainloop()

Update: Using grid() on the frame and button:

from tkinter import *
from tkinter import font

root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

fontsize = 16

def increase_font():
    global fontsize
    fontsize += 2
    textfont.config(size=fontsize)

# frame for the text widget
container = Frame(root, width=600, height=400)
container.grid(row=0, column=0, sticky="nsew", padx=5, pady=5)
container.pack_propagate(0)

textfont = font.Font(family = "consolas" , size = fontsize)
# set the width and height of the Text widget to smallest values
# and let the layout manager to expand it
my_text = Text(container , width=1 , height=1 , font=textfont)
my_text.pack(fill="both", expand=1) # fill the parent frame
my_text.insert(1.0 , "There is a problem")

my_button = Button(root , text="Increase Font" , width=13 , font="arial 11" , command=increase_font)
my_button.grid(row=1, column=0, pady=5)

mainloop()
Sign up to request clarification or add additional context in comments.

1 Comment

You can use grid() on the frame and button.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.