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

基于Python制作一個相冊播放器

 更新時間:2022年06月06日 09:59:10   作者:小F  
對于相冊播放器,大家應(yīng)該都不陌生(用于瀏覽多張圖片的一個應(yīng)用)。本文將利用Python編寫一個簡單的相冊播放器,感興趣的可以學(xué)習(xí)一下

大家好,我是小F。

對于相冊播放器,大家應(yīng)該都不陌生(用于瀏覽多張圖片的一個應(yīng)用)。

當(dāng)然還有視頻、音樂播放器,同樣是用來播放多個視頻、音樂文件的。

在Win10系統(tǒng)下,用【照片】這個應(yīng)用打開一張圖片,就可以瀏覽該圖片所在文件夾中其它圖片了。

從上面的圖中發(fā)現(xiàn),還有不少其它方面的功能,比如圖片裁剪、編輯、打印等。

今天小F就帶大家學(xué)習(xí)一個Python制作相冊播放器的實戰(zhàn)項目。

功能嘛,當(dāng)然沒有系統(tǒng)自帶的好,僅做學(xué)習(xí)哈。

默認(rèn)5秒切換一張圖片,點擊向前按鈕,可以快速切換到下一張圖片。

主要使用到Pygame這個庫,創(chuàng)建一個圖形界面。

還有Tkinter庫,因為要添加一個圖片文件夾,使用tkinter的filedialog快速選取本地文件夾。

# 安裝
pip install pygame
pip install tkinter

好了,接下來就給大家介紹一下。

導(dǎo)入相關(guān)庫

import os
import sys
import glob
import pygame
import tkinter
import os.path
from button import Button
from tkinter import filedialog

初始化,設(shè)置圖形界面的寬為1600,高為900。

添加標(biāo)題欄圖表和標(biāo)題欄文字,以及中文字體,這里用宋體,所以界面顯得有些丑...

最后設(shè)置文字背景色和背景圖片

# 初始化
pygame.init()

# 設(shè)置寬, 高, 標(biāo)題欄
WIDTH, HEIGHT = 1600, 900
SCREEN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("相冊播放器 | 小F 2022")


# 添加中文字體
def bold_font(size):
    os.chdir(sys.path[0])
    return pygame.font.Font("assets/simhei.ttf", size)


def regular_font(size):
    return pygame.font.SysFont("simhei", size)

# 設(shè)置文字背景色, 背景圖片
BASE_TEXT_COLOR = "#6fffe9"
BACKGROUND_IMAGE = pygame.image.load("assets/background.png")
SCREEN.blit(BACKGROUND_IMAGE, (0, 0))
# 更新
pygame.display.update()

# 設(shè)置標(biāo)題欄圖標(biāo)
WINDOW_ICON = pygame.image.load("assets/window_icon.png")
pygame.display.set_icon(WINDOW_ICON)

效果如下,空空蕩蕩。

加載部分按鈕的圖標(biāo)

# 設(shè)置按鈕背景色, 向后按鈕, 暫停按鈕, 播放按鈕, 向前按鈕, 加載新相冊按鈕
MAIN_MENU_BUTTON_BACKGROUND = pygame.image.load("assets/main_menu_button_bg.png")
REWIND_ICON_SURFACE = pygame.image.load("assets/rewind_icon.png")
PAUSE_ICON_SURFACE = pygame.image.load("assets/pause_icon.png")
PLAY_ICON_SURFACE = pygame.image.load("assets/play_icon.png")
SEEK_ICON_SURFACE = pygame.image.load("assets/seek_icon.png")
LOAD_NEW_ALBUM_SURFACE = pygame.image.load("assets/load_new_album_icon.png")

設(shè)置按鈕背景色,向后按鈕,暫停按鈕,播放按鈕,向前按鈕,加載新相冊按鈕。

其次定義各個按鈕的功能函數(shù)

# 加載按鈕函數(shù)
def load_button():
    # 打開文件管理器, 選擇文件夾
    filedialogwindow = tkinter.Tk()
    filedialogwindow.withdraw()
    filepath = filedialog.askdirectory(title="選擇你的相冊")
    filedialogwindow.destroy()
    album_player(filepath)


# 關(guān)閉按鈕
def quit_button():
    pygame.quit()
    sys.exit()


# 向后按鈕
def rewind_button(current_image_index):
    if current_image_index > 0:
        current_image_index -= 1
    rewind_button_pressed = True
    return rewind_button_pressed, current_image_index


# 向前按鈕
def seek_button(current_image_index, image_names):
    if current_image_index+1 < len(image_names):
        current_image_index += 1
    seek_button_pressed = True
    return seek_button_pressed, current_image_index


# 播放按鈕
def play_button():
    paused = False
    unpaused = True
    return paused, unpaused


