使用wxpython的子模块wx.lib.agw.aui实现小学数学自动出题判题小程序,源代码IDE运行和调试通过

传奇开心果编程
Python领域优质创作者
2024-09-11 18:50:19

img

源代码传奇开心果IDE运行和调试通过


import random
import wx
from wx.lib.agw import aui

# 定义主窗口类
class MathQuizFrame(wx.Frame):
    def __init__(self, parent, title):
        super(MathQuizFrame, self).__init__(parent, title=title, size=(600, 400))

        # 初始化题目相关变量
        self.current_question = 0
        self.questions = []
        self.answers = []

        # 创建 AUI 管理器
        self.manager = aui.AuiManager()
        self.manager.SetManagedWindow(self)

        # 设置面板
        self.panel = wx.Panel(self)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.panel.SetSizer(self.sizer)

        # 添加问题显示控件
        self.question_label = wx.StaticText(self.panel, label="")
        self.sizer.Add(self.question_label, 0, wx.ALL | wx.CENTER, 5)

        # 用户输入框
        self.user_answer = wx.TextCtrl(self.panel)
        self.sizer.Add(self.user_answer, 0, wx.ALL | wx.EXPAND, 5)

        # 按钮布局
        self.button_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.submit_button = wx.Button(self.panel, label="提交")
        self.prev_button = wx.Button(self.panel, label="上一题")
        self.next_button = wx.Button(self.panel, label="下一题")
        self.quit_button = wx.Button(self.panel, label="退出")

        self.button_sizer.Add(self.submit_button, 0, wx.ALL, 5)
        self.button_sizer.Add(self.prev_button, 0, wx.ALL, 5)
        self.button_sizer.Add(self.next_button, 0, wx.ALL, 5)
        self.button_sizer.Add(self.quit_button, 0, wx.ALL, 5)

        self.sizer.Add(self.button_sizer, 0, wx.CENTER)

        # 绑定事件
        self.Bind(wx.EVT_BUTTON, self.on_submit, self.submit_button)
        self.Bind(wx.EVT_BUTTON, self.on_prev, self.prev_button)
        self.Bind(wx.EVT_BUTTON, self.on_next, self.next_button)
        self.Bind(wx.EVT_BUTTON, self.on_quit, self.quit_button)

        # 初始化菜单栏
        self.init_menu()

        # 初始化工具栏
        self.init_toolbar()

        # 生成初始题目
        self.generate_questions(10)
        self.show_question()

    def init_menu(self):
        # 创建菜单栏
        menu_bar = wx.MenuBar()

        # 文件菜单
        file_menu = wx.Menu()
        file_menu.Append(wx.ID_EXIT, "退出")

        # 操作菜单
        operation_menu = wx.Menu()
        operation_menu.Append(wx.NewId(), "上一题")
        operation_menu.Append(wx.NewId(), "下一题")
        operation_menu.Append(wx.NewId(), "提交")

        # 将菜单添加到菜单栏
        menu_bar.Append(file_menu, "&文件")
        menu_bar.Append(operation_menu, "&操作")

        # 设置菜单栏
        self.SetMenuBar(menu_bar)

        # 绑定事件
        self.Bind(wx.EVT_MENU, self.on_quit, id=wx.ID_EXIT)
        self.Bind(wx.EVT_MENU, self.on_prev, id=operation_menu.GetMenuItems()[0].GetId())
        self.Bind(wx.EVT_MENU, self.on_next, id=operation_menu.GetMenuItems()[1].GetId())
        self.Bind(wx.EVT_MENU, self.on_submit, id=operation_menu.GetMenuItems()[2].GetId())

    def init_toolbar(self):
        # 创建工具栏
        toolbar = self.CreateToolBar(wx.TB_HORIZONTAL | wx.NO_BORDER)

        # 工具栏按钮
        prev_tool = toolbar.AddTool(wx.ID_BACKWARD, "上一题", wx.ArtProvider.GetBitmap(wx.ART_GO_BACK, wx.ART_TOOLBAR))
        next_tool = toolbar.AddTool(wx.ID_FORWARD, "下一题", wx.ArtProvider.GetBitmap(wx.ART_GO_FORWARD, wx.ART_TOOLBAR))
        submit_tool = toolbar.AddTool(wx.ID_APPLY, "提交", wx.ArtProvider.GetBitmap(wx.ART_TICK_MARK, wx.ART_TOOLBAR))
        quit_tool = toolbar.AddTool(wx.ID_EXIT, "退出", wx.ArtProvider.GetBitmap(wx.ART_QUIT, wx.ART_TOOLBAR))

        toolbar.Realize()

        # 绑定事件
        self.Bind(wx.EVT_TOOL, self.on_prev, id=prev_tool.GetId())
        self.Bind(wx.EVT_TOOL, self.on_next, id=next_tool.GetId())
        self.Bind(wx.EVT_TOOL, self.on_submit, id=submit_tool.GetId())
        self.Bind(wx.EVT_TOOL, self.on_quit, id=quit_tool.GetId())

    def generate_questions(self, num):
        for _ in range(num):
            op = random.choice(['+', '-', '*', '/'])
            if op == '/':
                x = random.randint(1, 10)
                y = random.randint(1, x)
            else:
                x = random.randint(1, 10)
                y = random.randint(1, 10)

            question = f"{x} {op} {y}"
            answer = eval(question)

            self.questions.append(question)
            self.answers.append(answer)

    def show_question(self):
        if self.current_question < len(self.questions):
            self.question_label.SetLabel(f"问题: {self.questions[self.current_question]}")
            self.user_answer.Clear()

    def on_submit(self, event):
        user_ans = self.user_answer.GetValue()
        try:
            user_ans = float(user_ans)
            if user_ans == self.answers[self.current_question]:
                wx.MessageBox("正确!", "结果", wx.OK | wx.ICON_INFORMATION)
            else:
                wx.MessageBox(f"错误!正确答案为: {self.answers[self.current_question]}", "结果", wx.OK | wx.ICON_WARNING)
        except ValueError:
            wx.MessageBox("请输入数字!", "警告", wx.OK | wx.ICON_WARNING)

    def on_prev(self, event):
        if self.current_question > 0:
            self.current_question -= 1
            self.show_question()

    def on_next(self, event):
        if self.current_question < len(self.questions) - 1:
            self.current_question += 1
            self.show_question()

    def on_quit(self, event):
        self.Close(True)


# 运行应用程序
if __name__ == "__main__":
    app = wx.App(False)
    frame = MathQuizFrame(None, "小学数学自动出题判题小程序")
    frame.Show(True)
    app.MainLoop()
...全文
144 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复 1
开源,可以免费商用

8

社区成员

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

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