python code win10 行到, win7行唔到

好奇怪, 下面呢句python code win10 行到, win7行唔到, 請問咩原因?

ratio = ctypes.windll.shcore.GetScaleFactorForDevice(0) / 100

=============
import pyautogui
import time
import keyboard
import ctypes, win32gui, win32ui
from PIL import Image, ImageGrab

ratio = ctypes.windll.shcore.GetScaleFactorForDevice(0) / 100

=============


Traceback (most recent call last):
  File "test.py", line 8, in <module>
    ratio = ctypes.windll.shcore.GetScaleFactorForDevice(0) / 100
  File "C:\Users\qqq\AppData\Local\Programs\Python\Python38\lib\ctypes\__init__.p
y", line 443, in __getattr__
    dll = self._dlltype(name)
  File "C:\Users\qqq\AppData\Local\Programs\Python\Python38\lib\ctypes\__init__.p
y", line 373, in __init__
    self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'shcore' (or one of its dependencies).
Try using the full path with constructor syntax.

Python 幾?

via HKEPC Reader for Android

TOP

好奇怪, 下面呢句python code win10 行到, win7行唔到, 請問咩原因?

ratio = ctypes.windll.shcore.GetSca ...
bongbong3481 發表於 2023-12-27 22:51


Win7要用GetScaleFactorForMonitor

TOP

回覆 3# wunit

呢句GetScaleFactorForMonitor,睇網上資料,好似win8 或以上先用到

    https://learn.microsoft.com/en-u ... alefactorformonitor

TOP

回覆 2# 燕飛


    Python 3.8

TOP

回覆  wunit

呢句GetScaleFactorForMonitor,睇網上資料,好似win8 或以上先用到

     ...
bongbong3481 發表於 2023-12-28 19:36


傻左添......一時記錯反轉左

但 shcore 好似係Win 8野, Win 7可能要加DLL

TOP

回覆 6# wunit


   似乎越搞越複雜,已經超出我的能力

TOP

回覆  wunit


   似乎越搞越複雜,已經超出我的能力
bongbong3481 發表於 2023-12-28 22:22



    以下程式碼支援Windows2000~11:




def getdpiforwindow(hwnd):
    n = 96
    try:
        #>=Win8.1
        nx, ny = [ctypes.c_ulong()] * 2
        monitor_h = user32.MonitorFromWindow(hwnd, 2)
        ctypes.windll.shcore.GetDpiForMonitor(monitor_h, 0, ctypes.byref(nx), ctypes.byref(ny))
        n = nx.value
    except:
        #<Win8.1
        aware = user32.IsProcessDPIAware() if hasattr(user32, 'IsProcessDPIAware') else True
        if aware:
            hDC = user32.GetDC(None)
            n = ctypes.windll.gdi32.GetDeviceCaps(hDC, 88)
            user32.ReleaseDC(None, hDC)
    return n

TOP

以下程式碼支援Windows2000~11:




def getdpiforwindow(hwnd):
    n = 96
    try:
        #>= ...
w7pinuser 發表於 2024-2-18 13:51



   
import ctypes
user32 = ctypes.windll.user32

TOP

其實也可以用Resource Hacker直接修改python.exe、pythonw.exe的manifest,然後用PyInstaller+Python3.8封裝。
ScaleFactor = DPI / 96.0

TOP