{"id":1140232,"date":"2025-01-08T22:18:20","date_gmt":"2025-01-08T14:18:20","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1140232.html"},"modified":"2025-01-08T22:18:22","modified_gmt":"2025-01-08T14:18:22","slug":"%e5%a6%82%e4%bd%95%e7%94%a8python%e8%8e%b7%e5%be%97%e4%b8%80%e4%b8%aa%e7%aa%97%e5%8f%a3%e7%9a%84%e7%b1%bb%e5%90%8d","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1140232.html","title":{"rendered":"\u5982\u4f55\u7528python\u83b7\u5f97\u4e00\u4e2a\u7a97\u53e3\u7684\u7c7b\u540d"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103316\/85e01b70-d7e1-4427-b27c-33645bc1b633.webp\" alt=\"\u5982\u4f55\u7528python\u83b7\u5f97\u4e00\u4e2a\u7a97\u53e3\u7684\u7c7b\u540d\" \/><\/p>\n<p><p> <strong>\u8981\u7528Python\u83b7\u5f97\u4e00\u4e2a\u7a97\u53e3\u7684\u7c7b\u540d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528Python\u7684ctypes\u5e93\u3001\u4f7f\u7528PyWin32\u5e93\u3001\u5229\u7528\u7b2c\u4e09\u65b9\u5e93\u5982PyGetWindow<\/strong>\u3002\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u901a\u8fc7ctypes\u5e93\u76f4\u63a5\u8c03\u7528Windows API\u51fd\u6570\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7075\u6d3b\uff0c\u8fd8\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95ee\u8bb8\u591a\u5e95\u5c42\u7cfb\u7edf\u529f\u80fd\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528ctypes\u5e93\u6765\u83b7\u5f97\u4e00\u4e2a\u7a97\u53e3\u7684\u7c7b\u540d\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u4f7f\u7528ctypes\u5e93<\/h2>\n<\/p>\n<p><h3>1\u3001\u51c6\u5907\u5de5\u4f5c<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u4e86\u89e3\u4e00\u4e9bWindows API\u7684\u57fa\u672c\u77e5\u8bc6\u3002Windows API\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u6765\u4e0e\u64cd\u4f5c\u7cfb\u7edf\u8fdb\u884c\u4ea4\u4e92\u3002\u4e3a\u4e86\u83b7\u5f97\u7a97\u53e3\u7684\u7c7b\u540d\uff0c\u6211\u4eec\u4e3b\u8981\u9700\u8981\u4f7f\u7528\u4ee5\u4e0b\u51e0\u4e2aAPI\u51fd\u6570\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>FindWindow<\/code>: \u67e5\u627e\u7a97\u53e3\u53e5\u67c4\u3002<\/li>\n<li><code>GetClassName<\/code>: \u83b7\u53d6\u7a97\u53e3\u7c7b\u540d\u3002<\/li>\n<li><code>GetDesktopWindow<\/code>: \u83b7\u53d6\u684c\u9762\u7a97\u53e3\u53e5\u67c4\u3002<\/li>\n<\/ul>\n<p><h3>2\u3001\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528ctypes\u5e93\u83b7\u53d6\u7a97\u53e3\u7c7b\u540d\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import ctypes<\/p>\n<p>import ctypes.wintypes<\/p>\n<h2><strong>\u5b9a\u4e49\u5e38\u91cf<\/strong><\/h2>\n<p>MAX_CLASS_NAME = 256<\/p>\n<h2><strong>\u52a0\u8f7d\u7528\u623732\u5e93<\/strong><\/h2>\n<p>user32 = ctypes.windll.user32<\/p>\n<h2><strong>\u83b7\u53d6\u7a97\u53e3\u53e5\u67c4<\/strong><\/h2>\n<p>hwnd = user32.FindWindowW(None, &quot;\u7a97\u53e3\u6807\u9898&quot;)<\/p>\n<p>if hwnd:<\/p>\n<p>    # \u521b\u5efa\u7f13\u51b2\u533a<\/p>\n<p>    class_name = ctypes.create_unicode_buffer(MAX_CLASS_NAME)<\/p>\n<p>    # \u83b7\u53d6\u7a97\u53e3\u7c7b\u540d<\/p>\n<p>    user32.GetClassNameW(hwnd, class_name, MAX_CLASS_NAME)<\/p>\n<p>    print(&quot;\u7a97\u53e3\u7c7b\u540d:&quot;, class_name.value)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u627e\u4e0d\u5230\u6307\u5b9a\u7a97\u53e3&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4ee3\u7801\u5206\u6790<\/h3>\n<\/p>\n<ul>\n<li><strong>FindWindow<\/strong>: \u901a\u8fc7\u7a97\u53e3\u6807\u9898\u67e5\u627e\u7a97\u53e3\u53e5\u67c4\u3002\u5982\u679c\u7a97\u53e3\u6807\u9898\u662f\u552f\u4e00\u7684\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u6b64\u51fd\u6570\u3002<\/li>\n<li><strong>GetClassName<\/strong>: \u83b7\u53d6\u7a97\u53e3\u7c7b\u540d\u3002\u9700\u8981\u4f20\u5165\u7a97\u53e3\u53e5\u67c4\u548c\u4e00\u4e2a\u7f13\u51b2\u533a\u6765\u5b58\u50a8\u7c7b\u540d\u3002<\/li>\n<li><strong>create_unicode_buffer<\/strong>: \u521b\u5efa\u4e00\u4e2a\u7f13\u51b2\u533a\uff0c\u7528\u6765\u5b58\u50a8\u7c7b\u540d\u3002<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001\u4f7f\u7528PyWin32\u5e93<\/h2>\n<\/p>\n<p><p>PyWin32\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u5bf9Windows API\u7684\u8bbf\u95ee\u3002\u4f60\u53ef\u4ee5\u901a\u8fc7\u5b83\u6765\u7b80\u5316\u8bb8\u591a\u590d\u6742\u7684\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5PyWin32<\/h3>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f60\u9700\u8981\u5b89\u88c5PyWin32\u5e93\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install pywin32<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528PyWin32\u5e93\u83b7\u53d6\u7a97\u53e3\u7c7b\u540d\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import win32gui<\/p>\n<p>def get_window_class_name(title):<\/p>\n<p>    hwnd = win32gui.FindWindow(None, title)<\/p>\n<p>    if hwnd:<\/p>\n<p>        class_name = win32gui.GetClassName(hwnd)<\/p>\n<p>        return class_name<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<p>window_title = &quot;\u7a97\u53e3\u6807\u9898&quot;<\/p>\n<p>class_name = get_window_class_name(window_title)<\/p>\n<p>if class_name:<\/p>\n<p>    print(f&quot;\u7a97\u53e3\u7c7b\u540d: {class_name}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u627e\u4e0d\u5230\u6307\u5b9a\u7a97\u53e3&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4ee3\u7801\u5206\u6790<\/h3>\n<\/p>\n<ul>\n<li><strong>FindWindow<\/strong>: \u4e0ectypes\u5e93\u4e2d\u7684\u529f\u80fd\u76f8\u540c\uff0c\u7528\u6765\u67e5\u627e\u7a97\u53e3\u53e5\u67c4\u3002<\/li>\n<li><strong>GetClassName<\/strong>: \u76f4\u63a5\u8c03\u7528API\u51fd\u6570\uff0c\u7b80\u5316\u4e86\u4ee3\u7801\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<p><h2>\u4e09\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93PyGetWindow<\/h2>\n<\/p>\n<p><p>PyGetWindow\u662f\u4e00\u4e2a\u66f4\u9ad8\u7ea7\u522b\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u4fbf\u7684\u65b9\u6cd5\u6765\u64cd\u4f5c\u7a97\u53e3\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u5b89\u88c5PyGetWindow<\/h3>\n<\/p>\n<p><p>\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u547d\u4ee4\u6765\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-bash\">pip install PyGetWindow<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u4ee3\u7801\u5b9e\u73b0<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0b\u662f\u4f7f\u7528PyGetWindow\u5e93\u83b7\u53d6\u7a97\u53e3\u7c7b\u540d\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pygetwindow as gw<\/p>\n<p>def get_window_class_name(title):<\/p>\n<p>    window = gw.getWindowsWithTitle(title)<\/p>\n<p>    if window:<\/p>\n<p>        class_name = window[0].getClassName()<\/p>\n<p>        return class_name<\/p>\n<p>    else:<\/p>\n<p>        return None<\/p>\n<p>window_title = &quot;\u7a97\u53e3\u6807\u9898&quot;<\/p>\n<p>class_name = get_window_class_name(window_title)<\/p>\n<p>if class_name:<\/p>\n<p>    print(f&quot;\u7a97\u53e3\u7c7b\u540d: {class_name}&quot;)<\/p>\n<p>else:<\/p>\n<p>    print(&quot;\u627e\u4e0d\u5230\u6307\u5b9a\u7a97\u53e3&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4ee3\u7801\u5206\u6790<\/h3>\n<\/p>\n<ul>\n<li><strong>getWindowsWithTitle<\/strong>: \u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5339\u914d\u7a97\u53e3\u7684\u5217\u8868\u3002<\/li>\n<li><strong>getClassName<\/strong>: \u83b7\u53d6\u7a97\u53e3\u7c7b\u540d\u3002<\/li>\n<\/ul>\n<p><h2>\u56db\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u4e09\u79cd\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u5728Python\u4e2d\u8f7b\u677e\u83b7\u53d6\u7a97\u53e3\u7684\u7c7b\u540d\u3002<strong>\u4f7f\u7528ctypes\u5e93\u7684\u65b9\u6cd5\u6700\u4e3a\u7075\u6d3b\uff0c\u9002\u5408\u9700\u8981\u8bbf\u95ee\u5e95\u5c42\u7cfb\u7edf\u529f\u80fd\u7684\u573a\u666f<\/strong>\u3002<strong>PyWin32\u5e93\u63d0\u4f9b\u4e86\u5bf9Windows API\u7684\u7b80\u5316\u8bbf\u95ee\uff0c\u9002\u5408\u5927\u591a\u6570\u5e94\u7528\u573a\u666f<\/strong>\u3002<strong>PyGetWindow\u5219\u63d0\u4f9b\u4e86\u66f4\u9ad8\u7ea7\u522b\u7684\u63a5\u53e3\uff0c\u9002\u5408\u5feb\u901f\u5f00\u53d1\u548c\u8c03\u8bd5<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4f60\u9700\u8981\u66f4\u591a\u5e95\u5c42\u64cd\u4f5c\uff0c\u53ef\u4ee5\u9009\u62e9ctypes\u5e93\uff1b\u5982\u679c\u4f60\u9700\u8981\u5feb\u901f\u5b9e\u73b0\u529f\u80fd\uff0c\u53ef\u4ee5\u9009\u62e9PyWin32\u6216PyGetWindow\u5e93\u3002<\/p>\n<\/p>\n<p><p>\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u9700\u8981\u5bf9Windows API\u6709\u4e00\u5b9a\u7684\u4e86\u89e3\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u7406\u89e3\u548c\u4f7f\u7528\u8fd9\u4e9b\u5e93\u3002\u5e0c\u671b\u672c\u6587\u80fd\u591f\u5e2e\u52a9\u4f60\u5728Python\u4e2d\u66f4\u597d\u5730\u83b7\u53d6\u7a97\u53e3\u7684\u7c7b\u540d\uff0c\u5e76\u4e3a\u4f60\u7684\u5f00\u53d1\u5de5\u4f5c\u63d0\u4f9b\u6709\u529b\u7684\u652f\u6301\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u83b7\u53d6\u7a97\u53e3\u7684\u7c7b\u540d\uff1f<\/strong><br \/>\u8981\u83b7\u53d6\u7a97\u53e3\u7684\u7c7b\u540d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pywin32<\/code>\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9Windows API\u7684\u8bbf\u95ee\u3002\u53ef\u4ee5\u901a\u8fc7\u7a97\u53e3\u53e5\u67c4\u83b7\u53d6\u7a97\u53e3\u7684\u7c7b\u540d\u3002\u9996\u5148\u786e\u4fdd\u5b89\u88c5\u4e86\u8be5\u5e93\uff0c\u7136\u540e\u4f7f\u7528<code>win32gui<\/code>\u6a21\u5757\u4e2d\u7684<code>GetClassName<\/code>\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u6211\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9bPython\u5e93\u6765\u83b7\u53d6\u7a97\u53e3\u7684\u7c7b\u540d\uff1f<\/strong><br \/>\u5e38\u7528\u7684\u5e93\u5305\u62ec<code>pywin32<\/code>\u548c<code>pygetwindow<\/code>\u3002<code>pywin32<\/code>\u63d0\u4f9b\u5bf9Windows API\u7684\u76f4\u63a5\u8bbf\u95ee\uff0c\u800c<code>pygetwindow<\/code>\u5219\u662f\u4e00\u4e2a\u66f4\u9ad8\u5c42\u6b21\u7684\u5c01\u88c5\uff0c\u9002\u5408\u66f4\u7b80\u5355\u7684\u7a97\u53e3\u7ba1\u7406\u9700\u6c42\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u5e93\u3002<\/p>\n<p><strong>\u83b7\u53d6\u7a97\u53e3\u7c7b\u540d\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u83b7\u53d6\u7a97\u53e3\u7c7b\u540d\u65f6\uff0c\u786e\u4fdd\u7a97\u53e3\u5904\u4e8e\u6d3b\u52a8\u72b6\u6001\u6216\u53ef\u89c1\u72b6\u6001\u3002\u540c\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u5904\u7406\u4e0d\u540c\u6743\u9650\u4e0b\u7684\u7a97\u53e3\uff0c\u786e\u4fddPython\u811a\u672c\u6709\u8db3\u591f\u7684\u6743\u9650\u6765\u8bbf\u95ee\u76ee\u6807\u7a97\u53e3\u7684\u4fe1\u606f\u3002\u6d4b\u8bd5\u548c\u8c03\u8bd5\u65f6\uff0c\u6700\u597d\u5728\u7ba1\u7406\u5458\u6a21\u5f0f\u4e0b\u8fd0\u884c\u811a\u672c\uff0c\u4ee5\u907f\u514d\u6743\u9650\u95ee\u9898\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u83b7\u53d6\u7a97\u53e3\u7c7b\u540d\u65f6\u53ef\u80fd\u9047\u5230\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u83b7\u53d6\u7a97\u53e3\u7c7b\u540d\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u627e\u4e0d\u5230\u7a97\u53e3\u53e5\u67c4\u6216\u6743\u9650\u4e0d\u8db3\u7684\u9519\u8bef\u3002\u5efa\u8bae\u6dfb\u52a0\u5f02\u5e38\u5904\u7406\u673a\u5236\uff0c\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u6349\u8fd9\u4e9b\u9519\u8bef\uff0c\u5e76\u7ed9\u51fa\u53cb\u597d\u7684\u63d0\u793a\u4fe1\u606f\u3002\u6b64\u5916\uff0c\u68c0\u67e5\u76ee\u6807\u7a97\u53e3\u662f\u5426\u5b58\u5728\u4e5f\u662f\u4e00\u4e2a\u826f\u597d\u7684\u4e60\u60ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u7528Python\u83b7\u5f97\u4e00\u4e2a\u7a97\u53e3\u7684\u7c7b\u540d\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528Python\u7684ctypes\u5e93\u3001\u4f7f\u7528PyWin3 [&hellip;]","protected":false},"author":3,"featured_media":1140240,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140232"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1140232"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140232\/revisions"}],"predecessor-version":[{"id":1140243,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1140232\/revisions\/1140243"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1140240"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1140232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1140232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1140232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}