3

First attempt. Here you can see how the font changes size and The text widget changes size. I need the text widget to keep its size. I tried to create a text widget in a frame and make frm.grid_propagate (False). the result you see in the second attempt. In the second attempt, the widget could now only decrease, it could not increase. But when the font size is large the text is printed outside the text widget border and it is not visible

from tkinter import*
import tkinter as tk
def fontUp():
    global count
    if text.tag_ranges('sel'):
        text.tag_add('colortag_' + str(count), SEL_FIRST,SEL_LAST)
        text.tag_configure('colortag_' + str(count),font='Area 35')
        count+=1
    else:    
        text.configure(font='Area 30')

count=0    
root = tk.Tk()
root.geometry("800x800")

frm = tk.Frame(root, height = 300, width = 500)
frm.grid(row = 0, column = 0)
frm.grid_propagate(False)

text = tk.Text(frm, height = 20, width = 50)
text.grid(row = 0, column = 0)

btn = tk.Button(root, text="Font bigger", command = fontUp)
btn.grid(row = 2, column = 0)

This code has the same error as my second attempt.

When we clicked on button size text increases and the widget also increases. But I have a 'fontUp' function that only increases the selected text.the function works like this: first select the text then click on the button and the text increases but the widget does not increase is what I need. How to make that the text changes its size, and the widget does not change its size

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
A.Kross
  • 165
  • 8

1 Answers1

2

For me, the best solution is to set the widget size to 0 or 1, and then let the geometry management cause the window to grow or shrink to fit a containing frame. This works because the font change only affects the desired size of the window rather than the actual size, and the desired size is based on the width and height. The actual size is controlled by the containing frame.

In the following example I'm using pack for the text widget. It works with grid too, but requires an extra couple of lines of code to give the row and column weight. Since the text widget is the only widget in the containing frame, pack is easier to use.

frm = tk.Frame(root, height = 300, width = 500)
frm.grid(row = 0, column = 0)
frm.pack_propagate(False)

text = tk.Text(frm, height = 0, width = 0)
text.pack(fill="both", expand=True)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685