Python Forum
[Tkinter] TKINTER quiz using sqlite3 database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] TKINTER quiz using sqlite3 database
#1
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()
Reply
#2
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
Reply
#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
Reply
#4
I figured out that backend was not needed.

But file has never been opened.
Reply
#5
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)
Reply
#6
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.
Reply
#7
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
Reply
#8
Can anyone help me set this up to work with a database rather than from a text file?
Reply
#9
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!
Reply
#10
very true ?
the prorgam working is here but you need a text file how can i send that?
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
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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PyQt5 form not displaying my data from SQLite3 Database Linuxdesire 2 6,322 Jan-10-2023, 09:51 PM
Last Post: gradlon93
  Can't get tkinter database aware cascading comboboxes to update properly dford 6 5,902 Jan-11-2022, 08:37 PM
Last Post: deanhystad
Question [Tkinter] data enterred through gui is not storing in sqlite3 database Hilal 21 12,037 Dec-15-2021, 08:48 PM
Last Post: Hilal
  Help with PySimpleGUI INSERT INTO sqlite3 database jrbond 5 8,771 Jul-20-2020, 01:24 PM
Last Post: jrbond
  sQlite3 output to tkinter treeview - how do I set / increase width of the output? dewijones67 5 8,202 Jan-23-2019, 08:45 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020