asp.net网站开发实例,做网站要自己租服务器,人才网站建设标题,搜房网在这篇博客中#xff0c;我将向大家展示如何使用 xPython 创建一个图片轮播应用。这个应用能够从指定文件夹中加载图片#xff0c;定时轮播#xff0c;并提供按钮来保存当前图片到收藏夹或仅轮播收藏夹中的图片。我们还将实现退出按钮和全屏显示的功能。 C:\pythoncode\new\…在这篇博客中我将向大家展示如何使用 xPython 创建一个图片轮播应用。这个应用能够从指定文件夹中加载图片定时轮播并提供按钮来保存当前图片到收藏夹或仅轮播收藏夹中的图片。我们还将实现退出按钮和全屏显示的功能。 C:\pythoncode\new\pictureinfoldershow.py
环境准备
首先我们需要安装 wxPython 和 pypubsub
pip install wxPython pypubsubimport wx
import os
import glob
import random
import xml.etree.ElementTree as ET
from pubsub import pubFAVORITE_FILE favorite.xmlclass PhotoFrame(wx.Frame):def __init__(self, parent, title):super(PhotoFrame, self).__init__(parent, titletitle, size(800, 600))self.Bind(wx.EVT_CLOSE, self.on_close)self.Bind(wx.EVT_KEY_DOWN, self.on_key_down)self.panel wx.Panel(self)self.imageCtrl wx.StaticBitmap(self.panel)# Button Panelbtn_panel wx.Panel(self.panel)btn_exit wx.Button(btn_panel, label退出)btn_fav wx.Button(btn_panel, label保存当前文件到收藏夹)btn_show_fav wx.Button(btn_panel, label轮播收藏夹照片)btn_exit.Bind(wx.EVT_BUTTON, self.on_exit_button)btn_fav.Bind(wx.EVT_BUTTON, self.on_fav_button)btn_show_fav.Bind(wx.EVT_BUTTON, self.on_show_fav_button)btn_sizer wx.BoxSizer(wx.VERTICAL)btn_sizer.Add(btn_exit, 0, wx.ALL, 5)btn_sizer.Add(btn_fav, 0, wx.ALL, 5)btn_sizer.Add(btn_show_fav, 0, wx.ALL, 5)btn_panel.SetSizer(btn_sizer)hbox wx.BoxSizer(wx.HORIZONTAL)hbox.Add(self.imageCtrl, 1, wx.EXPAND | wx.ALL, 5)hbox.Add(btn_panel, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5)self.panel.SetSizer(hbox)self.timer wx.Timer(self)self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)pub.subscribe(self.on_folder_selected, folder_selected)pub.subscribe(self.on_favorites_selected, favorites_selected)self.ShowFullScreen(True)self.Centre()def on_close(self, event):self.timer.Stop()self.Destroy()def on_key_down(self, event):keycode event.GetKeyCode()if keycode wx.WXK_ESCAPE:self.Close()event.Skip()def on_exit_button(self, event):self.Close()def on_fav_button(self, event):self.save_to_favorites()def on_show_fav_button(self, event):self.show_favorites()def on_timer(self, event):if hasattr(self, photos) and self.photos:photo random.choice(self.photos)self.show_photo(photo)def on_folder_selected(self, folder):self.photos glob.glob(os.path.join(folder, *.jpg)) \glob.glob(os.path.join(folder, *.png)) \glob.glob(os.path.join(folder, *.jpeg))if self.photos:self.timer.Start(5000)def on_favorites_selected(self):self.photos self.load_favorites()if self.photos:self.timer.Start(5000)def show_photo(self, photo):self.current_photo photoimage wx.Image(photo, wx.BITMAP_TYPE_ANY)screenWidth, screenHeight self.GetSize()imgWidth, imgHeight image.GetSize()# 等比例缩放aspectRatio imgWidth / imgHeightif screenWidth / screenHeight aspectRatio:newHeight screenHeightnewWidth screenHeight * aspectRatioelse:newWidth screenWidthnewHeight screenWidth / aspectRatioimage image.Scale(int(newWidth), int(newHeight), wx.IMAGE_QUALITY_HIGH)self.imageCtrl.SetBitmap(wx.Bitmap(image))self.Layout()def save_to_favorites(self):if not hasattr(self, current_photo):returnroot ET.Element(favorites)if os.path.exists(FAVORITE_FILE):tree ET.parse(FAVORITE_FILE)root tree.getroot()new_entry ET.SubElement(root, photo)new_entry.text self.current_phototree ET.ElementTree(root)tree.write(FAVORITE_FILE, encodingutf-8, xml_declarationTrue)def load_favorites(self):if not os.path.exists(FAVORITE_FILE):return []tree ET.parse(FAVORITE_FILE)root tree.getroot()return [child.text for child in root.findall(photo)]def show_favorites(self):pub.sendMessage(favorites_selected)class FolderSelectorFrame(wx.Frame):def __init__(self, parent, title):super(FolderSelectorFrame, self).__init__(parent, titletitle, size(400, 200))panel wx.Panel(self)vbox wx.BoxSizer(wx.VERTICAL)self.folderPicker wx.DirPickerCtrl(panel, message选择照片文件夹)vbox.Add(self.folderPicker, proportion1, flagwx.EXPAND | wx.ALL, border10)btn wx.Button(panel, label开始轮播)vbox.Add(btn, proportion0, flagwx.ALIGN_CENTER | wx.ALL, border10)btn.Bind(wx.EVT_BUTTON, self.on_start_slideshow)panel.SetSizer(vbox)self.Centre()self.Show()def on_start_slideshow(self, event):folder self.folderPicker.GetPath()pub.sendMessage(folder_selected, folderfolder)self.Close()class MyApp(wx.App):def OnInit(self):self.selectorFrame FolderSelectorFrame(None, title选择文件夹)self.photoFrame PhotoFrame(None, title照片轮播)return Trueif __name__ __main__:app MyApp(False)app.MainLoop()
代码实现
我们将实现一个 PhotoFrame 类来展示图片以及一个 FolderSelectorFrame 类来选择图片文件夹。以下是完整的代码
import wx
import os
import glob
import random
import xml.etree.ElementTree as ET
from pubsub import pubFAVORITE_FILE favorite.xmlclass PhotoFrame(wx.Frame):def __init__(self, parent, title):super(PhotoFrame, self).__init__(parent, titletitle, size(800, 600))self.Bind(wx.EVT_CLOSE, self.on_close)self.Bind(wx.EVT_KEY_DOWN, self.on_key_down)self.panel wx.Panel(self)self.imageCtrl wx.StaticBitmap(self.panel)# Button Panelbtn_panel wx.Panel(self.panel)btn_exit wx.Button(btn_panel, label退出)btn_fav wx.Button(btn_panel, label保存当前文件到收藏夹)btn_show_fav wx.Button(btn_panel, label轮播收藏夹照片)btn_exit.Bind(wx.EVT_BUTTON, self.on_exit_button)btn_fav.Bind(wx.EVT_BUTTON, self.on_fav_button)btn_show_fav.Bind(wx.EVT_BUTTON, self.on_show_fav_button)btn_sizer wx.BoxSizer(wx.VERTICAL)btn_sizer.Add(btn_exit, 0, wx.ALL, 5)btn_sizer.Add(btn_fav, 0, wx.ALL, 5)btn_sizer.Add(btn_show_fav, 0, wx.ALL, 5)btn_panel.SetSizer(btn_sizer)hbox wx.BoxSizer(wx.HORIZONTAL)hbox.Add(self.imageCtrl, 1, wx.EXPAND | wx.ALL, 5)hbox.Add(btn_panel, 0, wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5)self.panel.SetSizer(hbox)self.timer wx.Timer(self)self.Bind(wx.EVT_TIMER, self.on_timer, self.timer)pub.subscribe(self.on_folder_selected, folder_selected)pub.subscribe(self.on_favorites_selected, favorites_selected)self.ShowFullScreen(True)self.Centre()def on_close(self, event):self.timer.Stop()self.Destroy()def on_key_down(self, event):keycode event.GetKeyCode()if keycode wx.WXK_ESCAPE:self.Close()event.Skip()def on_exit_button(self, event):self.Close()def on_fav_button(self, event):self.save_to_favorites()def on_show_fav_button(self, event):self.show_favorites()def on_timer(self, event):if hasattr(self, photos) and self.photos:photo random.choice(self.photos)self.show_photo(photo)def on_folder_selected(self, folder):self.photos glob.glob(os.path.join(folder, *.jpg)) \glob.glob(os.path.join(folder, *.png)) \glob.glob(os.path.join(folder, *.jpeg))if self.photos:self.timer.Start(5000)def on_favorites_selected(self):self.photos self.load_favorites()if self.photos:self.timer.Start(5000)def show_photo(self, photo):self.current_photo photoimage wx.Image(photo, wx.BITMAP_TYPE_ANY)screenWidth, screenHeight self.GetSize()imgWidth, imgHeight image.GetSize()# 等比例缩放aspectRatio imgWidth / imgHeightif screenWidth / screenHeight aspectRatio:newHeight screenHeightnewWidth screenHeight * aspectRatioelse:newWidth screenWidthnewHeight screenWidth / aspectRatioimage image.Scale(int(newWidth), int(newHeight), wx.IMAGE_QUALITY_HIGH)self.imageCtrl.SetBitmap(wx.Bitmap(image))self.Layout()def save_to_favorites(self):if not hasattr(self, current_photo):returnroot ET.Element(favorites)if os.path.exists(FAVORITE_FILE):tree ET.parse(FAVORITE_FILE)root tree.getroot()new_entry ET.SubElement(root, photo)new_entry.text self.current_phototree ET.ElementTree(root)tree.write(FAVORITE_FILE, encodingutf-8, xml_declarationTrue)def load_favorites(self):if not os.path.exists(FAVORITE_FILE):return []tree ET.parse(FAVORITE_FILE)root tree.getroot()return [child.text for child in root.findall(photo)]def show_favorites(self):pub.sendMessage(favorites_selected)class FolderSelectorFrame(wx.Frame):def __init__(self, parent, title):super(FolderSelectorFrame, self).__init__(parent, titletitle, size(400, 200))panel wx.Panel(self)vbox wx.BoxSizer(wx.VERTICAL)self.folderPicker wx.DirPickerCtrl(panel, message选择照片文件夹)vbox.Add(self.folderPicker, proportion1, flagwx.EXPAND | wx.ALL, border10)btn wx.Button(panel, label开始轮播)vbox.Add(btn, proportion0, flagwx.ALIGN_CENTER | wx.ALL, border10)btn.Bind(wx.EVT_BUTTON, self.on_start_slideshow)panel.SetSizer(vbox)self.Centre()self.Show()def on_start_slideshow(self, event):folder self.folderPicker.GetPath()pub.sendMessage(folder_selected, folderfolder)self.Close()class MyApp(wx.App):def OnInit(self):self.selectorFrame FolderSelectorFrame(None, title选择文件夹)self.photoFrame PhotoFrame(None, title照片轮播)return Trueif __name__ __main__:app MyApp(False)app.MainLoop()功能实现
选择文件夹用户可以选择一个包含图片的文件夹应用将自动开始轮播。定时轮播每隔 5 秒钟应用会自动切换到下一张图片。全屏显示应用启动时自动全屏显示图片。退出功能按下 ESC 键或点击“退出”按钮可以退出程序。保存到收藏夹点击“保存当前文件到收藏夹”按钮将当前显示的图片路径保存到 favorite.xml 文件。轮播收藏夹图片点击“轮播收藏夹照片”按钮仅轮播 favorite.xml 中保存的图片。
结果如下 总结
本文介绍了如何使用 wxPython 创建一个功能齐全的图片轮播应用。通过 wxPython 的强大功能和灵活的布局管理我们能够轻松实现图片显示、定时切换、按钮交互和文件操作等功能。希望这篇博客能为你提供一些帮助让你在 wxPython 的学习和使用过程中有所收获。如果有任何问题或建议欢迎在评论区留言。