Hi, I have this program using Tkinter to generate a quiz but I am having difficulty changing it so it uses sqlite3 to import questions from a database rather than a text file. Is anyone able to suggest how I can get this to work?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
from Tkinter import Tk, Frame, Label, Button from time import sleep import backend import sqlite3 class Question: def __init__( self , question, answers, correctLetter): self .question = question self .answers = answers self .correctLetter = correctLetter def check( self , letter, view): global right if (letter = = self .correctLetter): label = Label(view, text = "Right!" ) right + = 1 label = Label(Right) else : label = Label(view, text = "Wrong!" ) label.pack() view.after( 1000 , lambda * args: self .unpackView(view)) def getView( self , window): view = Frame(window) Label(view, text = self .question).pack() Button(view, text = self .answers[ 0 ], command = lambda * args: self .check( "A" , view)).pack() Button(view, text = self .answers[ 1 ], command = lambda * args: self .check( "B" , view)).pack() Button(view, text = self .answers[ 2 ], command = lambda * args: self .check( "C" , view)).pack() Button(view, text = self .answers[ 3 ], command = lambda * args: self .check( "D" , view)).pack() Button(view, text = self .answers[ 4 ], command = lambda * args: self .check( "E" , view)).pack() return view def unpackView( self , view): view.pack_forget() askQuestion() def askQuestion(): global questions, window, index, button, right, number_of_questions if ( len (questions) = = index + 1 ): Label(window, text = "Thank you for answering the questions. " + str (right) + " of " + str (number_of_questions) + " questions answered right" ).pack() return button.pack_forget() index + = 1 questions[index].getView(window).pack() questions = [] con = sqlite3.connect( "questions.db" ) line = file .readline() while (line ! = ""): questionString = line answers = [] for i in range ( 4 ): answers.append( file .readline()) correctLetter = file .readline() correctLetter = correctLetter[: - 1 ] questions.append(Question(questionString, answers, correctLetter)) line = file .readline() file .close() index = - 1 right = 0 number_of_questions = len (questions) window = Tk() button = Button(window, text = "Start" , command = askQuestion) button.pack() window.mainloop() |