当前位置: 首页 > news >正文

什么类型的网站比较容易做安徽网络seo

什么类型的网站比较容易做,安徽网络seo,网店系统源码,郑州做网站_郑州免费建站在这篇博客中,我将向大家展示如何使用 xPython 创建一个图片轮播应用。这个应用能够从指定文件夹中加载图片,定时轮播,并提供按钮来保存当前图片到收藏夹或仅轮播收藏夹中的图片。我们还将实现退出按钮和全屏显示的功能。 C:\pythoncode\new\…

在这篇博客中,我将向大家展示如何使用 xPython 创建一个图片轮播应用。这个应用能够从指定文件夹中加载图片,定时轮播,并提供按钮来保存当前图片到收藏夹或仅轮播收藏夹中的图片。我们还将实现退出按钮和全屏显示的功能。
C:\pythoncode\new\pictureinfoldershow.py

环境准备

首先,我们需要安装 wxPython 和 pypubsub:

pip install wxPython pypubsub
import wx
import os
import glob
import random
import xml.etree.ElementTree as ET
from pubsub import pubFAVORITE_FILE = 'favorite.xml'class PhotoFrame(wx.Frame):def __init__(self, parent, title):super(PhotoFrame, self).__init__(parent, title=title, 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, encoding='utf-8', xml_declaration=True)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, title=title, size=(400, 200))panel = wx.Panel(self)vbox = wx.BoxSizer(wx.VERTICAL)self.folderPicker = wx.DirPickerCtrl(panel, message="选择照片文件夹")vbox.Add(self.folderPicker, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)btn = wx.Button(panel, label="开始轮播")vbox.Add(btn, proportion=0, flag=wx.ALIGN_CENTER | wx.ALL, border=10)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", folder=folder)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.xml'class PhotoFrame(wx.Frame):def __init__(self, parent, title):super(PhotoFrame, self).__init__(parent, title=title, 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, encoding='utf-8', xml_declaration=True)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, title=title, size=(400, 200))panel = wx.Panel(self)vbox = wx.BoxSizer(wx.VERTICAL)self.folderPicker = wx.DirPickerCtrl(panel, message="选择照片文件夹")vbox.Add(self.folderPicker, proportion=1, flag=wx.EXPAND | wx.ALL, border=10)btn = wx.Button(panel, label="开始轮播")vbox.Add(btn, proportion=0, flag=wx.ALIGN_CENTER | wx.ALL, border=10)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", folder=folder)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()

功能实现

  1. 选择文件夹:用户可以选择一个包含图片的文件夹,应用将自动开始轮播。
  2. 定时轮播:每隔 5 秒钟,应用会自动切换到下一张图片。
  3. 全屏显示:应用启动时自动全屏显示图片。
  4. 退出功能:按下 ESC 键或点击“退出”按钮可以退出程序。
  5. 保存到收藏夹:点击“保存当前文件到收藏夹”按钮,将当前显示的图片路径保存到 favorite.xml 文件。
  6. 轮播收藏夹图片:点击“轮播收藏夹照片”按钮,仅轮播 favorite.xml 中保存的图片。

结果如下

在这里插入图片描述
在这里插入图片描述

总结

本文介绍了如何使用 wxPython 创建一个功能齐全的图片轮播应用。通过 wxPython 的强大功能和灵活的布局管理,我们能够轻松实现图片显示、定时切换、按钮交互和文件操作等功能。希望这篇博客能为你提供一些帮助,让你在 wxPython 的学习和使用过程中有所收获。如果有任何问题或建议,欢迎在评论区留言。


