python - 以 4k (3840*2160) 分辨率缩放 Tkinter GUI?

标签 python python-2.7 tkinter tk-toolkit

我在VirtualBox中使用LinuxMint 17.3 Cinnamon, native 使用1920*1080分辨率,开启Hi-DPI选项。宿主机为Windows 10,分辨率为3840*2160。
尽管在 LinuxMint 中打开了 Hi-DPI 选项,一些应用程序在缩放方面看起来很适合舒适的工作,但是 python-tk GUI (python2) 没有改变——字体很小,改变了 Cinnamon 中的字体选项不会更改 tk 中的字体。有什么方法可以正确扩展已经编写的 tk GUI 应用程序吗?

最佳答案

tkinter 有一个内部比例因子,用于将点和英寸等测量值转换为像素。您可以使用“tk scaling”命令进行设置。该命令接受一个参数,即一个“点”中的像素数。一个点是 1/72 英寸,因此 1.0 的比例因子适用于 72DPI 显示器。

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

根据 a similar question 中的评论,这不会影响默认字体,因为它们是在 tkinter 的上下文之外定义的。如果您以磅为单位指定自己的字体,它们应该遵守此设置。

official documentation 缩放命令是这样的:

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.

关于python - 以 4k (3840*2160) 分辨率缩放 Tkinter GUI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34132203/

相关文章:

python - 从 stdout 或 stderr 获取子进程的消息

python - 未找到 'pip==7.1.0' 分布,应用程序需要该分布

python - 如何在一个GUI中显示两个或更多视频流

python - 去掉选项菜单周围的白色边框

python - __init__ 是类方法吗?

python - 相同的指令,不同的结果

python - Pandas :noauth_local_webserver

python - 如何从输出中删除包含非字母字符的行?

python - 从 tkinter 创建新的 ttk 小部件

python - 从 python 中**快速**截取屏幕截图