Python 3 Tkinter Script to Build Countries Capital MCQ Quiz App Using Sqlite Database and Random Module GUI Desktop App Full Project For Beginners

 

 

Welcome folks today in this blog post we will be building a countries capital mcq quiz app in python using tkinter framework and sqlite database. All the full source code of the application is shown below.

 

 

 

 

Get Started

 

 

 

In order to get started you need to make an quizzer_00.py file and copy paste the following code

 

 

quizzer_oo.py

 

 

import sqlite3
import random
from tkinter import *
from tkinter import ttk

class Quizzer(object):

    def __init__(self):
        self.country_list = []
        self.capital_list = []
        self.country_capital = {}
        self.target_country = ''


        # set up the country and capital dict
        # country_list = []
        # capital_list = []
        # country_capital = {}
        # target_country = ''
        # question_list = []

        # connect to the database
        country_db="./country_database.db"
        connection=sqlite3.connect(country_db)
        cursor=connection.cursor()

        # query the database and return all matching values
        cursor.execute('select country_name from country where has_capital=1')
        rows=cursor.fetchall()


        # iterate over the rows and put them into a list
        for x in rows:
            x = str(x)
            x = x[2:-3]
            self.country_list.append(x)

        # query the database and return all matching values
        cursor.execute('select capital_city from country where has_capital=1')
        for x in cursor:
            x = str(x)
            x = x[2:-3]
            # print(x)
            self.capital_list.append(x)

        # create the dict
        for x in range(len(self.country_list)):
            self.country_capital[self.country_list[x]] = self.capital_list[x]

    def get_question_list(self):
        question_list = []
        country_capital = self.country_capital
        self.target_country = random.choice(list(country_capital.keys()))

        question_list.append(country_capital[self.target_country])

        x = random.choice(list(country_capital.values()))
        # print(len(question_list))
        while len(question_list) < 4:
            x = random.choice(list(country_capital.values()))
            if x in question_list:
                pass
            else:
                question_list.append(x)

        random.shuffle(question_list)

        question_string = 'What is the capital of {}?'.format(self.target_country)

        return question_string, question_list 

        # print('What is the capital of {}?'.format(self.target_country))
        # print( 'A. {} \nB. {} \nC. {} \nD. {} \n'.format(
        # question_list[0], question_list[1], question_list[2], question_list[3]) )
        
        # i think maybe the gui class should be responsible for the text.
        # def get_label():
        #     label = ttk.Label(root, text = 
        #     target_string)
        #     label.config(justify = CENTER)
        #     # label.config(font = ('Calibri', 18, 'bold'))

        #     label.grid(row = 0, column = 1, columnspan = 4)
        #     label.grid(row=0, column=1, padx=20, pady=20)


    # def get_result(self, value):
    #     if value == self.country_capital[self.target_country]:
    #         return True
    #     else:
    #         return False


def main():


    blah = Quizzer()
    blah.get_question_list()

    meh = Quizzer()
    # print(meh.get_question_list())

    # print(blah.target_country)
    # print(blah.get_question_list())
    # blah.get_question_list()

    whatis = blah.get_question_list()
    # print(whatis)
    # print('target country: {}\n'.format(whatis[0]))
    
    # print(whatis[0])

    # print(whatis[1][0])
    # print(whatis[1][1])
    # print(whatis[1][2])
    # print(whatis[1][3])





    

if __name__ == '__main__':
    main()

 

See also  Python 3 WxPython BMI (Body Mass Index) Calculator By Height and Weight GUI Script Desktop App Full Project For Beginners

 

 

quizzer_two.py

 

 

 

from tkinter import *
from tkinter import ttk
from quizzer_oo import Quizzer


