Python--Tkinter完成一个考试测验界面

简介:使用Tkinter完成一个学生考试(测验)的界面,大概长这个样子,因为我要把它放到其他界面中,所以组件的布局还不是很            美观。目前只完成填空题和判断题的输入,其他题型还不能满足。

提醒:1、第一次输入答案,点击下一题答案会被记录;只是输入答案,没有点击下一题,答案不会被记录。

          2、修改答案不仅要在输入框中输入要修改的答案,而且要点击修改,否则答案不会被修改。

 

代码:

import tkinter as tk

courseName = '人工智能及其应用'
question_lists = [['1、 这是一道判断题', '对'],
                  ['2、 这不是一道判断题', '错'],
                  ['3、 Python标准库math中用来计算平方根的函数是____。', '(sqrt)'],
                  ['4、Python程序文件扩展名主要有__和__两种,其中后者常用于GUI程序。', '(py、pyw)'],
                  ['5、 Python源代码程序编译后的文件扩展名为___。', '(pyc)']]
parent = tk.Tk()
parent.geometry("500x500")
# 存放学生的答案
studentAllAnswer = ['' for i in range(10)]
# 单选按钮的值,方便获得选中的按钮的值
var = tk.StringVar()
# 题目index, 用于翻页
completed_question_index = 0


def showQuestions(courseName, question_lists):
    # 添加一个说明
    tk.Label(parent, bg='white', text='学生自测考试界面', font=("微软雅黑", 14)).pack(fill=tk.X, pady=5)
    #  科目、题目、选项、我的答案 的frame
    course_frame = tk.Frame(parent, bg='white')
    question_frame = tk.Frame(parent, bg='white')
    select_frame = tk.Frame(parent, bg='white')
    answer_frame = tk.Frame(parent, bg='white')
    button_frame = tk.Frame(parent, bg='white')
    course_frame.pack(fill=tk.X)
    question_frame.pack(fill=tk.X)
    select_frame.pack(fill=tk.X)
    answer_frame.pack(fill=tk.X)
    button_frame.pack(fill=tk.X)

    # 在 科目、题目、选项、我的答案 frame中添加一个Label说明
    tk.Label(course_frame, bg='white', text='科目', font=("微软雅黑", 12), padx=10, pady=10).pack(side=tk.LEFT)
    tk.Label(question_frame, bg='white', text='题目', font=("微软雅黑", 12), padx=10, pady=10).pack(side=tk.LEFT, pady=5)
    tk.Label(select_frame, bg='white', text='选项', font=("微软雅黑", 12), padx=10, pady=30).pack(side=tk.LEFT)
    tk.Label(answer_frame, bg='white', text='我的答案', font=("微软雅黑", 12), padx=10, pady=10).pack(side=tk.LEFT, pady=5)

    # 科目名称框
    courseNameLabel = tk.Label(course_frame, bg='white', text=courseName, font=("微软雅黑", 12))
    courseNameLabel.pack(fill=tk.X)
    # 题目框
    questionLabel = tk.Label(question_frame, bg='white', wraplength=400, justify='left', font=("微软雅黑", 12))
    questionLabel.pack(fill=tk.X)
    # 判断题的单选按钮
    rbt = tk.Radiobutton(select_frame, text='对', font=("微软雅黑", 16), value='对', indicatoron=1)
    ebt = tk.Radiobutton(select_frame, text='错', font=("微软雅黑", 16), value='错', indicatoron=1)
    r_e_bt = [rbt, ebt]
    # 填空题的输入框
    input_entry = tk.Entry(select_frame, bd=3, justify='left')
    # 我的答案显示框
    student_answer_en = tk.Entry(answer_frame, font=("微软雅黑", 12))
    student_answer_en.selection_clear()
    student_answer_en.xview_scroll(10, 'units')
    student_answer_en.pack(fill=tk.X, pady=5)
    # 提交按钮
    submit_bt = tk.Button(button_frame, text='提交', bg='white', font=("微软雅黑", 12), command=submitAnswer)
    # 因为情景设置是有题目的,所以先显示出第一个题目
    insertQuestion(questionLabel, select_frame, r_e_bt, input_entry, student_answer_en, question_lists[0])

    change_button = tk.Button(button_frame, text='修改', bg='white', font=("微软雅黑", 12),
              command=lambda: collectAnswer('change', input_entry, student_answer_en)
              )
    change_button.pack(side=tk.LEFT)
    # 如果题目数量多于一个, 显示出上下翻题按钮和当前题目序号
    if len(question_lists) > 1:
        index_lb = tk.Label(button_frame, bg='white', font=("微软雅黑", 12))

        tk.Button(button_frame, text='上一题', bg='white', font=("微软雅黑", 12),
                  command=lambda: back_next_question('back', index_lb, question_lists, input_entry,
                                                     questionLabel, select_frame, r_e_bt,
                                                     student_answer_en, submit_bt, change_button)
                  ).pack(side=tk.LEFT, padx=50)
        index_lb.pack(side=tk.LEFT, padx=20)
        tk.Button(button_frame, text='下一题', bg='white', font=("微软雅黑", 12),
                  command=lambda: back_next_question('next', index_lb, question_lists, input_entry,
                                                     questionLabel, select_frame, r_e_bt, student_answer_en, submit_bt,
                                                     change_button)
                  ).pack(side=tk.LEFT, padx=20)


