使用wxpython的子模块wx.lib.agw.aui实现的中秋抽奖程序,IDE运行和调试通过

传奇开心果编程
Python领域优质创作者
2024-09-16 18:01:29

img

img

import random
import wx
import wx.lib.agw.aui as aui

class LotteryFrame(wx.Frame):
    def __init__(self, *args, **kw):
        super(LotteryFrame, self).__init__(*args, **kw)
        
        self.SetSize((800, 600))
        self.SetTitle("中秋抽奖小程序")

        # 使用AuiManager管理布局
        self.mgr = aui.AuiManager(self)

        # 创建一个面板用于显示奖项
        panel = wx.Panel(self)
        grid_sizer = wx.GridSizer(3, 3, 5, 5)

        prizes = ["山地车", "谢谢参与", "电动车", 
                  "电动车", "开始抽奖", "精美礼品1份",
                  "谢谢参与", "山地车", "代金券100元"]
        
        self.buttons = []
        for prize in prizes:
            btn = wx.Button(panel, label=prize)
            if prize == "开始抽奖":
                btn.Bind(wx.EVT_BUTTON, self.on_lottery)
            else:
                btn.Bind(wx.EVT_BUTTON, lambda evt, p=prize: self.on_prize_button(evt, p))
            self.buttons.append(btn)
            grid_sizer.Add(btn, 0, wx.EXPAND)

        # 添加外部BoxSizer以设置边缘空白
        outer_sizer = wx.BoxSizer(wx.VERTICAL)
        outer_sizer.Add(grid_sizer, 1, wx.ALL | wx.EXPAND, 30)  # 设置四周空白为30像素
        panel.SetSizer(outer_sizer)
        self.mgr.AddPane(panel, aui.AuiPaneInfo().Center().Caption("奖项"))

        # 一个面板用于显示中奖名单
        self.winner_panel = wx.Panel(self)
        self.winner_listbox = wx.ListBox(self.winner_panel, style=wx.LB_SINGLE | wx.LB_NEEDED_SB)
        self.winner_sizer = wx.BoxSizer(wx.VERTICAL)
        self.winner_sizer.Add(wx.StaticText(self.winner_panel, label="中奖奖品:"), 0, wx.ALL, 5)
        self.winner_sizer.Add(self.winner_listbox, 1, wx.EXPAND | wx.ALL, 5)
        self.winner_panel.SetSizer(self.winner_sizer)
        self.mgr.AddPane(self.winner_panel, aui.AuiPaneInfo().Right().Caption("中奖奖品").BestSize(200, -1))  # 加宽15像素

        # 创建工具栏
        toolbar = self.CreateToolBar(style=wx.TB_HORIZONTAL | wx.TB_FLAT | wx.TB_TEXT)
        reset_btn_id = wx.NewIdRef()
        reset_icon = wx.ArtProvider.GetBitmap(wx.ART_REDO, wx.ART_TOOLBAR, (24, 24))
        reset_btn = toolbar.AddTool(reset_btn_id, "重置", reset_icon, shortHelp="重置")
        toolbar.Realize()
        self.Bind(wx.EVT_TOOL, self.on_reset, id=reset_btn_id)

        self.mgr.Update()

        # 初始化变量
        self.round_number = 1

    def on_lottery(self, event):
        # 抽奖逻辑
        self.disable_buttons()
        self.animate_lottery()

    def animate_lottery(self):
        # 动画效果:遍历所有按钮并随机停止
        num_buttons = len(self.buttons)
        current_index = 0
        self.current_button = None

        def animate():
            nonlocal current_index
            if current_index < num_buttons:
                self.current_button = self.buttons[current_index]
                self.current_button.SetBackgroundColour(wx.RED)
                self.current_button.Refresh()
                current_index += 1
                wx.CallLater(100, animate)  # 每隔100毫秒切换一次按钮
            else:
                self.stop_animation()

        animate()

    def stop_animation(self):
        # 随机选择一个按钮作为中奖按钮
        winning_button = random.choice(self.buttons)
        winning_button.SetBackgroundColour(wx.GREEN)
        winning_button.Refresh()
        self.enable_buttons()
        self.update_winner_list(winning_button.GetLabel())

    def update_winner_list(self, prize):
        # 更新中奖名单
        self.winner_listbox.Append(f"第{self.round_number}轮:{prize}")
        self.round_number += 1
        self.winner_panel.Layout()

    def disable_buttons(self):
        for button in self.buttons:
            button.Disable()

    def enable_buttons(self):
        for button in self.buttons:
            button.Enable()

    def on_prize_button(self, event, prize):
        # 处理其他按钮点击事件
        print(f"点击了 {prize}")

    def on_reset(self, event):
        # 重置逻辑
        self.winner_listbox.Clear()
        self.enable_buttons()
        self.round_number = 1

class LotteryApp(wx.App):
    def OnInit(self):
        frame = LotteryFrame(None)
        frame.Show()
        return True

if __name__ == "__main__":
    app = LotteryApp(False)
    app.MainLoop()

