繁体   English   中英

我正在使用 tkinter 在 python 中创建测验,但是当我尝试获得分数时,它没有给我分数

[英]I am using tkinter to create quiz in python, but when i try to get the score, it does not give me the score

我在 python 中使用 tkinter 创建了一个基本测验。 我想向用户展示他们在测验结束时得到的分数,但目前,分数只显示 0。这是代码

import os
from tkinter import *
import tkinter.messagebox
import random
from PIL import ImageTk,Image
global score
score = 0
global count
count = 0
root = Tk()
root.title('Quiz')
root.config(bg = 'white')
root.geometry('770x555')

class Question:
    def __init__(self, question, correct_ans):
        self.question = question
        self.correct_ans = correct_ans


def ask_quit():
    if tkinter.messagebox.askokcancel("Quit", "The quiz has finished now, would you like to quit?"):
        root.destroy()

def checkResult(letter):
    global score
    if letter == currentQuestion.correct_ans:
        score += 1
    if not len(questions) == 0:
        getNewQuestion()
    else:
        result()
        ask_quit()

def getNewQuestion():
    global count
    global currentQuestion
    currentQuestion = questions.pop()
    for i, var in enumerate((titleVar, aVar, bVar, cVar, dVar)):
        var.set(currentQuestion.question[i])
    count+= 1

def result():
    global score
    print('result:', score)

def buttons():
    question = Label(root, textvariable=titleVar)
    question.pack()

    A = Button(root, textvariable = aVar, command = lambda: checkResult('A'))
    A.pack()

    B = Button(root, textvariable = bVar, command = lambda: checkResult('B'))
    B.pack()

    C = Button(root, textvariable = cVar, command = lambda: checkResult('C'))
    C.pack()

    D = Button(root, textvariable = dVar, command = lambda: checkResult('D'))
    D.pack()

file_handle = os.path.join(os.path.dirname(__file__), "easyquestion.csv") 
count = 0
score = 0

questions = []

with open('easyquestion.csv', 'r') as file_handle:
    # list_question = []
    displayQ = []
    for line in file_handle:
        line_sections = line.split(",")
        displayQ = line_sections[:-1]
        correct_ans = line_sections[-1]
        questions.append(Question(displayQ, correct_ans))

titleVar = StringVar()
aVar = StringVar()
bVar = StringVar()
cVar = StringVar()
dVar = StringVar()

currentQuestion = None
getNewQuestion()

canvas = Canvas(width = 100, height = 100, bg = 'black')
canvas.pack(expand = YES, fill = BOTH)

image = ImageTk.PhotoImage(file = "/Users/arjyo/Downloads/image.png")
canvas.create_image(10, 10, image = image, anchor = NW)

buttons()

root.mainloop()

您还需要一个名为“easyquestion.csv”的同一文件夹中的 .csv 文件。 这就是它的样子

what is the smallest country in the world?,a) Monaco,b) Vatican City,c)Luxembourg,d)Maldives,B
what is the largest country in the world?,a)Russia,b)USA,c)India,d)China,A
what is the largest continent in the world?,a)Europe,b)Africa,c)Asia,d)Australia,C
When did world war 2 start?,a)1945,b)1942,c)1918,d)1939,D
What did Isaac newton discover?,a)electricity,b)gravity,c)energy,d)magnetism,B
what is the scientific name for humans?,a)hetero sapians,b)homo sapiens,c)homo exodus,d)hetero exodus,B
what is the boiling pont of water?,a)110 degree C,b)373 degree C,c)100 deegre C,d)0 degree C,C
What is 2*3*4*5*0+7-4?,a)123,b)27,c)3,d)9,C
Who owns microsoft?,a)Google,b)apple,c)Microsoft,d) ubuntu,C
what is the denary value of: 01001111,a)64,b)70,c)15,d)79,D

我试图打印出分数,然后让它出现在消息框中以在测验结束时显示。 如果您对如何改进代码有任何建议,请随时分享。 提前致谢!

从 csv 文件读取时,您需要删除输入行:

line_sections = line.strip().split(",")

否则, Question.correct_ans末尾会有一个 '\\n' 并且checkResult()内部的比较将失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM