利用 Python 制作图片轮播应用

在这篇博客中,我将向大家展示如何使用 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 pub

FAVORITE_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 Panel
        btn_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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值