Board logo

標題: python code win10 行到, win7行唔到 [打印本頁]

作者: bongbong3481    時間: 2023-12-27 22:51     標題: 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.
作者: 燕飛    時間: 2023-12-27 22:59

Python 幾?

via HKEPC Reader for Android
作者: wunit    時間: 2023-12-28 01:11

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

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


Win7要用GetScaleFactorForMonitor
作者: bongbong3481    時間: 2023-12-28 19:36

回覆 3# wunit

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

    https://learn.microsoft.com/en-u ... alefactorformonitor
作者: bongbong3481    時間: 2023-12-28 19:36

回覆 2# 燕飛


    Python 3.8
作者: wunit    時間: 2023-12-28 20:26

回覆  wunit

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

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


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

但 shcore 好似係Win 8野, Win 7可能要加DLL
作者: bongbong3481    時間: 2023-12-28 22:22

回覆 6# wunit


   似乎越搞越複雜,已經超出我的能力
作者: w7pinuser    時間: 2024-2-18 13:51

回覆  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
作者: w7pinuser    時間: 2024-2-18 13:52

以下程式碼支援Windows2000~11:




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



   
import ctypes
user32 = ctypes.windll.user32
作者: w7pinuser    時間: 2024-2-18 13:55

其實也可以用Resource Hacker直接修改python.exe、pythonw.exe的manifest,然後用PyInstaller+Python3.8封裝。
ScaleFactor = DPI / 96.0
作者: bongbong3481    時間: 2024-2-19 09:11

回覆 8# w7pinuser


    謝謝





歡迎光臨 電腦領域 HKEPC Hardware (https://h0.hkepc.com/forum/) Powered by Discuz! 7.2