自己有题仅想试验掌握层度,翻阅全网(已阅)找到的刷题软件基本付费,自己手搓一个岂不更香!

下付代码:小伙伴可根据自己情况修改

import random
from tkinter import *


class XiTi:
    def __init__(self):
        self.i = 1
        self.file1 = "select_data.csv"  # 此处修改选择题的文件名 格式CSV 表 ID 问题 选项A  选项B 选项C 选项D 答案 解析
        self.file2 = "bool_data.csv"  ##此处修改判断题的文件名 格式CSV 表 ID 问题 答案 解析
        self.window = Tk()
        self.window.title("电工刷题")
        self.window.geometry("550x600")
        self.data1 = load_file(self.file1)
        self.data2 = load_file(self.file2)
        self.fd = self.random_data()
        self.set_ui()
        self.show()

    def show(self):
        self.window.mainloop()

    def set_ui(self):
        self.label_num = Label(self.window, text="{}/100".format(self.i))
        self.label_num.place(x=400, y=20, width=200, height=20)
        self.Label_ques = Label(self.window, text="问题 : ")
        self.Label_ques.place(x=0, y=20, width=40, height=20)
        self.Button_L = Button(self.window, text="<", command=self.comm_L)
        self.Button_L.place(x=10, y=200, width=100, height=300)
        self.Button_R = Button(self.window, text=">", command=self.comm_R)
        self.Button_R.place(x=435, y=200, width=100, height=300)
        self.Label_anser = Label(self.window, text="")
        self.Label_anser.place(x=20, y=150, width=200, height=40)
        self.Label_jie_xi = Label(self.window, text="")
        self.Label_jie_xi.place(x=0, y=500, width=200, height=40)
        if self.i != 100:
            self.button_request = Label(self.window, text="")
            self.button_request.place(x=150, y=0, width=200, height=40)
        if self.i < 71:
            self.Label_ques_Text = Label(self.window, text="{}".format(self.data1[self.fd[self.i - 1]][1]),
                                         wraplength=500)
            self.Label_ques_Text.place(x=20, y=40, width=500, height=60)
        else:
            self.Label_ques_Text = Label(self.window, text="{}".format(self.data2[self.fd[self.i - 1]][1]),
                                         wraplength=500)
            self.Label_ques_Text.place(x=20, y=40, width=500, height=60)
        if self.i < 71:
            self.Button_A = Button(self.window, text="{}".format("A" + self.data1[self.fd[self.i - 1]][2]),
                                   command=self.distory)
            self.Button_A.place(x=125, y=200, width=300, height=40)
        else:
            self.Button_A = Button(self.window, text="对", command=self.distory)
            self.Button_A.place(x=125, y=200, width=300, height=40)
        if self.i < 71:
            self.Button_B = Button(self.window, text="{}".format("B" + self.data1[self.fd[self.i - 1]][3]),
                                   command=self.distory)
            self.Button_B.place(x=125, y=260, width=300, height=40)
        else:
            self.Button_B = Button(self.window, text="错", command=self.distory)
            self.Button_B.place(x=125, y=260, width=300, height=40)
        if self.i < 71:
            if self.data1[self.fd[self.i - 1]][4] != "":
                self.Button_C = Button(self.window, text="{}".format('C:' + self.data1[self.fd[self.i - 1]][4]),
                                       command=self.distory)
                self.Button_C.place(x=125, y=320, width=300, height=40)
            else:
                self.Label_C = Label(self.window, text="")
                self.Label_C.place(x=125, y=320, width=300, height=40)
        else:
            self.Label_C = Label(self.window, text="")
            self.Label_C.place(x=125, y=320, width=300, height=40)
        if self.i < 71:
            if self.data1[self.fd[self.i - 1]][5] != "":
                self.Button_D = Button(self.window, text="{}".format('D:' + self.data1[self.fd[self.i - 1]][5]),
                                       command=self.distory)
                self.Button_D.place(x=125, y=380, width=300, height=40)
            else:
                self.Label_D = Label(self.window, text="")
                self.Label_D.place(x=125, y=380, width=300, height=40)
        else:
            self.Label_D = Label(self.window, text="")
            self.Label_D.place(x=125, y=380, width=300, height=40)

    def random_data(self):
        que_length = []
        A = self.random_num(self.data1, 70)
        for i in range(len(A)):
            que_length.append(A[i])
        B = self.random_num(self.data2, 30)
        for i in range(len(B)):
            que_length.append(B[i])
        return que_length

    def random_num(self, data, num):
        que_len = []
        while len(que_len) < num:
            num1 = random.randint(0, len(data))
            if num1 not in que_len:
                que_len.append(num1)
        return que_len

    def comm_L(self):
        if self.i == 1:
            self.i = 1
            self.set_ui()
            self.show()
        else:
            self.i -= 1
            self.set_ui()
            self.show()

    def comm_R(self):
        if self.i < 100:
            self.i += 1
            self.set_ui()
            self.show()

    def distory(self):
        if self.i == 100:
            self.button_request = Button(self.window, text="重测",command=self.refist)
            self.button_request.place(x=150, y=0, width=200, height=40)
        if self.i < 71:
            self.Label_anser = Label(self.window, text="答案: {}".format(self.data1[self.fd[self.i - 1]][6]))
            self.Label_anser.place(x=20, y=150, width=200, height=40)
            self.Label_jie_xi = Label(self.window, text="解析: {}".format(self.data1[self.fd[self.i - 1]][7]))
            self.Label_jie_xi.place(x=0, y=500, width=200, height=40)
        else:
            self.Label_anser = Label(self.window, text="答案: {}".format(self.data2[self.fd[self.i - 1]][2]),wraplength=500)
            self.Label_anser.place(x=20, y=150, width=200, height=40)
            self.Label_jie_xi = Label(self.window, text="解析: {}".format(self.data2[self.fd[self.i - 1]][3]),wraplength=500)
            self.Label_jie_xi.place(x=0, y=500, width=200, height=40)

    def refist(self):
        self.i =1
        self.data1 = load_file(self.file1)
        self.data2 = load_file(self.file2)
        self.set_ui()
        self.show()
def load_file(path):
    data = []
    with open(file=path, mode='r') as fd:
        while fd.readline():
            line = fd.readline()
            line = line.split(',')
            data.append(line)
            line = fd.readline()
    return data


if __name__ == '__main__':
    ti = XiTi()

下面是成品(UI不过关,基本功能有的)

选择题(单选):

               

判断题:

第一百道题选择答案后会出现重测按钮,点击可进行重新刷题

Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