最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python 基于 pygame 實(shí)現(xiàn)輪播圖動(dòng)畫效果

 更新時(shí)間:2024年03月13日 16:29:50   作者:碼農(nóng)強(qiáng)仔  
在Python中可以適應(yīng)第三方庫pygame來實(shí)現(xiàn)輪播圖動(dòng)畫的效果,使用pygame前需確保其已經(jīng)安裝,本文通過實(shí)例代碼介紹Python 基于 pygame 實(shí)現(xiàn)輪播圖動(dòng)畫效果,感興趣的朋友跟隨小編一起看看吧

Python 基于 pygame 實(shí)現(xiàn)輪播圖動(dòng)畫

輪播圖動(dòng)畫是在一個(gè)固定的區(qū)域內(nèi)循環(huán)展示多個(gè)圖片或者內(nèi)容項(xiàng)。在 Python 中可以適應(yīng)第三方庫pygame來實(shí)現(xiàn)輪播圖動(dòng)畫的效果,使用pygame前需確保其已經(jīng)安裝。
如下是代碼示例:

import pygame
def carousel_animation(image_files, screen_width=800, screen_height=600, interval=2000):
    pygame.init()
    # 初始化 Pygame
    pygame.init()
    # 設(shè)置窗口尺寸
    screen = pygame.display.set_mode((screen_width, screen_height))
    # 創(chuàng)建定時(shí)器事件
    CHANGE_IMAGE_EVENT = pygame.USEREVENT + 1
    pygame.time.set_timer(CHANGE_IMAGE_EVENT, interval)
    # 加載第一張圖片
    current_image_index = 0
    image = pygame.image.load(image_files[current_image_index])
    image = pygame.transform.scale(image, (screen_width, screen_height))
    running = True
    # 開啟時(shí)間循環(huán)
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == CHANGE_IMAGE_EVENT:
                # 定時(shí)器事件觸發(fā)時(shí)切換到下一張圖片
                current_image_index = (current_image_index + 1) % len(image_files)
                image = pygame.image.load(image_files[current_image_index])
                # # 調(diào)整圖片大小以適應(yīng)窗口尺寸
                image = pygame.transform.scale(image, (screen_width, screen_height))
        # 在窗口上繪制當(dāng)前的圖片
        screen.blit(image, (0, 0))
        pygame.display.flip()
    pygame.quit()
# 主函數(shù)調(diào)用
if __name__ == "__main__":
    # 圖片文件路徑列表
    image_files = ['img/gou1.jpg', 'img/gou2.jpg', 'img/mao1.jpg', 'img/mao2.jpg']
    # 數(shù)開始輪播圖動(dòng)畫
    carousel_animation(image_files)

上述代碼通過carousel_animation函數(shù)實(shí)現(xiàn)了一個(gè)輪播圖動(dòng)畫的效果,函數(shù)接收?qǐng)D片文件路徑列表image_files作為參數(shù),在函數(shù)內(nèi)部通過pygame.time.set_timer方法來設(shè)置定時(shí)器事件,在主循環(huán)中,不斷地檢測(cè)是否觸發(fā)了定時(shí)器事件,如果觸發(fā)了,就更新當(dāng)前圖片索引值,從而實(shí)現(xiàn)圖片的輪播效果。

需要注意的是,上述示例中的圖像路徑需要根據(jù)實(shí)際情況進(jìn)行替換。

擴(kuò)展:

Python 實(shí)現(xiàn)圖片輪播及音樂循環(huán)播放

根據(jù)自己的實(shí)際情況修改Path參數(shù)。
遇到的問題:如果文件夾下存在圖片損壞會(huì)停止播放,為了播放順暢,可手動(dòng)刪除已損壞圖片。

# -*- coding: utf-8 -*-
"""
Created on 2019/8/20
@author: eln
@requirements: PyCharm 2017.2; Python 3.5.6 |Anaconda 4.1.1 (64-bit)
@decription: 用 Python 制作一個(gè)電子相冊(cè)
"""
# pip install pillow pygame mutagen
import os
import sys
import threading
import tkinter as tk
import time
from PIL import ImageTk, Image
import pygame
from mutagen.mp3 import MP3
def playmusic():
    """播放音樂。"""
    Path = r'music\\'
    try:
        list1 = os.listdir(Path)  # 獲取指定路徑下所有的 mp3 文件
        for x in list1:
            if not (x.endswith('.mp3')):
                list1.remove(x)
        list2 = []
        for i in list1:
            s = os.path.join(Path, i)  # 對(duì)路徑與文件進(jìn)行拼接
            list2.append(s)
        while True:
            for n in list2:
                # 獲取每一首歌的時(shí)長
                path = n
                audio = MP3(n)
                pygame.mixer.init()  # 初始化所有引入的模塊
                pygame.mixer.music.load(path)  # 載入音樂,音樂可以是 ogg、mp3 等格式
                pygame.mixer.music.play()  # 播放載入的音樂
                time.sleep(int(audio.info.length))  # 獲取每一首歌曲的時(shí)長,使程序存活的時(shí)長等于歌曲時(shí)長
    except Exception as e:
        print("Exception: %s" % e)
