pygetwindow Error, AttributeError: 'list' object has no attribute 'topleft'

Question:

I am trying to get a screenshot of a specific window, but I get the following Error: AttributeError: ‘list’ object has no attribute ‘topleft’. How can I solve it ?

import pygetwindow
import pyautogui
from PIL import Image
import platform

path = 'report/result.png'
titles = pygetwindow .getAllTitles()

if platform.system() == 'Windows':
    window = pygetwindow.getWindowsWithTitle('bachelor-project-vision-based-traffic-scene-surveillance – report.py')
    #print(titles)
    left, top = window.topleft
    right, bottom = window.bottomright
    pyautogui.screenshot(path)
    im = Image.open(path)
    im = im.crop((left, top, right, bottom))
    im = im.save(path)
    im.show(path)

elif platform.system() == 'Darwin':
    x1, y1, width, height = pygetwindow .getWindowGeometry('Chrome')
    x2 = x1 + width
    y2 = y1 + height
    pyautogui.screenshot(path)
    im = Image.open(path)
    # im = im.crop((x1, y1, x2, y2+10))
    im.save(path)
Asked By: user20965296

||

Answers:

getWindowsWithTitle returns a list , try to access its member

window[0] and try to extract top and left from that

Answered By: ashish singh

Solved the Problem.

I should have taken the first element of the list:

window = pygetwindow.getWindowsWithTitle('bachelor-project-vision-based-traffic-scene-surveillance – report.py')[0]
Answered By: user20965296
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.