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
#23
Well, I didn't go to sleep yet, been up since 4:00 AM EST yesterday 27.5 hours.
I went ahead and designed as I saw fit, this code is almost done, next question not yet working.
Don't know when I'll wake after pushing myself, but will get it working then.
At that point, I'll leave it up to you to add any embellishments and/or additions
Dont' forget to run the database creation program first

Here's current code and screenshot
import tkinter as tk
import tkinter.ttk as ttk
import sqlite3
import os
from pathlib import Path
import sys


class QuizGui:
    def __init__(self, parent, title='Generic Title'):
        # create directory anchor
        os.chdir(os.path.abspath(os.path.dirname(__file__)))
        homepath = Path('.')
        datadir = homepath / 'QuestionsDatabase'
        self.database = datadir / 'Questions.db'

        self.dbcon = None
        self.db_connect()
        
        self.choices = None
        self.answer = None
        self.next_question = 1

        self.youranswer = tk.StringVar()
        self.current_question = tk.StringVar()
        self.current_choice = tk.StringVar()
        self.answer_disp = tk.StringVar()
        self.current_choice.set('A')
        
        parent.geometry('600x400+50+50')
        parent.title(title)

        self.build_gui(parent)
        self.db_close()
    
    def build_gui(self, parent):
        frame1 = tk.Frame(parent, bd=5, relief=tk.SUNKEN, padx=5, pady=5)
        frame1.pack(fill=tk.BOTH)
        qlabel = tk.Label(frame1, textvariable=self.current_question)
        qlabel.pack(anchor='w')
        self.get_next_question()
        for choice in self.choices:
            choiceid = choice[0]
            choice_text = choice[1]
            choiceval = f'{choiceid}     {choice_text}'
            rb = tk.Radiobutton(frame1, 
                  text=choiceval,
                  padx = 10, 
                  variable=self.current_choice, 
                  value=choiceid)
            rb.pack(anchor=tk.W)

        frame2 = tk.Frame(frame1, height=5, bg='gray', padx=10, pady=10, relief=tk.RAISED)
        frame2.pack(fill=tk.BOTH)

        btn1 = tk.Button(frame1, text='Select', bd=2, relief=tk.RAISED, 
            padx=5, pady=5, command=self.ChoiceMade)
        btn1.pack(anchor='w')
        btn2 = tk.Button(frame1, text='Next Question', bd=2, relief=tk.RAISED, 
            padx=5, pady=5, command=self.ChoiceMade)
        btn2.pack(anchor='w')
        response = tk.Label(frame1, textvariable=self.get_next_question)
        response.pack(anchor='w')            

    def get_next_question(self):
        question_number = self.next_question
        question, self.choices, self.answer = self.get_question(question_number)
        # print(f'question: {question}, self.choices: {self.choices}, answer: {self.answer}')
        self.current_question.set(f'Question {question_number}:     {question}')
        self.next_question += 1
        self.answer_disp.set('')

    def ChoiceMade(self):
        if self.current_choice.get() == self.answer:
            self.youranswer.set('Correct Answer')            
        else:
            self.youranswer.set('Sorry Wrong Answer')

    def db_connect(self):
        try:
            self.dbcon = sqlite3.connect(self.database)
        except sqlite3.Error as e:
            print(e)

    def get_question(self, question_number):
        qsql = f''' SELECT Question FROM Questions WHERE QuestionId = '{question_number}' '''
        csql = f''' SELECT SeqLetter, Choice FROM Choices WHERE QuestionId = '{question_number}' ORDER BY SeqLetter '''
        asql = f''' SELECT Answer FROM Answers WHERE QuestionId = '{question_number}' '''
        try:
            cursor = self.dbcon.cursor()
            cursor.execute(qsql)
            question = cursor.fetchone()[0]
            choices = []
            cursor.execute(csql)
            while True:
                choice = cursor.fetchmany()
                if not len(choice):
                    break
                choices.append(list(choice[0]))
            cursor.execute(asql)
            answer = cursor.fetchone()[0]
            return question, choices, answer
        except TypeError:
            print(f'Invalid question_number {question_number}')

    def db_close(self):
        self.dbcon.close()

if __name__ == '__main__':
    root = tk.Tk()
    QuizGui(root, title='Quiz Program')
    root.mainloop()
   
Reply


Messages In This Thread
TKINTER quiz using sqlite3 database - by hezza_23 - Apr-01-2019, 01:02 PM
RE: TKINTER quiz using sqlite3 database - by Larz60+ - Apr-05-2019, 11:31 AM
RE: TKINTER quiz using sqlite3 database - by Hilal - Nov-29-2021, 09:42 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  PyQt5 form not displaying my data from SQLite3 Database Linuxdesire 2 5,065 Jan-10-2023, 09:51 PM
Last Post: gradlon93
  Can't get tkinter database aware cascading comboboxes to update properly dford 6 3,783 Jan-11-2022, 08:37 PM
Last Post: deanhystad
Question [Tkinter] data enterred through gui is not storing in sqlite3 database Hilal 21 7,887 Dec-15-2021, 08:48 PM
Last Post: Hilal
  Help with PySimpleGUI INSERT INTO sqlite3 database jrbond 5 7,267 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 6,786 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