class Feedback():

    def __init__(self, master):

        self.master = master
        self.label_text = None
        self.blah = Quizzer()

        # so this is how you do the title
        master.title('Quizzer')
        master.resizable(False, False)

    def show_start(self):

        # self.label.config(text = "this is the second welcome screen")
        self.label = ttk.Label(
            self.master, text="Hello and welcome.\nThis is a small quiz app. \nThat will help you learn the capital cities of countries.")
        self.label.config(justify=CENTER)
        self.label.config(font=('Calibri', 18, 'bold'))

        self.label.grid(row=0, column=1, columnspan=4)
        self.label.grid(row=0, column=1, padx=20, pady=20)

        self.button_continue = ttk.Button(self.master, text='Continue', padding=(150, 100),
                                 command=self.show_questions)
        self.button_continue.grid(row=1, column=1, stick='nsew', columnspan=2)

        self.button_quit = ttk.Button(self.master, text='Quit', padding=(150, 100),
                                 command=self.master.destroy)
        self.button_quit.grid(row=1, column=3, stick='nsew', columnspan=2)

    def show_questions(self):
        self.label.destroy()
        self.button_continue.destroy()
        self.button_quit.destroy()

        # Set the questions up.
        whatis = self.blah.get_question_list()

        question_text = whatis[0]
        button_a_text = whatis[1][0]
        button_b_text = whatis[1][1]
        button_c_text = whatis[1][2]
        button_d_text = whatis[1][3]

        self.label = ttk.Label(self.master, text=question_text)
        self.label.config(justify=CENTER)
        self.label.config(font=('Calibri', 18, 'bold'))

        self.label.grid(row=0, column=1, columnspan=4)
        self.label.grid(row=0, column=1, padx=20, pady=40)

        self.button_a = ttk.Button(self.master, text=button_a_text, padding=(150, 50),
                                   command=lambda: self.check_win(button_a_text))
        self.button_a.grid(row=1, column=1, stick='nsew', columnspan=2)

        self.button_b = ttk.Button(self.master, text=button_b_text, padding=(150, 50),
                                   command=lambda: self.check_win(button_b_text))
        self.button_b.grid(row=1, column=3, stick='nsew', columnspan=2)

        self.button_c = ttk.Button(self.master, text=button_c_text, padding=(150, 50),
                                   command=lambda: self.check_win(button_c_text))
        self.button_c.grid(row=2, column=1, stick='nsew', columnspan=2)

        self.button_d = ttk.Button(self.master, text=button_d_text, padding=(150, 50),
                                   command=lambda: self.check_win(button_d_text))
        self.button_d.grid(row=2, column=3, stick='nsew', columnspan=2)

    def check_win(self, value):
        # print(value)
        if value == self.blah.country_capital[self.blah.target_country]:
            # print('yes?')
            self.show_win()
        else:
            # print('no?')
            self.show_lose()

    def show_win(self):
        self.button_a.destroy()
        self.button_b.destroy()
        self.button_c.destroy()
        self.button_d.destroy()

        self.label.destroy()
        self.label = ttk.Label(
            self.master, text='Well done!\n The capital of {} is {}!\n Do you want to continue?'.format(self.blah.target_country,
                self.blah.country_capital[self.blah.target_country]))
        self.label.config(justify=CENTER)
        self.label.config(font=('Calibri', 18, 'bold'))

        self.label.grid(row=0, column=1, columnspan=4)
        self.label.grid(row=0, column=1, padx=20, pady=20)

        self.button_continue = ttk.Button(self.master, text='Continue', padding=(150, 100),
                                 command=self.show_questions)
        self.button_continue.grid(row=1, column=1, stick='nsew', columnspan=2)

        self.button_quit = ttk.Button(self.master, text='Quit', padding=(150, 100), 
                                command=self.master.destroy)
        self.button_quit.grid(row=1, column=3, stick='nsew', columnspan=2)

    def show_lose(self):
        self.button_a.destroy()
        self.button_b.destroy()
        self.button_c.destroy()
        self.button_d.destroy()
        self.button_continue.destroy()
        self.button_quit.destroy()

        self.label.destroy()
        self.label = ttk.Label(
            self.master, text='That is incorrect.\nThe capital of {} is {}!\n Do you want to continue?'.format(self.blah.target_country,
                                                                                                                 self.blah.country_capital[self.blah.target_country]))
        self.label.config(justify=CENTER)
        self.label.config(font=('Calibri', 18, 'bold'))

        self.label.grid(row=0, column=1, columnspan=4)
        self.label.grid(row=0, column=1, padx=20, pady=20)

        self.button_continue = ttk.Button(self.master, text='Continue', padding=(150, 100),
                                 command=self.show_questions)
        self.button_continue.grid(row=1, column=1, stick='nsew', columnspan=2)

        self.button_quit = ttk.Button(self.master, text='Quit', padding=(150, 100),
                                 command=self.master.destroy)
        self.button_quit.grid(row=1, column=3, stick='nsew', columnspan=2)


def main():

    blah = Quizzer()
    root = Tk()
    feedback = Feedback(root)
    feedback.show_start()
    root.mainloop()

if __name__ == '__main__':
    main()

 

See also  Python 3 PySpark Library Example to Read CSV Files Using Spark Library Full Tutorial For Beginners

 

 

 

 

 

 

DOWNLOAD FULL SOURCE CODE

 

 

Leave a Reply