def collectAnswer(type, input_entry, student_answer_en):
    global completed_question_index, var, studentAllAnswer

    # 向下翻页
    if type == 'next':
        # 还没有填答案,就收集答案,否则就只是在翻题
        if student_answer_en.get().startswith('注意'):
            if input_entry.get():
                # 这是填空题
                studentAllAnswer[completed_question_index - 1] = input_entry.get()
            else:
                # 这是判断题
                studentAllAnswer[completed_question_index - 1] = var.get()
    # 修改答案
    elif type == 'change':
        if student_answer_en.get() == '对' or student_answer_en.get() == '错':
            # 判断题
            studentAllAnswer[completed_question_index] = var.get()
            student_answer_en.delete(0, 'end')
            student_answer_en.insert(0, var.get())

        else:
            # 填空题
            studentAllAnswer[completed_question_index] = input_entry.get()
            student_answer_en.delete(0, 'end')
            student_answer_en.insert(0, input_entry.get())


def back_next_question(type, index_lb, question_lists, input_entry, questionLabel,
                       select_frame, r_e_bt, student_answer_en, submit_bt, change_button):
    '''处理上下翻题函数'''
    global completed_question_index, studentAllAnswer
    if type == 'back':
        # 向前翻页
        if completed_question_index == 0:
            # 不能向上翻页
            completed_question_index = 1
        else:
            # 不管上一题是否是判断题,是否已经显示,将其隐藏
            submit_bt.forget()
            # 因为在最后一题点击了两次下一题,所以要-2
            if completed_question_index == len(question_lists):
                completed_question_index -= 2
            else:
                completed_question_index -= 1
            index_lb['text'] = str(completed_question_index + 1)
            insertQuestion(questionLabel, select_frame, r_e_bt, input_entry, student_answer_en,
                           question_lists[completed_question_index])

    else:
        # 向后翻页
        completed_question_index += 1
        if completed_question_index == len(question_lists):
            # 如果到达最后一题, 将最后一题的答案收集,显示提交按钮,隐藏修改按钮
            collectAnswer('next', input_entry, student_answer_en)
            submit_bt.pack(side=tk.LEFT)
            change_button.forget()
        elif completed_question_index > len(question_lists):
            # 到达最后一题,继续点击下一题,直接返回
            return
        else:
            # 更改题号,收集上一题的答案,显示新的题目
            index_lb['text'] = str(completed_question_index + 1)
            collectAnswer('next', input_entry, student_answer_en)
            insertQuestion(questionLabel, select_frame, r_e_bt, input_entry, student_answer_en,
                           question_lists[completed_question_index])


def insertQuestion(questionLabel, select_frame, r_e_bt, input_entry, student_answer_en, question):
    '''在界面上显示新的题目'''
    global var, completed_question_index
    # ['2、 Python标准库math中用来计算平方根的函数是____。', '(sqrt)']
    if question[1] == '对' or question[1] == '错':
        # 判断题
        questionLabel['text'] = (question[0] + '\t(判断题)')
        # 将输入框隐藏
        select_frame.winfo_children()[3].forget()
        # 显示选择对错的按钮
        r_e_bt[0]['variable'] = var
        r_e_bt[0]['text'] = '对'
        r_e_bt[0]['value'] = '对'

        r_e_bt[1]['variable'] = var
        r_e_bt[1]['text'] = '错'
        r_e_bt[1]['value'] = '错'

        r_e_bt[0].pack(side=tk.LEFT, padx=30)
        r_e_bt[1].pack(side=tk.LEFT)
        # 清空我的答案显示框,如果是还没有回答的题目,显示提示,回答过的题目,显示之前回答的答案
        student_answer_en.delete(0, 'end')
        if not studentAllAnswer[completed_question_index]:
            student_answer_en.insert(0, '注意:不要在此输入框中输入答案')
        else:
            student_answer_en.insert(0, studentAllAnswer[completed_question_index])
    elif question[1].startswith('A'):
        # 选择题,还没有设计,忽略
        questionLabel['text'] = (question[0] + '\t(选择题)')

    elif question[1].startswith('(') or question[1].startswith('('):
        # 填空题
        questionLabel['text'] = (question[0] + '\t(填空题)')
        # 将选项按钮隐藏
        for radio_bt in select_frame.winfo_children()[1:-1]:
            radio_bt.forget()
        # 改变选项标签的文字
        select_frame.winfo_children()[0]['text'] = '填写答案'
        input_entry.delete(0, 'end')
        input_entry.xview_scroll(10, 'units')
        input_entry.pack(fill=tk.X, pady=30)
        student_answer_en.delete(0, 'end')
        if not studentAllAnswer[completed_question_index]:
            student_answer_en.insert(0, '注意:不要在此输入框中输入答案')
        else:
            student_answer_en.insert(0, studentAllAnswer[completed_question_index])


def submitAnswer():
    global studentAllAnswer
    print('提交的答案是:')
    print(studentAllAnswer)


showQuestions(courseName, question_lists)
parent.mainloop()

 

  • 7
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值