tkinter - Changing the fontsize for the window and its widgets ?

Terry Reedy tjreedy at udel.edu
Tue Jan 21 13:23:13 EST 2020


On 1/21/2020 7:03 AM, R.Wieser wrote:
> Hello all,
> 
> I've got a small tkinter based program showing (among others) a treeview.
> I would like to set the default font for the whole window to something
> larger, as its supposed to be displayed on a TV a few meters away from the
> user.

A physically large display should have physically larger pixels, but see 
below.

> A few problems:
> 
> 1) I have not been able to find any info on how to set the default font for
> the main window (so its caption as well as (I assume, automatically) all its
> placed widgets would than use the new font size)

AFAIK, Toplevel windows do not have a font setting.  Tk uses one of its 
default fonts for the window title and menu bar, which I believe are set 
from the system font settings.  So the system settings would need to be 
changed to get relatively larger window text.  Not easy, even if 
possible, from within a python program.

> 2) Although I've been able to find how to change the font size for both the
> heading and the items within the treeview, only the heading resizes
> vertically while the items do not (causing the font to be cut-of
> vertically).

The tkinter treeview doc is deficient here.  See Styling Options near 
the end of
https://www.tcl.tk/man/tcl8.6/TkCmd/ttk_treeview.htm

The widget font is set with the Treeview font style.
The heading font is set with the Heading font style.
Item fonts are set with item tag with font settings.
Row height is set with the Treeview rowheight style.
Equal height for all rows is a restriction.

"The -rowheight value is not corrected by the tk scaling value or by the 
configured font size and must always be set. ..."  I believe the tcl 
expression given translates to "font.metrics('linespace')+2".  Of 
course, use the linespace for the tallest font.

Searching "python tkinter treeview height" returns Q&As on Stackoverflow 
and other forums.

> Also, the horizontal size of both the the heading and items
> seem to stay the same (do not adjust to the new font size).

Yes, column widths are set in pixels and are only auto-adjusted by 
window resizings and separator movements.  If you allow dynamic font 
changes, you have to redo row height and column widths.

> Used example code:
> https://riptutorial.com/tkinter/example/31885/customize-a-treeview


-- 
Terry Jan Reedy



More information about the Python-list mailing list