0

My quiz is currently only recognizing one of the three csv files sent with it. this is the hardquestion.csv file. I am attempting to have different levels of difficulty Here is my code:

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

You will need 3 .csv files called "easyquestion.csv", "mediumquestion.csv" and 'hardquestion.csv" stored in the same folder. They will look something like this

#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

Thanks in advance!

Arjyo
  • 3
  • 3
  • 1
    Does this answer your question? [Why is Button parameter “command” executed when declared?](https://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared) – fhdrsdg Feb 06 '20 at 14:38
  • I tried adding lambda but then it just says that : Traceback (most recent call last): File "/Users/arjyo/Documents/quiz experiments/Actual quiz trial.py", line 102, in file_handle = os.path.join(os.path.dirname(__file__), filename) NameError: name 'filename' is not defined – Arjyo Feb 06 '20 at 14:51
  • That makes sense, since you call that line before any button has been pressed so `filename` is indeed not defined. You need to understand event-driven programming to use tkinter. To quote [Bryan Oakley](https://stackoverflow.com/a/9343402/3714930): "Event-driven programming requires a different mindset from procedural code. Your application is running in an infinite loop, pulling events off of a queue and processing them." – fhdrsdg Feb 06 '20 at 14:59
  • I tried defining filename by calling the easy, medium and hard functions and it defines the filename. but in doing so the code still only reads the hardquestion.csv file. What am I doing wrong – Arjyo Feb 07 '20 at 01:48
  • If you called `easy()`, `medium()` and `hard()` in one go, the result `filename` is obviously `hardquestion.csv`. You need to find a way to let user select difficulty and call the corresponding function to set the `filename`. – acw1668 Feb 07 '20 at 04:31

0 Answers0