resolution = (1366, 768)  # 分辨率
Path = r'D:/nlpPredict/SentenceSimilarity/daj/'  # 相冊(cè)路徑
Interval = 5  # 播放間隔.單位:s
Index = 0  # 當(dāng)前照片計(jì)數(shù)
title = "電子相冊(cè)"  # 窗口標(biāo)題
def getfiles():
    """獲取圖片文件名。"""
    files = os.listdir(Path)
    for x in files:
        if not (x.endswith('.jpg') or x.endswith('.JPG') or x.endswith('.png')):
            files.remove(x)
    return files
files = getfiles()
print(files)
scaler = Image.ANTIALIAS  # 設(shè)定 ANTIALIAS ,即抗鋸齒
root = tk.Tk()  # 創(chuàng)建窗口
root.title(title)  # 設(shè)置窗口標(biāo)題
img_in = Image.open(Path + files[0])  # 加載第一張圖片
# img_in = Image.open("load.jpg")  # 加載第一張圖片
w, h = img_in.size  # 獲取圖片大小
size_new = (int(w * resolution[1] / h), resolution[1])
img_out = img_in.resize(size_new, scaler)  # 重新設(shè)置大小
img = ImageTk.PhotoImage(img_out)  # 用 PhotoImage 打開圖片
panel = tk.Label(root, image=img)  # Label 自適應(yīng)圖片大小
panel.pack(side="bottom", fill="both", expand="yes")
def callback(e):
    """手動(dòng)切換圖片。"""
    try:
        global Index
        for i, x in enumerate(files):
            # 判斷文件是否存在
            if not os.path.isfile(Path + '%s' % x):
                break
            if i != Index:  # 跳過已播放的圖片
                continue
            print('手動(dòng)處理圖片', x, Index)  # python 3.5
            # print(unicode('手動(dòng)處理圖片 %s %d' % (x, Index), "utf8", errors="ignore"))  # python 2.7.15
            img_in = Image.open(Path + '%s' % x)
            print(img_in)
            w, h = img_in.size
            size_new = (int(w * resolution[1] / h), resolution[1])
            img_out = img_in.resize(size_new, scaler)
            img2 = ImageTk.PhotoImage(img_out)
            panel.configure(image=img2)
            panel.image = img2
            Index += 1
            if Index >= len(files):
                Index = 0
            break
    except Exception as e:
        print("Exception: %s " % e)
        sys.exit(1)
# root.bind("<Return>", callback)
root.bind("<Button-1>", callback)  # 點(diǎn)擊窗口切換下一張圖片
def image_change():
    """自動(dòng)切換圖片。"""
    try:
        global Index
        time.sleep(3)
        while True:
            for i, x in enumerate(files):
                # 判斷文件是否存在
                if not os.path.isfile(Path + '%s' % x):
                    break
                if i != Index:  # 跳過已播放的圖片
                    continue
                print('自動(dòng)處理圖片', x, Index)  # python 3.5
                # print(unicode('自動(dòng)處理圖片 %s %d' % (x, Index), "utf8", errors="ignore"))  # python 2.7.15
                img_in = Image.open(Path + '%s' % x)
                w, h = img_in.size
                size_new = (int(w * resolution[1] / h), resolution[1])
                img_out = img_in.resize(size_new, scaler)
                img2 = ImageTk.PhotoImage(img_out)
                panel.configure(image=img2)
                panel.image = img2
                Index += 1
                if Index >= len(files):
                    Index = 0
                time.sleep(Interval)
    except Exception as e:
        print("Exception: %s " % e)
        sys.exit(1)
# m = threading.Thread(target=playmusic)  # 創(chuàng)建音樂播放線程
t = threading.Thread(target=image_change)  # 創(chuàng)建圖片切換線程
# python 可以通過 threading module 來創(chuàng)建新的線程,然而在創(chuàng)建線程的線程(父線程)關(guān)閉之后,相應(yīng)的子線程可能卻沒有關(guān)閉
# 需要把 setDaemon 函數(shù)放在 start 函數(shù)前面解決此問題
# m.setDaemon(True)
# m.start()  # 啟動(dòng)線程
t.start()  # 啟動(dòng)線程
root.mainloop()  # 窗口循環(huán)