这段代码实现了一个简单的抽奖程序,主要分为以下几个部分:

  1. 窗口初始化
    • 窗口大小和标题:
    python self.SetSize((800, 600))
    self.SetTitle("中秋抽奖小程序")
  2. 布局管理
    • 使用 AUI Manager 进行布局管理:
    python self.mgr = aui.AuiManager(self)
  3. 显示奖项的面板
    • 创建一个面板用于显示奖项:
    python panel = wx.Panel(self)
    grid_sizer = wx.GridSizer(3, 3, 5, 5)
    • 奖项列表:
    python prizes = ["山地车", "谢谢参与", "电动车", "电动车", "开始抽奖", "精美礼品1份", "谢谢参与", "山地车", "代金券100元"]
    • 创建按钮并绑定事件:
    python self.buttons = []
    for prize in prizes:
    btn = wx.Button(panel, label=prize)
    if prize == "开始抽奖":
     btn.Bind(wx.EVT_BUTTON, self.on_lottery)
    
    else:
     btn.Bind(wx.EVT_BUTTON, lambda evt, p=prize: self.on_prize_button(evt, p))
    
    self.buttons.append(btn)
    grid_sizer.Add(btn, 0, wx.EXPAND)
    • 添加外部 BoxSizer 以设置边缘空白:
    python outer_sizer = wx.BoxSizer(wx.VERTICAL)
    outer_sizer.Add(grid_sizer, 1, wx.ALL | wx.EXPAND, 30) # 设置四周空白为30像素
    panel.SetSizer(outer_sizer)
    self.mgr.AddPane(panel, aui.AuiPaneInfo().Center().Caption("奖项"))
  4. 显示中奖奖品的面板
    • 创建一个面板用于显示中奖奖品:
    python self.winner_panel = wx.Panel(self)
    self.winner_listbox = wx.ListBox(self.winner_panel, style=wx.LB_SINGLE | wx.LB_NEEDED_SB)
    self.winner_sizer = wx.BoxSizer(wx.VERTICAL)
    self.winner_sizer.Add(wx.StaticText(self.winner_panel, label="中奖奖品:"), 0, wx.ALL, 5)
    self.winner_sizer.Add(self.winner_listbox, 1, wx.EXPAND | wx.ALL, 5)
    self.winner_panel.SetSizer(self.winner_sizer)
    self.mgr.AddPane(self.winner_panel, aui.AuiPaneInfo().Right().Caption("中奖奖品").BestSize(200, -1)) # 加宽15像素
  5. 创建工具栏
    • 创建工具栏并添加“重置”按钮:
    python toolbar = self.CreateToolBar(style=wx.TB_HORIZONTAL | wx.TB_FLAT | wx.TB_TEXT)
    reset_btn_id = wx.NewIdRef()
    reset_icon = wx.ArtProvider.GetBitmap(wx.ART_REDO, wx.ART_TOOLBAR, (24, 24))
    reset_btn = toolbar.AddTool(reset_btn_id, "重置", reset_icon, shortHelp="重置")
    toolbar.Realize()
    self.Bind(wx.EVT_TOOL, self.on_reset, id=reset_btn_id)
  6. 更新布局 • 更新 AUI Manager 的布局:
    python self.mgr.Update()
  7. 抽奖逻辑 • 开始抽奖事件处理:
    python def on_lottery(self, event):
    self.disable_buttons()
    self.animate_lottery()
    • 动画效果:遍历所有按钮并随机停止:
    python def animate_lottery(self):
    num_buttons = len(self.buttons)
    current_index = 0
    self.current_button = None
    def animate():
     nonlocal current_index
     if current_index < num_buttons:
         self.current_button = self.buttons[current_index]
         self.current_button.SetBackgroundColour(wx.RED)
         self.current_button.Refresh()
         current_index += 1
         wx.CallLater(100, animate)  # 每隔100毫秒切换一次按钮
     else:
         self.stop_animation()
    
    animate()
    • 停止动画并更新中奖名单: python def stop_animation(self):
    winning_button = random.choice(self.buttons)
    winning_button.SetBackgroundColour(wx.GREEN)
    winning_button.Refresh()
    self.enable_buttons()
    self.update_winner_list(winning_button.GetLabel())
    • 更新中奖名单: python def update_winner_list(self, prize):
    self.winner_listbox.Append(f"第{self.round_number}轮:{prize}")
    self.round_number += 1
    self.winner_panel.Layout()
  8. 其他功能 • 禁用和启用按钮: python def disable_buttons(self):
    for button in self.buttons:
     button.Disable()
    
    def enable_buttons(self):
    for button in self.buttons:
     button.Enable()   
    
    • 处理其他按钮点击事件: python def on_prize_button(self, event, prize):
    print(f"点击了 {prize}")
    • 重置逻辑: python def on_reset(self, event):
    self.winner_listbox.Clear()
    self.enable_buttons()
    self.round_number = 1
    总结 这个程序实现了以下功能:
  9. 奖项面板四周留空30像素:通过 wx.BoxSizer 和 wx.ALL | wx.EXPAND, 30 设置四周空白。
  10. 中奖奖品列表加宽15像素:通过 aui.AuiPaneInfo().Right().Caption("中奖奖品").BestSize(200, -1) 设置右侧中奖奖品列表的宽度。
  11. 抽奖逻辑:通过遍历按钮并随机停止来实现抽奖动画效果。
  12. 重置功能:清空中奖名单并重新启用按钮。 以上就是这个抽奖程序的主要逻辑和实现细节。
...全文
172 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复 1
可以把九宫格按钮都换成图片按钮,这样更美观

8

社区成员

发帖
与我相关
我的任务
社区描述
软件开发那些事
软件工程需求分析团队开发 个人社区 甘肃省·酒泉市
社区管理员
  • 传奇开心果编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