Мой тестовый код показывает только использование одного CSV-файла: использование tkinter и python - PullRequest
0 голосов
/ 06 февраля 2020

Мой тест в настоящее время распознает только один из трех CSV-файлов, отправленных с ним. это файл hardquestion.csv. Я пытаюсь иметь разные уровни сложности. Вот мой код:

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

def menubutton():
    Easybutton = Button(root, text = 'easy', command = easy())
    Easybutton.pack()

    Mediumbutton = Button(root, text = 'medium', command = medium())
    Mediumbutton.pack()

    Hardbutton = Button(root, text = 'hard', command = hard())
    Hardbutton.pack()

# please read / the filename is always equal to hardquestion.csv / idk why??????

def easy ():
    global filename
    filename = "easyquestion.csv"

def medium():
    global filename
    filename = "mediumquestion.csv"

def hard():
    global filename
    filename = "hardquestion.csv"



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
    s = "your score is " + str(score)
    tkinter.messagebox.showinfo("Result", s)

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()

menubutton()
file_handle = os.path.join(os.path.dirname(__file__), filename) 
print(filename)
count = 0
score = 0

questions = []

with open(filename, 'r') as file_handle:
    displayQ = []
    for line in file_handle:
        line_sections = line.strip().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)

buttons()

root.mainloop()

Вам понадобятся 3 файла .csv с именами "easyquestion.csv", "mediumquestion.csv" и "hardquestion.csv", которые хранятся в та же папка. Они будут выглядеть примерно так

#easyquestion.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

#mediumquestion.csv
where is Genghis Khan from?,a)China,b)Nigeria,c)Mongolia,d)Kazakhstan,C
what is Newton's second law of motion?,a) F=ma,b)E=mc^2,c)mv-mu,d) v=u+at,A
who was the first man to walk on the moon?,a)Buzz Aldrin,b)Neil Armstrong,c)Charles Duke,d)David Scott,B

#hardquestion.csv
In Music what term is used to denote a gradual increase in loudness in a piece of music?,a)cresecendo,b)pitch rise,c)note amplification,d)crese rise,A
How many freds are there in a guitar?,a)29,b)25,c)20,d)22,B
What do the initials POTUS stand for?,a)politicians of the United States,b)prime minister of the United States,c)Parliament of the United States,d)president of the United States,D

Заранее спасибо!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...