使用pywebview操作,编写播放器

import webview
import tkinter as tk

# 创建一个Tkinter根窗口,但保持它不可见
root = tk.Tk()
root.withdraw()
listMap = []
w = None

# 获取屏幕的宽度和高度
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
def print_handler(e):
    obj = e['target']
    if(obj):
       # print(obj['innerHTML'])
       # print(obj['outerHTML'])
       # print("||||" + obj['outerHTML'] + "||||")
        listMap.insert(0, "<li>" + obj['outerHTML'] + "</li>")
        if(len(listMap)>20):
            listMap.pop()

        

def favorateHandler(e):
    obj = e['target']


def func_close():
    return True

 



def on_loaded(window):
    #print('Window is about to be shown')
    cssStr = """    
       
        #newPlayList {
        cursor: pointer;
        }
        #listUL{
        display: none;
        background-color:rgb(254, 249, 249);
        border-radius: 3px;
        min-height: 280px;
        }
        #newPlayList:hover #listUL{
        display: block;
        }
        #listUL:hover {
        color: rgb(95, 115, 216);
        }
        
    """
    window.load_css(cssStr)
    #print('DOM is ready')
    divElements = w.dom.get_elements('a[target="_mp3"][href$="html"][href^="/song"]')
    #print(len(divElements))
    for item in divElements:
        item.on('click', print_handler)

    
    body = window.dom.get_elements("body")
    if(body):
        str = ""
        for li in listMap:
            str = str + li

        favorateElement = w.dom.create_element('<div style=" position: absolute; z-index:999; top: 20px; left: 180px;  width: 80px; height: 20px; "id="newPlayList"  ><i style="color:red;font-size:large;"  title="最近播放列表" class="fed-icon-font fed-icon-zhuanti"></i><ul style=";position:absolute;left:-150px;width:300px;line-height:25px;padding:0px 0px 0px 10px;"id="listUL">' + str + '</ul></div>', parent= body[0] , mode=webview.dom.ManipulationMode.FirstChild) 
        favorateElement.on('click', favorateHandler)



if __name__ == '__main__':
    webview.settings = {
    'ALLOW_DOWNLOADS': False,
    'ALLOW_FILE_URLS': True,
    'OPEN_EXTERNAL_LINKS_IN_BROWSER': False,
    'OPEN_DEVTOOLS_IN_DEBUG': False
    }
    w = webview.create_window('爱听音乐网', 'http://music.2t58.com/',width=400,height=screen_height-50,x=screen_width-120,y=0,draggable=True,resizable=False)
    w.confirm_close = func_close
    w.events.loaded += on_loaded
  
    webview.start()
    

1、 使用了python pywebview和tkinter类库

2、 使用·pyinstaller --onefile --noconsole songplayer.py· 能够打包为exe。

3、效果如下

 

posted @ 2024-12-15 16:16  和平鸽  阅读(20)  评论(0编辑  收藏  举报