欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中pywebview框架使用方法記錄

 更新時間:2024年09月27日 10:42:23   作者:monster?justin  
Pywebview是一個用于構(gòu)建網(wǎng)頁的Python庫,類似于Flask框架,但主要使用Python編寫而非HTML或JS,通過簡單的命令即可安裝和使用,支持創(chuàng)建自制或調(diào)用外部網(wǎng)頁界面,需要的朋友可以參考下

前言

pywebview是python的一個庫,類似于flask框架,這也是用來構(gòu)建網(wǎng)頁的軟件包,它的特點就是不用更多的和html語言和js語言,更多的使用python語言就可以完成網(wǎng)頁的創(chuàng)建和元素的監(jiān)聽該庫的簡介和示例,簡單使用看示例足夠,但是其中也不夠詳盡:https://pywebview.flowrl.com/

一、安裝pywebview

首先,確保已經(jīng)安裝了pywebview,可以通過以下命令進行安裝:

pip install pywebview

二、簡單使用

簡單的創(chuàng)建界面和一些基礎(chǔ)操作,這里不再贅述,也沒什么營養(yǎng),官方示例都已給出,這里只貼上一些示例:

一個示例界面;

import webview

if __name__ == '__main__':
    # Create a standard webview window
    window = webview.create_window('Simple browser', 'https://pywebview.flowrl.com/hello')
    webview.start()

一個自制界面:

import webview

html = """
  <html>
    <head></head>
    <body>
      <h2>Links</h2>

      <p><a >Regular links</a> are opened in the application window.</p>
      <p><a  target='_blank'>target='_blank' links</a> are opened in an external browser.</p>

    </body>
  </html>
"""

if __name__ == '__main__':
    window = webview.create_window('Link types', html=html)
    webview.start()

調(diào)用的是外部界面:

import webview

if __name__ == '__main__':
    window = webview.create_window(title='Webview App', url="https://hailuoai.com/?type=chat&chatID=251739240281759747/", confirm_close=True,
                                   zoomable=True, vibrancy=True, width=1275, height=745)
    webview.start(user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0',
                  debug=True)

 于是可以從這幾個例子看出來,pywebview主要有兩種,可以是自制的網(wǎng)頁html參數(shù),又或者是url的外部鏈接。

三、高級使用

主要是關(guān)于dom的元素創(chuàng)建選擇何使用:按鈕等等,關(guān)于bind函數(shù)

import random
import webview

rectangles = []

def random_color():
    red = random.randint(0, 255)
    green = random.randint(0, 255)
    blue = random.randint(0, 255)

    return f'rgb({red}, {green}, {blue})'

def bind(window):
    def toggle_disabled():
        disabled = None if len(rectangles) > 0 else True
        remove_button.attributes = { 'disabled': disabled }
        empty_button.attributes = { 'disabled': disabled }
        move_button.attributes = { 'disabled': disabled }

    def create_rectangle(_):
        color = random_color()
        rectangle = window.dom.create_element(f'<div class="rectangle" style="background-color: {color};"></div>', rectangle_container)
        rectangles.append(rectangle)
        toggle_disabled()

    def remove_rectangle(_):
        if len(rectangles) > 0:
            rectangles.pop().remove()
        toggle_disabled()

    def move_rectangle(_):
        if len(rectangle_container.children) > 0:
            rectangle_container.children[-1].move(circle_container)

    def empty_container(_):
        rectangle_container.empty()
        rectangles.clear()
        toggle_disabled()

    def change_color(_):
        circle.style['background-color'] = random_color()

    def toggle_class(_):
        circle.classes.toggle('circle')

    rectangle_container = window.dom.get_element('#rectangles')
    circle_container = window.dom.get_element('#circles')
    circle = window.dom.get_element('#circle')

    toggle_button = window.dom.get_element('#toggle-button')
    toggle_class_button = window.dom.get_element('#toggle-class-button')
    duplicate_button = window.dom.get_element('#duplicate-button')
    remove_button = window.dom.get_element('#remove-button')
    move_button = window.dom.get_element('#move-button')
    empty_button = window.dom.get_element('#empty-button')
    add_button = window.dom.get_element('#add-button')
    color_button = window.dom.get_element('#color-button')

    toggle_button.events.click += lambda e: circle.toggle()
    duplicate_button.events.click += lambda e: circle.copy()
    toggle_class_button.events.click += toggle_class
    remove_button.events.click += remove_rectangle
    move_button.events.click += move_rectangle
    empty_button.events.click += empty_container
    add_button.events.click += create_rectangle
    color_button.events.click += change_color

