日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

用Python制作一个相册播放器(附源码)

發布時間:2024/1/1 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 用Python制作一个相册播放器(附源码) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

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

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

今天小F就帶大家學習一個Python制作相冊播放器的實戰項目。

功能嘛,當然沒有系統自帶的好,僅做學習哈。

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

主要使用到Pygame這個庫,創建一個圖形界面。

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

#?安裝 pip?install?pygame pip?install?tkinter

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

導入相關庫。

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

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

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

最后設置文字背景色和背景圖片。

#?初始化 pygame.init()#?設置寬,?高,?標題欄 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)#?設置文字背景色,?背景圖片 BASE_TEXT_COLOR?=?"#6fffe9" BACKGROUND_IMAGE?=?pygame.image.load("assets/background.png") SCREEN.blit(BACKGROUND_IMAGE,?(0,?0)) #?更新 pygame.display.update()#?設置標題欄圖標 WINDOW_ICON?=?pygame.image.load("assets/window_icon.png") pygame.display.set_icon(WINDOW_ICON)

效果如下,空空蕩蕩。

加載部分按鈕的圖標。

#?設置按鈕背景色,?向后按鈕,?暫停按鈕,?播放按鈕,?向前按鈕,?加載新相冊按鈕 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")

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

其次定義各個按鈕的功能函數。

#?加載按鈕函數 def?load_button():#?打開文件管理器,?選擇文件夾filedialogwindow?=?tkinter.Tk()filedialogwindow.withdraw()filepath?=?filedialog.askdirectory(title="選擇你的相冊")filedialogwindow.destroy()album_player(filepath)#?關閉按鈕 def?quit_button():pygame.quit()sys.exit()#?向后按鈕 def?rewind_button(current_image_index):if?current_image_index?>?0:current_image_index?-=?1rewind_button_pressed?=?Truereturn?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?+=?1seek_button_pressed?=?Truereturn?seek_button_pressed,?current_image_index#?播放按鈕 def?play_button():paused?=?Falseunpaused?=?Truereturn?paused,?unpaused#?暫停按鈕 def?pause_button():paused?=?Trueunpaused?=?Falsereturn?paused,?unpaused

加載按鈕,添加相冊;

關閉按鈕,退出播放器;

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

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

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

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

設置主界面,包含主頁標題欄,加載按鈕,關閉按鈕文字屬性。

同時還需要監聽鼠標點擊事件。

#?主界面 def?main_menu():#?主頁標題欄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")#?關閉按鈕QUIT_BUTTON?=?Button(surface=MAIN_MENU_BUTTON_BACKGROUND,?pos=(WIDTH/2,?585),?text_input="關閉",font=bold_font(100),?base_color=BASE_TEXT_COLOR,?hovering_color="white")while?True:#?監聽鼠標點擊事件current_mouse_pos?=?pygame.mouse.get_pos()LOAD_BUTTON.update(SCREEN)QUIT_BUTTON.update(SCREEN)#?根據鼠標點擊情況,?是否點擊右上角的關閉for?event?in?pygame.event.get():if?event.type?==?pygame.QUIT:pygame.quit()sys.exit()#?根據鼠標點擊情況,?點擊加載或關閉按鈕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()#?當鼠標放置在按鈕上,?按鈕顏色發生改變LOAD_BUTTON.change_color(current_mouse_pos)QUIT_BUTTON.change_color(current_mouse_pos)pygame.display.update()

根據鼠標點擊情況, 是否點擊右上角的關閉;

根據鼠標點擊情況, 點擊加載或關閉按鈕;

當鼠標放置在按鈕上, 按鈕顏色發生改變,變成白色。點擊關閉,應用會關閉掉。

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

此外還要調整圖片的尺寸大小,方便在播放器中查看。

#?相冊播放器功能函數 def?album_player(folder_path):SCREEN.blit(BACKGROUND_IMAGE,?(0,?0))image_file_paths?=?[]image_names?=?[]current_image_index?=?0paused?=?Falseunpaused?=?Falseseek_button_pressed?=?Falserewind_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")#?獲取時間,?設置每5s切換一張圖片previous_time?=?pygame.time.get_ticks()COOLDOWN?=?5000#?設置圖片名稱文字屬性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))#?獲取圖片寬高屬性,?窗口顯示不合適,?調整大小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()#?監聽鼠標點擊事件while?True:for?event?in?pygame.event.get():#?根據鼠標點擊情況,?是否點擊右上角的關閉if?event.type?==?pygame.QUIT:pygame.quit()sys.exit()if?event.type?==?pygame.MOUSEBUTTONDOWN:#?根據鼠標點擊情況,?做向前,?向后,?暫停,?開始等切換圖片操作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?=?Falseif?current_image_index?<?len(image_file_paths)-1?and?not?seek_button_pressed?and?not?rewind_button_pressed?and?not?paused:current_image_index?+=?1SCREEN.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?=?Falserewind_button_pressed?=?False

同樣也有監聽鼠標點擊事件,根據鼠標點擊情況,做向前、向后、暫停、開始等切換圖片操作。

最終效果如下。

好了,本期的分享就到此結束了,有興趣的小伙伴可以自行去實踐學習。

講真,Python能做的東西真不少...

使用到的代碼及文件都已上傳,在公眾號python研究者,回復「相冊」即可獲取。

推薦閱讀

  • Python人臉識別簽到考勤系統

  • Python制作可視化GUI界面,一鍵實現自動分類管理文件

  • 5行Python代碼爬取3000+ 上市公司的信息

總結

以上是生活随笔為你收集整理的用Python制作一个相册播放器(附源码)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。