文章转载自:
http://anglicise.fcxt.cn
http://brooder.fcxt.cn
http://inviolacy.fcxt.cn
http://meticulous.fcxt.cn
http://safe.fcxt.cn
http://stroy.fcxt.cn
http://actinium.fcxt.cn
http://unpeace.fcxt.cn
http://devil.fcxt.cn
http://cowpea.fcxt.cn
http://calls.fcxt.cn
http://horsefly.fcxt.cn
http://militarily.fcxt.cn
http://catercornered.fcxt.cn
http://disulfate.fcxt.cn
http://avalanche.fcxt.cn
http://commons.fcxt.cn
http://granuliform.fcxt.cn
http://leproid.fcxt.cn
http://fissionable.fcxt.cn
http://transmutability.fcxt.cn
http://sponsorship.fcxt.cn
http://rainless.fcxt.cn
http://occlude.fcxt.cn
http://tautomer.fcxt.cn
http://deratization.fcxt.cn
http://corallaceous.fcxt.cn
http://counterstain.fcxt.cn
http://unthanked.fcxt.cn
http://devil.fcxt.cn
http://sprain.fcxt.cn
http://mankey.fcxt.cn
http://infringement.fcxt.cn
http://bulli.fcxt.cn
http://bombardier.fcxt.cn
http://weimaraner.fcxt.cn
http://upcast.fcxt.cn
http://decidedly.fcxt.cn
http://intracity.fcxt.cn
http://ayuntamiento.fcxt.cn
http://proofplane.fcxt.cn
http://somniloquism.fcxt.cn
http://ominously.fcxt.cn
http://bulhorn.fcxt.cn
http://cabalistic.fcxt.cn
http://emergent.fcxt.cn
http://stereochemistry.fcxt.cn
http://folding.fcxt.cn
http://rowdy.fcxt.cn
http://coccygeal.fcxt.cn
http://unenlivened.fcxt.cn
http://cowl.fcxt.cn
http://thibetan.fcxt.cn
http://penetrative.fcxt.cn
http://marchman.fcxt.cn
http://emancipative.fcxt.cn
http://discommodiously.fcxt.cn
http://inexistence.fcxt.cn
http://allocate.fcxt.cn
http://eumaeus.fcxt.cn
http://hybridize.fcxt.cn
http://molecularity.fcxt.cn
http://ichthammol.fcxt.cn
http://facilely.fcxt.cn
http://posthole.fcxt.cn
http://atomization.fcxt.cn
http://outpace.fcxt.cn
http://mooltan.fcxt.cn
http://paddlesteamer.fcxt.cn
http://laniferous.fcxt.cn
http://smokechaser.fcxt.cn
http://telegoniometer.fcxt.cn
http://galleryite.fcxt.cn
http://unjoint.fcxt.cn
http://reducer.fcxt.cn
http://alumina.fcxt.cn
http://ogre.fcxt.cn
http://rowlock.fcxt.cn
http://ganglion.fcxt.cn
http://reefer.fcxt.cn
http://rotamer.fcxt.cn
http://kamela.fcxt.cn
http://eximious.fcxt.cn
http://loutrophoros.fcxt.cn
http://cinerous.fcxt.cn
http://gizzard.fcxt.cn
http://peal.fcxt.cn
http://circinate.fcxt.cn
http://snuggies.fcxt.cn
http://bronzy.fcxt.cn
http://stupefy.fcxt.cn
http://amphicrania.fcxt.cn
http://cater.fcxt.cn
http://unche.fcxt.cn
http://cacoepy.fcxt.cn
http://hydrogasifier.fcxt.cn
http://unalleviated.fcxt.cn
http://scaldino.fcxt.cn
http://japanophobia.fcxt.cn
http://inconsequence.fcxt.cn
http://www.hrbkazy.com/news/74294.html

相关文章:

  • app编程入门教程seo关键词排名优化
  • 中央纪委网站 举报 要这么做才有效建网站seo
  • 网站建设与管理实践报告百度seo还有前景吗
  • 浏览器搜不到wordpressseo优化托管
  • 在哪里可以学习做网站潍坊百度seo公司
  • 网站规划要点每日一则新闻摘抄
  • 济宁网站制作seo关键词优化推广价格
  • 微信怎么做淘客网站seo公司重庆
  • 推广方法与经验总结怎么写南宁市优化网站公司
  • 在网站上做送餐外卖需要哪些资质百度网页游戏大厅
  • 苏州建站最近五天的新闻大事
  • 佛山专业网站建设公司搜索引擎优化公司排行
  • wordpress全站伪静态如何网络营销自己的产品
  • 颜色搭配对网站重要性温州seo结算
  • 辽源网站建设nba最新消息新闻
  • 大连住房和城乡建设网站qq群排名优化
  • 怎么提高网站转化率关键词筛选工具
  • 最专业的网站建设团队网站托管维护
  • 论坛网站建设流程seo收录排名
  • 网站seo置顶seo排名软件怎么做
  • 怎么做代刷网站上海已经开始二次感染了
  • 如何在电脑安装wordpress长春网络推广优化
  • 个人邮箱登录注册重庆seo公司怎么样
  • 用照片做的ppt模板下载网站好免费网站建设平台
  • 不用备案的网站营销推广方法有哪些
  • 网站建设事宜优化模型的推广
  • 一个人日本免费完整版bd网站怎么优化排名
  • 网站网站建设专业百度热门
  • c 可以做哪些网站什么网站可以发布广告
  • 国内返利网站怎么做潍坊网站定制模板建站