Python Forum

Full Version: TKINTER quiz using sqlite3 database
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
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?

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()
Cannot run script as presented:
No module named backend

NameError: name 'file' is not defined
line 49: line = file.readline()

Also, should upgrade to python 3
Hi mate
I'm not sure why the backend is where it can be removed but the file is where data is called from in this code which i am trying to remove to allow the use of a database
I figured out that backend was not needed.

But file has never been opened.
I hope it has I've tested the program it works but I want to remove it anyway so it works with a database (SQLITE3)
Quote:I hope it has I've tested the program it works but I want to remove it anyway so it works with a database (SQLITE3)
Not in the listing presented in post 1.
It loads the data from a text file which isnt included in post 1
I dont believe it is important I want to remove the use of a text file
Can anyone help me set this up to work with a database rather than from a text file?
We first need a script that will run standalone, or provide all modules needed to run.
As an analogy, it would be hard to fix a ping in an engine if we couldn't start the car!
very true ?
the prorgam working is here but you need a text file how can i send that?
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()
        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 = []
file = open("questions.txt", "r")
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()
the text is here but youll need to save it
Which Of these describes an element best?
A Building Block
B Made Of Only One Type Of Atom
C Contains No Chemical Bonds
D Pure Substance
B
Approximately How Many Elements Are There In The Periodic Table?
A 20
B 64
C 118
D 365
C
Groups in the periodic table contain elements with:
A similar atomic mass 
B similar colour
C similar uses
D similar chemical properties
D
Which element is represented by the symbol Cu?
A Copper 
B Carbon
C Chlorine
D Calcium
A
Atoms are made up of:
A Nuclei surrounded by proton(s) 
B Nuclei surrounded by electron(s)
C Neutrons surrounded by electron(s)
D Nuclei and cell membrane(s)
B
Forming compounds involves
A Sharing electrons
B Gain & loss of electrons
C Gain & loss or sharing electrons
D None of the above
A
Compounds are held together by:
A Atomic forces
B Intermolecular forces
C Chemical bonds
D Gluons
B
The formula of a compound shows:
A how a compound was made
B reactivity of a compound
C bonding in a compound
D number and type of atom in the compound
D
Ammonia  gas may be represented by which of the following:
A NH4+ 
B NH3
C NH4
D NH3+
D
A sulphuric acid molecule, H2SO4, contains:
A 1 hydrogen atom  & 1 sulphur atom
B 2 hydrogen atoms  & 1 sulphur atom
C 1 hydrogen atom  & 2 sulphur atoms
D 1 hydrogen atom  & 2 sodium atoms
B
For each carbon atom in (NH4)2CO3 there is/are:
A Eight hydrogen atoms 
B One nitrogen atom
C One and a half oxygen atoms
D Three oxygen molecules
C
Water, methane and ammonia
A All contain oxygen atoms 
B All burn in oxygen
C Are all often used as fuels
D All contain hydrogen atoms
D
Carbon dioxide is an environmental problem because 
A It is a greenhouse gas 
B It binds to haemoglobin
C Carbonic acid is strongly acidic
D It damages the ozone layer
A
The left hand side of a chemical equation has:
A more energy than the right	 
B same number of atoms as the right
C less energy than the right
D fewer atoms than the right
B
During a chemical reaction
A Mass is conserved 
B Heat is always given out
C Gases are always produced
D Elements are never formed
A
Limestone has been used to build:
A Houses 
B Churches
C Bridges
D All of the above
D
Thermal decomposition means a compound is:
A Broken down using electricity 
B Broken down using heat
C Produced from elements using heat
D Broken down by sunlight
B
When calcium carbonate is heated, the gas it produces:
A Bleaches damp litmus paper	 
B Relights a glowing splint
C Burns with a squeaky pop
D Turns limewater cloudy
B
Calcium oxide is also known as:
A Slaked lime 
B Quick lime
C Limewater
D Limestone
D
When copper carbonate is heated it produces:
A Copper oxide and carbon dioxide 
B Copper and carbon dioxide
C Copper oxide and carbon monoxide
D Copper and carbon monoxide
A
Slaked lime is often used:
A To make soil less acidic	 
B To make soil more acidic
C To store acid in soil
D To test for carbon dioxide
A		
Limestone is an important raw material for making:
A Paint; paper & nylon
B Mortar; cement & concrete 
C Plastic; rubber & petrol
D China; steel & silk
B
The reaction between calcium hydroxide and sulphuric acid produces calcium sulphate plus:
A Hydrogen
B Carbon dioxide
C Water
D Oxygen
BX
Pages: 1 2 3 4 5