din*_*les 6 tkinter python-3.x windows-10
经过几个小时的调整后,我已经确定了这个代码,这使我能够在Python 3中使用Tkinter接口时,在高DPI显示器上解决Windows 10中模糊/模糊文本的熟悉问题.
我不想设置兼容性标志或期望其他人这样做我发现通过DLL调用标记DPI意识'on'然后检索DPI设置然后我可以扩展GUI窗口和框架内.
然而,在将这个传递给其他人之前,我想检查我的方法是将'GUI'(一个tkinter.Tk()实例)传递给主体中的MakeTkDPIAware函数并获取该函数以向其添加自定义属性是一种健康的方法选择或导致tkinter实例出现问题的风险.然后添加的属性可以在主体中使用,但是可以安全地假设总会发生这种情况吗?
我已经能够发现这种做法是否是一个已知的做法 - 如果它是不赞成的或是一个糟糕的设计选择.(通常在Python中,我可以非常兴奋地得到一些工作,我当时忘记检查这类问题),所以我希望有人可以提供建议.它似乎是"记住"缩放数据的最整洁方式,而不是创建新的全局变量.
如果另一种解决方案更像Pythonic,我会非常感兴趣.
import re
def Get_HWND_DPI(window_handle):
#To detect high DPI displays and avoid need to set Windows compatibility flags
import os
if os.name == "nt":
from ctypes import windll, pointer, wintypes
try:
windll.shcore.SetProcessDpiAwareness(1)
except Exception:
pass # this will fail on Windows Server and maybe early Windows
DPI100pc = 96 # DPI 96 is 100% scaling
DPI_type = 0 # MDT_EFFECTIVE_DPI = 0, MDT_ANGULAR_DPI = 1, MDT_RAW_DPI = 2
winH = wintypes.HWND(window_handle)
monitorhandle = windll.user32.MonitorFromWindow(winH, wintypes.DWORD(2)) # MONITOR_DEFAULTTONEAREST = 2
X = wintypes.UINT()
Y = wintypes.UINT()
try:
windll.shcore.GetDpiForMonitor(monitorhandle, DPI_type, pointer(X), pointer(Y))
return X.value, Y.value, (X.value + Y.value) / (2 * DPI100pc)
except Exception:
return 96, 96, 1 # Assume standard Windows DPI & scaling
else:
return None, None, 1 # What to do for other OSs?
def TkGeometryScale(s, cvtfunc):
patt = r"(?P<W>\d+)x(?P<H>\d+)\+(?P<X>\d+)\+(?P<Y>\d+)" # format "WxH+X+Y"
R = re.compile(patt).search(s)
G = str(cvtfunc(R.group("W"))) + "x"
G += str(cvtfunc(R.group("H"))) + "+"
G += str(cvtfunc(R.group("X"))) + "+"
G += str(cvtfunc(R.group("Y")))
return G
def MakeTkDPIAware(TKGUI):
TKGUI.DPI_X, TKGUI.DPI_Y, TKGUI.DPI_scaling = Get_HWND_DPI(TKGUI.winfo_id())
TKGUI.TkScale = lambda v: int(float(v) * TKGUI.DPI_scaling)
TKGUI.TkGeometryScale = lambda s: TkGeometryScale(s, TKGUI.TkScale)
#Example use:
import tkinter
GUI = tkinter.Tk()
MakeTkDPIAware(GUI) # Sets the windows flag + gets adds .DPI_scaling property
GUI.geometry(GUI.TkGeometryScale("600x200+200+100"))
gray = "#cccccc"
DemoFrame = tkinter.Frame(GUI, width=GUI.TkScale(580), height=GUI.TkScale(180), background=gray)
DemoFrame.place(x=GUI.TkScale(10), y=GUI.TkScale(10))
DemoFrame.pack_propagate(False)
LabelText = "Scale = " + str(GUI.DPI_scaling)
DemoLabel = tkinter.Label(DemoFrame, text=LabelText, width=10, height=1)
DemoLabel.pack(pady=GUI.TkScale(70))
Run Code Online (Sandbox Code Playgroud)
bin*_*unt 11
不能完全回答您的问题,但是如果您想在 Windows 10(秋季创作者更新)的高 dpi 显示器上修复 tkinter 窗口的模糊性,而无需插入 Python 代码行:
寻找你的python.exe和pythonw.exe可执行文件
右键单击python.exe并单击“属性”
单击“兼容性”选项卡
单击“更改高 DPI 设置”按钮
勾选“覆盖高 DPI 缩放行为”并在下拉菜单中选择“应用程序”
pythonw.exe如有必要,重复
Jay*_*Jay 10
简单地使用windll.shcore.SetProcessDpiAwareness(1)和允许字体大小调整解决了我在Windows 10上的问题.
不知道为什么没有人评论或协助.模糊的字体是一种痛苦,我希望这个线程有一个争论和这个Tkinter讨厌的最佳解决方案..
| 归档时间: |
|
| 查看次数: |
2444 次 |
| 最近记录: |