(1400, 'GetClassName', 'Invalid window handle.') 使用 swspotify 时

(1400, 'GetClassName', 'Invalid window handle.') when using swspotify

我是一个目前正在播放的东西,但问题是我试图每隔几秒更新一次当前正在播放的歌曲,但它总是说'(1400, 'GetClassName', 'Invalid window handle.')'它更新

from SwSpotify import spotify
from plyer import notification
import time
from watchpoints import watch
song = spotify.song()
artist = spotify.artist()
message = ''
message_check = message
loop = True
title = 'Currently Playing'
def notif():
    message= '%s by %s' % (song,artist)
    notification.notify(title= title,message= message,app_icon = None,timeout= 10,toast=False)
    print('notif sent')
while True:
    song = spotify.song()
    artist = spotify.artist()
    print (message + '1')
    print (message_check + '2')
    message= '%s by %s' % (song,artist)
    if message != message_check:
        notif()
        message_check = message
        continue
    else:
        time.sleep(1)
        continue

SwSpotify 在使用 win32gui.enumWindows 时未捕获异常。您可以在调用周围添加自己的异常处理。

您也可以使用 spotify.current() 同时获取歌曲和艺术家。

我也看不出有任何理由在循环之前获取歌曲和艺术家,因为循环会立即覆盖它们。

from SwSpotify import spotify
from plyer import notification
import time
from watchpoints import watch

message = ''
message_check = message
loop = True
title = 'Currently Playing'
def notif():
    message= '%s by %s' % (song,artist)
    notification.notify(title= title,message= message,app_icon = None,timeout= 10,toast=False)
    print('notif sent')
while True:
    while True:
        try:
            song, artist = spotify.current()
        except:
            time.sleep(1)
    print (message + '1')
    print (message_check + '2')
    message= '%s by %s' % (song,artist)
    if message != message_check:
        notif()
        message_check = message
        continue
    else:
        time.sleep(1)
        continue

原来是通知超时导致请求失败我更改了通知扩展并且一切正常

from SwSpotify import spotify
from SwSpotify import *
from notify import notification
song = ''
artist = ''
import time
message_check = ''
title = 'Currently Playing On Spotify'
while True:
    try:
        song, artist = spotify.current()
        af = '%s by %s' % (song,artist)
        print(af)
        if message_check != af:
            message_check = af
            notification(af, title='Currently Playing')
            continue
        else:
            time.sleep(0.3)
            continue
        time.sleep(5)       
    except SpotifyNotRunning as e:
        print(e)