pythonshell怎么打开测试模式_用Python将excel题库转换为窗口测试模式,python,Excel,第一,弹...

总看Excel背题感觉效率不高,新手小白写了一段python,自动读取题目,进行自我测试。

暂时只完成了单选部分,贴出代码,与大神探讨学习。

# _*_ coding:utf-8 _*_

# creat:ES; Date:2020.11.7; Version:1.0

'''

该程序用于自动出题,仅单选题目

'''

from tkinter import *

from tkinter import messagebox

import xlrd

class Application(Frame):

def __init__(self, master=None):

Frame.__init__(self, master, bg="white")

self.pack(expand=YES, fill=BOTH)

self.window_init()

self.createWidgets()

self.change_question()

def read_quesion(self): # 定义题库

self.quesion_file = 'test.xlsx'

self.file_sheet = '样题'

def read_file(self): # 读取题库内容

self.read_quesion()

que_file = xlrd.open_workbook(self.quesion_file)

self.quesion = que_file.sheet_by_name(self.file_sheet)

self.max = self.quesion.nrows

def window_init(self): # 主页面

self.master.title("水獭的测试")

self.master.bg = 'white'

width, height = self.master.maxsize()

self.master.geometry("470x610".format(width, height))

def createWidgets(self):

# frm1 题目序列号、题目切换

self.frm1 = Frame(self, width=450, height=30)

self.frm1.grid(row=0, column=0, sticky=E)

self.count = 0

self.add_scores = []

self.scores = 0

self.read_file()

self.data = self.quesion.row_values(self.count)

Label(self.frm1, text='序号:').place(x=130, y=20, anchor=CENTER, width=50, height=20) # 序号

self.L1 = Label(self.frm1, text='0')

self.L1.place(x=170, y=20, anchor=CENTER) # 当前题目序号

Label(self.frm1, text='得分:').place(x=230, y=20, anchor=CENTER, width=70, height=20)

self.L2 = Label(self.frm1, text=int(0))

self.L2.place(x=270, y=20, anchor=CENTER, width=50, height=20)

# frm2 题目主干

self.frm2 = LabelFrame(self, width=450, height=120, text="题目")

self.frm2.grid(row=1, column=0, padx=10, pady=10)

self.L3 = Label(self.frm2, text="准备", height=5, wraplength=400, anchor='nw', justify='left')

self.L3.place(x=15, y=2)

# frm3 选择项 ,这里要区分单选、多选、判断,多种题型

self.frm3 = Frame(self, width=400, height=100)

self.frm3.grid(row=2, column=0, padx=10, pady=10, sticky=W)

self.single_choose()

# frm4 解析

self.frm4 = LabelFrame(self, width=450, height=270, text='题目解析')

self.frm4.grid(row=3, column=0, padx=10, pady=10)

self.L4 = Label(self.frm4, text='点击开始', anchor='nw', wraplength=420, justify='left')

self.L4.place(x=10, y=10)

# frm5 版本号,创建信息

self.frm5 = Frame(self, width=400, height=20)

self.frm5.grid(row=4, column=0)

self.msg5 = Label(self.frm5, text="Create by:水獭 Date:2020.11.07 Version:V1.0")

self.msg5.place(x=200, y=7, anchor=CENTER)

def last_quesion(self):

self.count -= 1

if self.count <= 0:

self.count = 1

messagebox.showinfo("提示", "已经是第一题了")

self.L4.config(text="")

self.read_file()

self.data = self.quesion.row_values(self.count)

self.L1.config(text=self.count)

self.L3.config(text=str(self.data[0]) + self.data[2]) # 题干

self.single_choose()

def next_quesion(self):

self.count += 1

if self.count == self.max:

messagebox.showinfo("提示", "答题完毕,得分" + str(self.scores))

self.master.destroy()

self.L4.config(text="")

self.read_file()

self.data = self.quesion.row_values(self.count)

self.L1.config(text=self.count)

self.L3.config(text=str(self.data[0]) + self.data[2]) # 题干

self.single_choose()

def scores_list(self):

self.add_scores.append(self.data[0])

add_scores = {}.fromkeys(self.add_scores).keys()

self.scores = len(add_scores)

def single_choose(self):

'''默认四个选项'''

if self.count == 0:

self.R1 = Radiobutton(self.frm3, text="准备", command=self.judge1)

self.R2 = Radiobutton(self.frm3, text="准备", command=self.judge2)

self.R3 = Radiobutton(self.frm3, text="准备", command=self.judge3)

self.R4 = Radiobutton(self.frm3, text="准备", command=self.judge4)

self.R5 = Radiobutton(self.frm3, text="准备", command=self.judge5)

self.R1.place(x=15, y=0)

self.R2.place(x=15, y=25)

self.R3.place(x=15, y=50)

self.R4.place(x=15, y=75)

self.R5.place(x=15, y=100)

else:

self.choose = self.data[3]

self.answer = self.data[4]

self.answer_list = self.choose.split('\n')

self.R1.config(text=self.answer_list[0])

self.R2.config(text=self.answer_list[1])

self.R3.config(text=self.answer_list[2])

self.R4.config(text=self.answer_list[3])

def judge1(self):

if self.answer == "A":

self.scores_list()

self.L2.config(text=self.scores)

self.L4.config(text="回答正确\n" + self.data[5])

else:

self.L4.config(text="回答错误,正确答案为" + self.answer + '\n' + self.data[5])

def judge2(self):

if self.answer == "B":

self.scores_list()

self.L2.config(text=self.scores)

self.L4.config(text="回答正确\n" + self.data[5])

else:

self.L4.config(text="回答错误,正确答案为" + self.answer + '\n' + self.data[5])

def judge3(self):

if self.answer == "C":

self.scores_list()

self.L2.config(text=self.scores)

self.L4.config(text="回答正确\n" + self.data[5])

else:

self.L4.config(text="回答错误,正确答案为" + self.answer + '\n' + self.data[5])

def judge4(self):

if self.answer == "D":

self.scores_list()

self.L2.config(text=self.scores)

self.L4.config(text="回答正确\n" + self.data[5])

else:

self.L4.config(text="回答错误,正确答案为" + self.answer + '\n' + self.data[5])

def judge5(self):

if self.answer == "E":

self.scores_list()

self.L2.config(text=self.scores)

self.L4.config(text="回答正确\n" + self.data[5])

else:

self.L4.config(text="回答错误,正确答案为" + self.answer + '\n' + self.data[5])

def change_question(self):

self.B1 = Button(self.frm1, text="上一题", command=lambda: self.last_quesion())

self.B1.place(x=10, y=20, anchor=W, width=80, height=20)

self.B2 = Button(self.frm1, text="下一题", command=lambda: self.next_quesion())

self.B2.place(x=410, y=20, anchor=E, width=80, height=20)

if __name__ == '__main__':

app = Application()

app.mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值