# 暫停按鈕
def pause_button():
    paused = True
    unpaused = False
    return paused, unpaused

加載按鈕,添加相冊;

關(guān)閉按鈕,退出播放器;

向后按鈕,向后切換一張圖片;

向前按鈕,向前切換一張圖片;

播放按鈕,開始播放相冊中的圖片;

暫停按鈕,暫停相冊圖片的播放;

設(shè)置主界面,包含主頁標(biāo)題欄,加載按鈕,關(guān)閉按鈕文字屬性。

同時還需要監(jiān)聽鼠標(biāo)點擊事件

# 主界面
def main_menu():
    # 主頁標(biāo)題欄
    TITLE_TEXT_SURFACE = bold_font(120).render("相冊播放器", True, BASE_TEXT_COLOR)
    TITLE_TEXT_RECT = TITLE_TEXT_SURFACE.get_rect(center=(WIDTH/2, 175))
    SCREEN.blit(TITLE_TEXT_SURFACE, TITLE_TEXT_RECT)
    # 加載按鈕
    LOAD_BUTTON = Button(
        surface=MAIN_MENU_BUTTON_BACKGROUND, pos=(WIDTH/2, 415), text_input="加載",
        font=bold_font(100), base_color=BASE_TEXT_COLOR, hovering_color="white"
    )
    # 關(guān)閉按鈕
    QUIT_BUTTON = Button(
        surface=MAIN_MENU_BUTTON_BACKGROUND, pos=(WIDTH/2, 585), text_input="關(guān)閉",
        font=bold_font(100), base_color=BASE_TEXT_COLOR, hovering_color="white"
    )
    while True:
        # 監(jiān)聽鼠標(biāo)點擊事件
        current_mouse_pos = pygame.mouse.get_pos()
        LOAD_BUTTON.update(SCREEN)
        QUIT_BUTTON.update(SCREEN)
        # 根據(jù)鼠標(biāo)點擊情況, 是否點擊右上角的關(guān)閉
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            # 根據(jù)鼠標(biāo)點擊情況, 點擊加載或關(guān)閉按鈕
            if event.type == pygame.MOUSEBUTTONDOWN:
                if LOAD_BUTTON.check_for_input(current_mouse_pos):
                    load_button()
                if QUIT_BUTTON.check_for_input(current_mouse_pos):
                    quit_button()

        # 當(dāng)鼠標(biāo)放置在按鈕上, 按鈕顏色發(fā)生改變
        LOAD_BUTTON.change_color(current_mouse_pos)
        QUIT_BUTTON.change_color(current_mouse_pos)
        pygame.display.update()

根據(jù)鼠標(biāo)點擊情況, 是否點擊右上角的關(guān)閉;

根據(jù)鼠標(biāo)點擊情況, 點擊加載或關(guān)閉按鈕;

當(dāng)鼠標(biāo)放置在按鈕上, 按鈕顏色發(fā)生改變,變成白色。點擊關(guān)閉,應(yīng)用會關(guān)閉掉。

最后是相冊播放器的功能函數(shù),設(shè)置每5s切換一張圖片。

此外還要調(diào)整圖片的尺寸大小,方便在播放器中查看

