【问题标题】:(1400, 'GetClassName', 'Invalid window handle.') when using swspotify(1400, 'GetClassName', '无效的窗口句柄。') 使用 swspotify 时
【发布时间】:2021-04-22 15:46:17
【问题描述】:

我正在制作一个目前正在播放的歌曲,但问题是我试图每隔几秒钟更新一次当前播放的歌曲,但它总是说'(1400,'GetClassName','无效的窗口句柄。')'每当它更新

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

【问题讨论】:

  • 你不需要写continue,循环自动继续,除非while条件失败或者你使用breakcontinue 仅在您想在到达正文末尾之前开始下一次迭代时才需要。
  • 请发布完整的回溯。你的代码从不调用GetClassName(),所以不清楚发生在哪里。
  • @barmar 我使用 continue 导致循环失败并且没有它就不会重新运行
  • 发生异常:错误 (1400, 'GetClassName', 'Invalid window handle.') 文件“C:\Users\hyper\Documents\better spotify\gui.py”,第 17 行,在 艺术家 = spotify.artist()
  • 你想找哪首歌的艺术家?

标签: python python-3.x spotify


【解决方案1】:

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

【讨论】:

  • 由于某些原因,这不起作用没有错误代码,但它不起作用
  • 那么这个bug比我想象的还要严重。您应该将其报告给库开发人员。
【解决方案2】:

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

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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-24
    • 2013-09-07
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多