How to scale the text with the rest of the widget, when rescaling a widget with Python tkinter? How to scale the text with the rest of the widget, when rescaling a widget with Python tkinter? tkinter tkinter

How to scale the text with the rest of the widget, when rescaling a widget with Python tkinter?


Yes, it is possible to scale the text, but it does take a little bit of work. I had to do something very similar, and the way I did it was via font size. When you create a Text widget, by default the Text widget gets a default font, 'TkFixedFont'. That named font is essentially 'Courier 10' or so. You can manually set the Text font via Text.config(font=...). The trick is to change the size of the font according to the relative size of the Text widget. So, you would use some sort of scaling function to translate the size of the Text widget to the size of the font you want, then update the font of the Text widget with your new font based on the font size that you calculated. Do this inside the '<Configure>' event handler which you define, which gets invoked when the Text widget (or whole app) gets resized.

In my app, I have buttons for zooming in and out, and I do this by changing the font size as described above. Works like a charm.

Rereading your post, I see that you're looking to change the text of some Labels. Create a new named font and assign it as the font for all your Labels. Then, in your '<Configure>' handler, just update the named font with the new font size, and it will propagate to all the widgets that use that named font. This goes for Labels, Buttons, Text widgets, etc.

Also, as a side note, as you scale the text on the Labels and other widgets, those widgets will adjust in size to the text they are displaying.