Python 调整当前windows应用程序的窗口大小

Python 调整当前windows应用程序的窗口大小,python,Python,嗨,我只是想自定义跑步窗口的大小, 但是,运行窗口具有“静态”大小,无法使用鼠标调整大小 使用python可以解决这个问题吗 我在这里搜索主题时尝试过使用此选项 导入win32gui hwnd=win32gui.FindWindow(无,“窗口标题”)x0,y0,x1,y1=win32gui.GetWindowRect(hwnd)w=x1-x0h=y1-y0 win32gui.MoveWindow(hwnd,x0,y0,w+100,h+100,True) *但是,我收到了这个特殊的错误“pywi

嗨,我只是想自定义跑步窗口的大小, 但是,运行窗口具有“静态”大小,无法使用鼠标调整大小 使用python可以解决这个问题吗

我在这里搜索主题时尝试过使用此选项
导入win32gui hwnd=win32gui.FindWindow(无,“窗口标题”)x0,y0,x1,y1=win32gui.GetWindowRect(hwnd)w=x1-x0h=y1-y0 win32gui.MoveWindow(hwnd,x0,y0,w+100,h+100,True)


*但是,我收到了这个特殊的错误“pywintypes.error:(5,“MoveWindow”,“访问被拒绝”)”

要从Python访问窗口,请使用以下步骤

  • 使用
    win32gui.EnumWindows
    查找具有特定标题的窗口
  • 调用
    win32.Dispatch
    在桌面上设置焦点
  • 使用
    SendKeys('%')
    启动窗口搜索
  • 使用
    win32gui
    函数修改窗口属性
请尝试以下代码调整窗口的大小:

import win32com.client as win32
import win32gui

title = "Untitled - Notepad2"  # find first window with this title

def windowEnumerationHandler(hwnd, top_windows):
    top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
    
top_windows = []  # all open windows
win32gui.EnumWindows(windowEnumerationHandler, top_windows)

winlst = []  # windows to cycle through
for i in top_windows:  # all open windows
   if i[1] == title:
      winlst.append(i)

hwnd = winlst[0][0]  # first window with title, get hwnd id
shell = win32.Dispatch("WScript.Shell")  # set focus on desktop
shell.SendKeys('%')  # Alt key,  send key
rect = win32gui.GetWindowRect(hwnd) 
x0, y0, x1, y1 = win32gui.GetWindowRect(hwnd) 
w = x1 - x0 
h = y1 - y0 
win32gui.MoveWindow(hwnd, x0, y0, w+100, h+100, True)

谢谢,它工作得很好!虽然shell.sendkeys(“%”)是什么意思?我的意思是为什么我们必须发送alt键?我不清楚为什么需要alt键。我认为这是windows中的一个特殊键(如alt+tab)。另一个键也可以工作,但我总是看到在类似的情况下使用alt键。