到此這篇關(guān)于Python 基于 pygame 實(shí)現(xiàn)輪播圖動(dòng)畫效果的文章就介紹到這了,更多相關(guān)Python 輪播圖動(dòng)畫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python反向傳播實(shí)現(xiàn)線性回歸步驟詳細(xì)講解

    Python反向傳播實(shí)現(xiàn)線性回歸步驟詳細(xì)講解

    回歸是監(jiān)督學(xué)習(xí)的一個(gè)重要問題,回歸用于預(yù)測(cè)輸入變量和輸出變量之間的關(guān)系,特別是當(dāng)輸入變量的值發(fā)生變化時(shí),輸出變量的值也隨之發(fā)生變化?;貧w模型正是表示從輸入變量到輸出變量之間映射的函數(shù)
    2022-10-10
  • TensorFlow模型保存和提取的方法

    TensorFlow模型保存和提取的方法

    這篇文章主要為大家詳細(xì)介紹了TensorFlow模型保存和提取的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-03-03
  • Django使用Channels實(shí)現(xiàn)WebSocket的方法

    Django使用Channels實(shí)現(xiàn)WebSocket的方法

    WebSocket是一種在單個(gè)TCP連接上進(jìn)行全雙工通訊的協(xié)議。WebSocket允許服務(wù)端主動(dòng)向客戶端推送數(shù)據(jù)。這篇文章主要介紹了Django使用Channels實(shí)現(xiàn)WebSocket,需要的朋友可以參考下
    2019-07-07
  • Python使用Ollama API的詳細(xì)代碼示例

    Python使用Ollama API的詳細(xì)代碼示例

    這篇文章主要介紹了如何在Python中使用OllamaAPI,涵蓋了從環(huán)境準(zhǔn)備、使用方法到高級(jí)功能的全面指南,無論是初學(xué)者還是經(jīng)驗(yàn)豐富的開發(fā)者都能從中受益,需要的朋友可以參考下
    2025-02-02
  • Python requirements.txt使用小結(jié)

    Python requirements.txt使用小結(jié)

    requirements.txt是Python項(xiàng)目中用于記錄項(xiàng)目依賴包及其版本信息的文本文件,類似于Node.js的或Java的pom.xml,下面就來詳細(xì)的介紹一下requirements.txt使用,感興趣的可以了解一下
    2025-11-11
  • Python代碼生成視頻的縮略圖的實(shí)例講解

    Python代碼生成視頻的縮略圖的實(shí)例講解

    在本篇文章里小編給大家正里的是一篇關(guān)于Python代碼生成視頻的縮略圖的實(shí)例講解,對(duì)此有需要的朋友們可以跟著學(xué)習(xí)下。
    2019-12-12
  • 如何保證Python代碼質(zhì)量,Python測(cè)試與調(diào)試方面的經(jīng)驗(yàn)和心得

    如何保證Python代碼質(zhì)量,Python測(cè)試與調(diào)試方面的經(jīng)驗(yàn)和心得

    本文分享了作者在學(xué)習(xí)Python測(cè)試與調(diào)試方面的經(jīng)驗(yàn)和心得,涵蓋了Python測(cè)試框架(如unittest、pytest)、測(cè)試覆蓋率、單元測(cè)試、集成測(cè)試、調(diào)試技巧、異常處理等內(nèi)容,還對(duì)比了Python與Rust在測(cè)試和調(diào)試方面的差異
    2026-05-05
  • python實(shí)現(xiàn)日歷效果

    python實(shí)現(xiàn)日歷效果

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)日歷效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • PyCharm遠(yuǎn)程調(diào)試代碼配置以及運(yùn)行參數(shù)設(shè)置方式

    PyCharm遠(yuǎn)程調(diào)試代碼配置以及運(yùn)行參數(shù)設(shè)置方式

    這篇文章主要介紹了PyCharm遠(yuǎn)程調(diào)試代碼配置以及運(yùn)行參數(shù)設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-01-01
  • 跟老齊學(xué)Python之通過Python連接數(shù)據(jù)庫

    跟老齊學(xué)Python之通過Python連接數(shù)據(jù)庫

    現(xiàn)在在做python的時(shí)候需要用到數(shù)據(jù)庫,于是自己重新整理了一下數(shù)據(jù)庫的知識(shí),并且熟悉了python中MysqlDB模塊的功能和函數(shù)等接口,現(xiàn)在系統(tǒng)地來總結(jié)一下吧
    2014-10-10

最新評(píng)論

河北区| 绿春县| 阿巴嘎旗| 积石山| 资阳市| 星座| 沅江市| 巩留县| 岳池县| 长垣县| 兴和县| 堆龙德庆县| 调兵山市| 通海县| 互助| 密山市| 华宁县| 延川县| 桐柏县| 潜江市| 治多县| 沈阳市| 曲周县| 贵州省| 昂仁县| 中宁县| 醴陵市| 临高县| 钟山县| 武山县| 正宁县| 江华| 高雄县| 青海省| 内江市| 阿荣旗| 邵阳市| 舒兰市| 富顺县| 莎车县| 鄂尔多斯市|