# 相冊播放器功能函數(shù)
def album_player(folder_path):
    SCREEN.blit(BACKGROUND_IMAGE, (0, 0))

    image_file_paths = []
    image_names = []
    current_image_index = 0
    paused = False
    unpaused = False
    seek_button_pressed = False
    rewind_button_pressed = False

    # 添加加載按鈕后, 得到的圖片文件夾路徑
    os.chdir(folder_path)
    for file in glob.glob("*"):
        current_image_path = f"{folder_path}/{file}"
        # 圖片路徑列表
        image_file_paths.append(current_image_path)
        # 圖片名稱列表
        image_names.append(file)

    # 向后按鈕
    REWIND_BUTTON = Button(
        surface=REWIND_ICON_SURFACE, pos=(WIDTH/2-100, HEIGHT-150), text_input="",
        font=bold_font(100), base_color=BASE_TEXT_COLOR, hovering_color="white"
    )
    # 暫停按鈕
    PAUSE_BUTTON = Button(
        surface=PAUSE_ICON_SURFACE, pos=(WIDTH/2, HEIGHT-150), text_input="",
        font=bold_font(100), base_color=BASE_TEXT_COLOR, hovering_color="white"
    )
    # 播放按鈕
    PLAY_BUTTON = Button(
        surface=PLAY_ICON_SURFACE, pos=(WIDTH/2, HEIGHT-150), text_input="",
        font=bold_font(100), base_color=BASE_TEXT_COLOR, hovering_color="white"
    )
    # 向前按鈕
    SEEK_BUTTON = Button(
        surface=SEEK_ICON_SURFACE, pos=(WIDTH/2+100, HEIGHT-150), text_input="",
        font=bold_font(100), base_color=BASE_TEXT_COLOR, hovering_color="white"
    )
    # 加載新相冊按鈕
    LOAD_NEW_ALBUM_BUTTON = Button(
        surface=LOAD_NEW_ALBUM_SURFACE, pos=(WIDTH-325, HEIGHT-150), text_input="",
        font=bold_font(100), base_color=BASE_TEXT_COLOR, hovering_color="white"
    )

    # 獲取時間, 設(shè)置每5s切換一張圖片
    previous_time = pygame.time.get_ticks()
    COOLDOWN = 5000

    # 設(shè)置圖片名稱文字屬性
    photo_title_text_surface = bold_font(90).render(image_names[current_image_index], True, BASE_TEXT_COLOR)
    photo_title_text_rect = photo_title_text_surface.get_rect(center=(WIDTH/2, 150))

    # 圖片張圖顯示
    image_count_text_surface = regular_font(80).render(f"圖片 {current_image_index+1}/{len(image_names)}", True, BASE_TEXT_COLOR)
    image_count_text_rect = image_count_text_surface.get_rect(center=(300, 755))

    # 獲取圖片寬高屬性, 窗口顯示不合適, 調(diào)整大小
    new_image_surface = pygame.image.load(image_file_paths[current_image_index])
    if new_image_surface.get_height() > 500:
        new_image_surface = pygame.transform.scale(new_image_surface, (new_image_surface.get_width() * (500/new_image_surface.get_height()), 500))
    elif new_image_surface.get_width() > 800:
        new_image_surface = pygame.transform.scale(new_image_surface, (800, new_image_surface.get_height() * (800/new_image_surface.get_width())))
    new_image_rect = new_image_surface.get_rect(center=(WIDTH/2, HEIGHT/2))

    SCREEN.blit(new_image_surface, new_image_rect)
    SCREEN.blit(photo_title_text_surface, photo_title_text_rect)
    SCREEN.blit(image_count_text_surface, image_count_text_rect)

    REWIND_BUTTON.update(SCREEN)
    PAUSE_BUTTON.update(SCREEN)
    SEEK_BUTTON.update(SCREEN)
    LOAD_NEW_ALBUM_BUTTON.update(SCREEN)

    pygame.display.update()

    # 監(jiān)聽鼠標(biāo)點擊事件
    while True:
        for event in pygame.event.get():
            # 根據(jù)鼠標(biāo)點擊情況, 是否點擊右上角的關(guān)閉
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.MOUSEBUTTONDOWN:
                # 根據(jù)鼠標(biāo)點擊情況, 做向前, 向后, 暫停, 開始等切換圖片操作
                current_mouse_pos = pygame.mouse.get_pos()
                if REWIND_BUTTON.check_for_input(current_mouse_pos):
                    rewind_button_pressed, current_image_index = rewind_button(current_image_index)
                if SEEK_BUTTON.check_for_input(current_mouse_pos):
                    seek_button_pressed, current_image_index = seek_button(current_image_index, image_names)
                if paused:
                    if PLAY_BUTTON.check_for_input(current_mouse_pos):
                        paused, unpaused = play_button()
                else:
                    if PAUSE_BUTTON.check_for_input(current_mouse_pos):
                        paused, unpaused = pause_button()
                if LOAD_NEW_ALBUM_BUTTON.check_for_input(current_mouse_pos):
                    load_button()

        current_time = pygame.time.get_ticks()

        # 切換圖片, 一定時間、點擊向后按鈕、點擊向前按鈕、點擊開始按鈕
        if current_time - previous_time >= COOLDOWN or rewind_button_pressed or seek_button_pressed or paused or unpaused:
            unpaused = False
            if current_image_index < len(image_file_paths)-1 and not seek_button_pressed and not rewind_button_pressed and not paused:
                current_image_index += 1

            SCREEN.blit(BACKGROUND_IMAGE, (0, 0))
            REWIND_BUTTON.update(SCREEN)
            if paused:
                PLAY_BUTTON.update(SCREEN)
            else:
                PAUSE_BUTTON.update(SCREEN)
            SEEK_BUTTON.update(SCREEN)
            LOAD_NEW_ALBUM_BUTTON.update(SCREEN)

            new_image_surface = pygame.image.load(image_file_paths[current_image_index])
            if new_image_surface.get_height() > 500:
                new_image_surface = pygame.transform.scale(new_image_surface, (new_image_surface.get_width() * (500/new_image_surface.get_height()), 500))
            elif new_image_surface.get_width() > 800:
                new_image_surface = pygame.transform.scale(new_image_surface, (800, new_image_surface.get_height() * (800/new_image_surface.get_width())))
            new_image_rect = new_image_surface.get_rect(center=(WIDTH/2, HEIGHT/2))

            SCREEN.blit(new_image_surface, new_image_rect)

            photo_title_text_surface = bold_font(90).render(image_names[current_image_index], True, BASE_TEXT_COLOR)
            photo_title_text_rect = photo_title_text_surface.get_rect(center=(WIDTH/2, 150))

            SCREEN.blit(photo_title_text_surface, photo_title_text_rect)

            image_count_text_surface = regular_font(80).render(f"圖片 {current_image_index+1}/{len(image_names)}", True, BASE_TEXT_COLOR)
            image_count_text_rect = image_count_text_surface.get_rect(center=(300, 755))

            SCREEN.blit(image_count_text_surface, image_count_text_rect)

            pygame.display.update()
            previous_time = pygame.time.get_ticks()
            seek_button_pressed = False
            rewind_button_pressed = False

