Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scaling of Tkinter GUI in 4k (3840*2160) resolution?

I use LinuxMint 17.3 Cinnamon in VirtualBox, 1920*1080 resolution is used in this machine, the Hi-DPI option is turned on. The host machine is Windows 10, with 3840*2160 resolution.
Despite turning on Hi-DPI option in LinuxMint, some applications become to look good for comfortable work, in terms of scaling, but python-tk GUI (python2) hasn't been changed - font size is tiny, changing of Font options in Cinnamon doesn't change fonts in tk. Is there any way to scale correctly already written tk GUI applications?

like image 647
alr Avatar asked Dec 07 '15 11:12

alr


1 Answers

tkinter has an internal scaling factor that it uses to convert measurements such as points and inches into pixels. You can set this with the "tk scaling" command. This command takes one argument, which is the number of pixels in one "point". A point is 1/72 of an inch, so a scaling factor of 1.0 is appropriate for a 72DPI display.

root = Tk()
root.tk.call('tk', 'scaling', 2.0)

According to a comment in a similar question, this won't affect the default fonts since they are defined outside the context of tkinter. If you specify your own fonts in points, they should honor this setting.

The official documentation for the scaling command is this:

Sets and queries the current scaling factor used by Tk to convert between physical units (for example, points, inches, or millimeters) and pixels. The number argument is a floating point number that specifies the number of pixels per point on window's display. If the window argument is omitted, it defaults to the main window. If the number argument is omitted, the current value of the scaling factor is returned.

A “point” is a unit of measurement equal to 1/72 inch. A scaling factor of 1.0 corresponds to 1 pixel per point, which is equivalent to a standard 72 dpi monitor. A scaling factor of 1.25 would mean 1.25 pixels per point, which is the setting for a 90 dpi monitor; setting the scaling factor to 1.25 on a 72 dpi monitor would cause everything in the application to be displayed 1.25 times as large as normal. The initial value for the scaling factor is set when the application starts, based on properties of the installed monitor, but it can be changed at any time. Measurements made after the scaling factor is changed will use the new scaling factor, but it is undefined whether existing widgets will resize themselves dynamically to accommodate the new scaling factor.

like image 147
Bryan Oakley Avatar answered Oct 15 '22 17:10

Bryan Oakley