if __name__ == '__main__':
    window = webview.create_window(
        'DOM Manipulations Example', html='''
            <html>
                <head>
                <style>
                    button {
                        font-size: 100%;
                        padding: 0.5rem;
                        margin: 0.3rem;
                        text-transform: uppercase;
                    }

                    .rectangle {
                        width: 100px;
                        height: 100px;
                        display: flex;
                        justify-content: center;
                        align-items: center;
                        margin: 0.5rem;
                        border-radius: 5px;
                        background-color: red;
                    }

                    .circle {
                        border-radius: 50px;
                        background-color: red;
                    }

                    .circle:hover {
                        background-color: green;
                    }

                    .container {
                        display: flex;
                        flex-wrap: wrap;
                    }
                </style>
                </head>
                <body>
                    <button id="toggle-button">Toggle circle</button>
                    <button id="toggle-class-button">Toggle class</button>
                    <button id="color-button">Change color</button>
                    <button id="duplicate-button">Duplicate circle</button>
                    <button id="add-button">Add rectangle</button>
                    <button id="remove-button" disabled>Remove rectangle</button>
                    <button id="move-button" disabled>Move rectangle</button>
                    <button id="empty-button" disabled>Remove all</button>
                    <div id="circles" style="display: flex; flex-wrap: wrap;">
                        <div id="circle" class="rectangle circle"></div>
                    </div>

                    <div id="rectangles" class="container"></div>
                </body>
            </html>
        '''
    )
    webview.start(bind, window)

并且使用event可以監(jiān)聽按鈕等等的事件發(fā)生

并且是支持動態(tài)生成元素的:

with open(file_path, 'r', encoding='utf-8') as file:
    for line in file:
        count += 1
        window.dom.create_element(
            f"<input id='word{count}' writingsuggestions='true' type='text' class='editable-textbox'>111</input>"
        )
        temp = window.dom.get_element(f'#word{count}')
        line = line.rstrip('\n')
        temp.value = line

總結(jié)

到此這篇關(guān)于python中pywebview框架使用方法記錄的文章就介紹到這了,更多相關(guān)python pywebview框架使用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • PyQt5實現(xiàn)類似別踩白塊游戲

    PyQt5實現(xiàn)類似別踩白塊游戲

    這篇文章主要為大家詳細(xì)介紹了PyQt5實現(xiàn)類似別踩白塊游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-01-01
  • Selenium中免登錄的實現(xiàn)方法option詳解

    Selenium中免登錄的實現(xiàn)方法option詳解

    在selenium中有很多種可以實現(xiàn)網(wǎng)站的免登錄,option就是其中的一種做法,這篇文章主要介紹了Selenium中免登錄的實現(xiàn)方法option,需要的朋友可以參考下
    2022-12-12
  • Pandas merge合并兩個DataFram的實現(xiàn)

    Pandas merge合并兩個DataFram的實現(xiàn)

    本文主要介紹了Pandas merge合并兩個DataFram的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • python PyTorch參數(shù)初始化和Finetune

    python PyTorch參數(shù)初始化和Finetune

    這篇文章主要介紹了python PyTorch參數(shù)初始化和Finetune,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • python 函數(shù)中的內(nèi)置函數(shù)及用法詳解

    python 函數(shù)中的內(nèi)置函數(shù)及用法詳解

    這篇文章主要介紹了python 函數(shù)中的內(nèi)置函數(shù) 及用法,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-07-07
  • 淺談python中的數(shù)字類型與處理工具

    淺談python中的數(shù)字類型與處理工具

    下面小編就為大家?guī)硪黄獪\談python中的數(shù)字類型與處理工具。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • 詳解Python如何與?java高效的交互

    詳解Python如何與?java高效的交互

    這篇文章主要為大家介紹了詳解Python如何與java高效的交互的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • python回調(diào)函數(shù)用法實例分析

    python回調(diào)函數(shù)用法實例分析

    這篇文章主要介紹了python回調(diào)函數(shù)用法,較為詳細(xì)的分析了常用的調(diào)用方式,并實例介紹了Python回調(diào)函數(shù)的使用技巧,需要的朋友可以參考下
    2015-05-05
  • Python日期時間模塊datetime詳解與Python 日期時間的比較,計算實例代碼

    Python日期時間模塊datetime詳解與Python 日期時間的比較,計算實例代碼

    python中的datetime模塊提供了操作日期和時間功能,本文為大家講解了datetime模塊的使用方法及與其相關(guān)的日期比較,計算實例
    2018-09-09
  • Python使用遺傳算法解決最大流問題

    Python使用遺傳算法解決最大流問題

    這篇文章主要為大家詳細(xì)介紹了Python使用遺傳算法解決最大流問題,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01

最新評論