同樣也有監(jiān)聽鼠標(biāo)點擊事件,根據(jù)鼠標(biāo)點擊情況,做向前、向后、暫停、開始等切換圖片操作。

最終效果如下

到此這篇關(guān)于基于Python制作一個相冊播放器的文章就介紹到這了,更多相關(guān)Python相冊播放器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Python中os和shutil模塊實用方法集錦

    Python中os和shutil模塊實用方法集錦

    這篇文章主要介紹了Python中os和shutil模塊實用方法集錦,需要的朋友可以參考下
    2014-05-05
  • python反轉(zhuǎn)一個三位整數(shù)的多種實現(xiàn)方案

    python反轉(zhuǎn)一個三位整數(shù)的多種實現(xiàn)方案

    這篇文章主要介紹了python反轉(zhuǎn)一個三位整數(shù)的多種實現(xiàn)方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • 一文教你解決Python不支持中文路徑的問題

    一文教你解決Python不支持中文路徑的問題

    Python是一種廣泛使用的高級編程語言,然而在處理包含中文字符的文件路徑時,Python有時會表現(xiàn)出一些不友好的行為,下面小編就來為大家介紹一下具體的解決方法吧
    2025-03-03
  • python httpx的具體使用

    python httpx的具體使用

    本文主要介紹了python httpx的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • Python中單、雙下劃線的區(qū)別總結(jié)

    Python中單、雙下劃線的區(qū)別總結(jié)

    這篇文章主要給大家介紹了關(guān)于Python中單、雙下劃線區(qū)別的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • 解讀tf.keras.layers模塊中的函數(shù)

    解讀tf.keras.layers模塊中的函數(shù)

    這篇文章主要介紹了tf.keras.layers模塊中的函數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Python實現(xiàn)電腦壁紙的采集與輪換效果

    Python實現(xiàn)電腦壁紙的采集與輪換效果

    這篇文章主要為大家介紹了如何利用Python實現(xiàn)電腦壁紙的采集以及輪換效果,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下
    2022-04-04
  • C++和python實現(xiàn)阿姆斯特朗數(shù)字查找實例代碼

    C++和python實現(xiàn)阿姆斯特朗數(shù)字查找實例代碼

    這篇文章主要給大家介紹了關(guān)于C++和python實現(xiàn)阿姆斯特朗數(shù)字查找的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • python GUI庫圖形界面開發(fā)之PyQt5工具欄控件QToolBar的詳細(xì)使用方法與實例

    python GUI庫圖形界面開發(fā)之PyQt5工具欄控件QToolBar的詳細(xì)使用方法與實例

    這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5工具欄控件QToolBar的詳細(xì)使用方法與實例,需要的朋友可以參考下
    2020-02-02
  • Python中文檔處理神器python-docx的用法解析

    Python中文檔處理神器python-docx的用法解析

    Python中有一個python-docx的庫,它允許創(chuàng)建、修改和操作Word文檔,本文將詳細(xì)介紹python-docx庫的用法,包括如何創(chuàng)建文檔、添加文本、格式化文本等,需要的可以參考下
    2023-11-11

最新評論

扎囊县| 新化县| 松桃| 济南市| 抚顺县| 旺苍县| 安陆市| 广饶县| 华阴市| 巫溪县| 通山县| 万盛区| 乌拉特后旗| 康平县| 中卫市| 五寨县| 辽源市| 焦作市| 望城县| 长寿区| 淮安市| 青浦区| 大邑县| 龙山县| 双柏县| 威远县| 辽宁省| 彭泽县| 宜川县| 盐亭县| 游戏| 泗水县| 齐齐哈尔市| 汝州市| 临漳县| 沾化县| 南江县| 吉首市| 丘北县| 江都市| 田东县|