AndroidDelphiDelphiFMXLearn PythonPythonPython GUI

How To Create A Python Quiz Application With DelphiFMX?

How To Create A Python Quiz Application With DelphiFMX

Python is one of the most popular programming languages today, with uses ranging from data analysis to web development. It’s understandable why many programmers prefer Python due to its simple syntax and robust libraries. While there are many resources for learning Python scripting, quizzes are occasionally the greatest way to evaluate your understanding. In this post, we’ll look at how to use DelphiFMX, a powerful cross-platform application development framework, to build a Python quiz application.

Why Should You Build a Python Quiz Android App?

Constructing a Python quiz enables you to combine your understanding of Python with mobile app development and is a terrific way to improve your programming abilities. To construct a quiz app installed on Android smartphones, you can utilize several tools, including DelphiFMX and PythonFMXBuilder. 

By creating a quiz app, you can give users a fun and instructive experience while simultaneously showcasing your programming skills to potential employers or clients. To keep users interested, you may personalize the app to include many categories and levels of difficulty. Developing an Android quiz app in Python can be a fulfilling undertaking that can advance your programming abilities and benefit users.

What Tools do Python Developers Prefer For Building Python GUIs?

How To Create A Python Quiz Application With DelphiFMX A laptop with the Python Tools download page displayed on screen

Several tools support the ability to create GUIs in Python. PyScripter is a free and open-source IDE for Python on Windows and is one of the most popular options. It was first created as a straightforward IDE to offer a potent scripting option for Delphi applications. It has since developed into a powerful IDE for Python development, nevertheless.

Another well-liked tool for creating GUIs among Python developers is Delphi Community Edition. Because of its open-source nature, the DelphiFMX module gives Python programmers access to the FireMonkey GUI framework. The library makes it possible to create GUIs for various operating systems, including Windows, macOS, Linux, and Android.

A strong Python and Delphi ecosystem depends on the Delphi4PythonExporter. Python programmers can write design code more quickly by utilizing Delphi’s UI design capabilities. As they may design programs using their Delphi skills and subsequently give Python analogs of those apps, this tool can also be helpful for Delphi developers.

PythonFMXBuilder is a DelphiFMX-based Python application builder for Android applications. It enables programmers to install a specific Python script on their phone and incorporate it into an Android application.

How Can You Create A Python Quiz App For Windows Desktop?

How To Create A Python Quiz Application With DelphiFMX A laptop showing the PyScripter Python IDE

Let’s create a basic quiz application for desktop devices! A good approach would be to use the Delphi tools mentioned above. So let’s get started!

What Are the Requirements to Create this App?

Python, PythonFMXBuilder, Delphi4PythonExporter, Delphi, and any text editor or IDE (PyScripter is recommended) that supports Python are required for this tutorial.

We’ve got you covered if you don’t have these installed on your device. We recommend checking out this article, “Powerful Python GUI Project Setup” which will help you get started.

You can grab the code for this tutorial from our Git repository at: https://github.com/Embarcadero/PythonBlogExamples/tree/main/Python_Quiz_App

How to Start a Basic Form For the App in Delphi?

Open Delphi CE and create a blank application by navigating to File > New > Multi-Device Application > Blank Application > Ok. Here we have named our project PythonQuizApp.

How To Create A Python Quiz Application With DelphiFMX The RAD Studio IDE with an example form on display

To understand what each section of the above Delphi IDE means/represents, please go through the free eBook bundle we have developed. This eBook explains the ideology around Delphi Python EcoSystem, all Python GUI offerings, and much more.

How To Create A Python Quiz Application With DelphiFMX The Python GUI ebook bundle

Let’s start by giving our form a name. Right-click on the form and click on QuickEdit. Here we will name our form MainForm and display the title as PythonQuizApp.

How To Create A Python Quiz Application With DelphiFMX Using QuickEdit

Next, let’s resize our form. Head to the object inspector, and in the properties tab, search for ClientHeight and ClientWidth. We will set them as 550 and 350, respectively.

How To Create A Python Quiz Application With DelphiFMX TYhe Object Inspector

We will also rename the source file of our main form to Main.pas to make it easy to keep track of.

How To Create A Python Quiz Application With DelphiFMX The project manager

As you may have guessed, this is the main form of our quiz app. It will serve as the main screen where users will start their Python quiz. To make it more like a quiz app, let us add a label that will greet us with the title of our app. Head over to the standard palette and search for the TLabel component. Next, drag and drop the component into the form.

How To Create A Python Quiz Application With DelphiFMX Searching the component palette

Now, let’s change the text style to display the title. We will do this using TextSettings. Head over to the Object Inspector and search for TextSettings. Select Font under it and click on the ... to see options allowing you to change the label font, size, and style according to your needs.

How To Create A Python Quiz Application With DelphiFMX The object inspector

Now rename the text of each label by right-clicking on that label and selecting Quick Edit. It is important to follow an intuitive naming convention to name the labels so that it is easier to track them. Here, we named our TLabel component as Title with the text “Ultimate Python Quiz.”

How To Create A Python Quiz Application With DelphiFMX The main form

We can change the form’s background color by navigating to the Object Inspector and selecting the form’s fill property.

How To Create A Python Quiz Application With DelphiFMX The object inspector
How To Create A Python Quiz Application With DelphiFMX Choosing the right color

Finally, we need a way to navigate to the next form, where we will start our quiz questions. So, go to the Palette and add a button using the TButton component.

How To Create A Python Quiz Application With DelphiFMX Searching the component palette for a button
How To Create A Python Quiz Application With DelphiFMX Adding the start button to our app's form

As you can see, we have named our button Start. You should even change the font size and style of this button text (as we did for the TLabels above) to make the “START” text on the button more prominent. Here we have to use the font style as Segoe UI. We have also aligned the text to the Center, the same as the Title label above, so that the text appears symmetric.

How To Create A Python Quiz Application With DelphiFMX Selecting the alignment properties

After these changes, our form will look much more visually attractive:

How To Create A Python Quiz Application With DelphiFMX The project in the style editor

Now that we are done with the Main form of our app, let’s create a new form that will display our questions. So right-click on PythonQuizApp.exe and select Add New > Multi-Device Form.

How To Create A Python Quiz Application With DelphiFMX Choosing a multi device form type

This will create a new form that we have named as QuizForm and the source file as Quiz.pas.

Next, we will populate our form with labels, text boxes, and buttons to help users log in to our application. We will give them intuitive names as we add elements to the form. Here is what our final QuizForm looks like:

How To Create A Python Quiz Application With DelphiFMX The options screen of our app

As you can see, we have added 3 labels. The label on the top named QuestionNumberLabel will display the number of attempted questions, whereas the Question label will display the question. Finally, the AnswerStatus label will help us displays a Correct or Incorrect message if the user chooses the answer to the question. If the user does not answer a question, the AnswerStatus will show an error.

We have also added 4 option buttons that will display the choices for each question, and the Next button will help display the next questions only when the user has attempted the current question. After the user has attempted all questions, the button will open the final form that will display the score for the quiz.

We may also see how our app appears on desktop and Android phones. Switch the Style from Windows to Android to verify this. This will alter how our form appears.

How To Create A Python Quiz Application With DelphiFMX The Android version of the form display

Finally, we can make our final form that will help display the user’s level and the quiz’s score. We named our form ResultsForm and the source file Results.pas. Here is what our ResultsForm looks like:

How To Create A Python Quiz Application With DelphiFMX Teh results

Now that we have our forms ready, we must add procedures to each button. Open up each form and double-click on each button. The form’s buttons can be clicked twice to construct Click methods in the .pas file corresponding to our .fmx for each button. This makes it easier to add an on-click function to each button.

How To Create A Python Quiz Application With DelphiFMX How the code looks in the IDE

To ensure that the method is preserved when we save the Python file, add at least one comment (//) after each button Click method. Since we don’t export the run-time implementation using the Delphi4PythonExporter, there is no need to write any run-time implementation code.

How To Create A Python Quiz Application With DelphiFMX No implementation code

How to Export this Form as a Python Script?

You can save your project as a Python script by selecting Tools > Export To Python > Export Current Entire Project.

How To Create A Python Quiz Application With DelphiFMX Exporting the form as a Python script

Now give your application a title, select the Application Main Form as the MainForm, and, finally, select the directory of choice. Click on Export to generate your files.

How To Create A Python Quiz Application With DelphiFMX Using the Python exporter

Delphi will generate four Python scripts, Main.py, Quiz.py, Results.py, and PythonQuizApp.py, in your chosen directory. The Main, Quiz, and Results python files contain the classes for the individual forms, and the PythonQuizApp is used to launch our Delphi app. 

Along with the Python files, .pyfmx files are saved in that directory containing all the form’s visual information.

Let’s take a look at the exported PythonQuizApp.py file.

Next, let’s take a look at the contents of Main.py that will show our start screen:

Here are the contents of Quiz.py where we will be adding code to show and change questions:

Notice that the QuizForm constructor does not take in any values. We will get to that later in the tutorial.

Finally, let’s take a look at Results.py, which we will use to display our results:

How to Add Functionality To the Quiz App?

Now that our app is ready, we can add logic to our game. 

What Initializations and Imports Have to be Made?

Go to your project folder and open up Main.py.

For our MainForm the only thing we need to do is open up the QuizForm that will display our questions. To do this, first, import the QuizForm class from Quiz.py.

Next, navigate to the StartClick function and create a new variable named self.QuestionWindow will create a new instance of the QuizForm. We then display the new form using the show() method. Our final Main.py looks like this:

How to Add Main Functionality to All the Buttons in Main Question Form?

Now let’s take a look at our QuizForm that will display our questions. Let’s start by initializing two variables self.Score and self.QuestionNumber that initializes the score and question number of our quiz.

Let’s store these values inside two instance variables in the class constructor:

Next, let’s define a nested dictionary that contains our questions:

The dictionary contains the QuestionNumber as its key and another dictionary with the question, answer options as a list, and correct answer. We will finally call an __UpdateLabels() function that populates the labels in our form based on the question number. Here is what our __init__ function looks like:

Next, we need to create a __UpdateLabels() function that will help us display our question in the Question label and its corresponding answers in the buttons. We access each value of the dictionary using the self.QuestionNumber variable. We then display each value using the .Text property of each object. In addition, we also update the QuestionNumberLabel to display the current question number.

Here is what our final __UpdateLabels() function looks like:

Now that we have displayed our questions, we need to check whether the option a user selects is correct. To do this, we can use the click functions. Let’s implement this functionality for OptionA.

Navigate to OptionAClick, and start by checking if a button has already been selected. We will use the AnswerStatus label to check whether there is an option that has already been selected. If an option has already been selected, we will allow no changes:

Next, we can check whether the text in the button corresponds to the correct answer to the question. If the correct answer has been selected, we will increment the score and update the AnswerStatus. Otherwise, we will display an Incorrect error message.

Here is what our final OptionAClick function looks like:

We can replicate this step for the other option click functions.

Finally, we need a way to display the next question or to open up the ResultsForm. To do this, we start by importing the ResultsForm from Results.py.

Next, navigate to the NextQuestionClick function. For our app, we will only allow the next question to appear only if the user has answered the current question. To do this, we check the AnswerStatus to see if it has been updated. If the AnswerStatus is empty or shows an error message, we display an error:

If the user has answered the current question, we increment the self.QuestionNumber by 1. Next, we check whether all ten questions have been answered. If the user has answered all ten questions, we will display the ResultsForm, we will update the questions by calling the __UpdateLabels function.. Here is what our final NextQuestionClick function looks like:

Here are the final contents of Quiz.py.

How to Display all Relevant Information on the Results Form?

Now let’s display our final quiz results in the ResultsForm.

Similarly to the QuizForm, our ResultsForm does not take in any additional parameters. Therefore, we will update the __init__ function to take in the final Score:

Next, we can display the score by simply using the .Text property of the self.Score label. We also display the level of the user based on their score on the quiz. Here is what our final Results.py looks like:

Now that code is ready, we can run our app by running PythonQuizApp.py. Here is what our app looks like:

How To Create A Python Quiz Application With DelphiFMX Our example app running as a Python script

How to Build this Quiz App As An Android Application Using Delphi?

Now that we have our PythonQuizApp ready let’s convert this into an Android application.

The Delphi4Python Exporter exports PythonQuizApp with initialization logic for a desktop-based application. Therefore, we will use the PythonFMXBuilder to convert it to an Android app. 

So, open up FMXBuilder and click on Project > New project on the toolbar. Enter the name of the project that you want to build here. Here we define our project as PythonQuizApp.

How To Create A Python Quiz Application With DelphiFMX Defining the project name

Before we can add our Python files we need to change some code that will allow us to run our project in Android. Let’s create a new file named PythonQuizApp_Android.py and paste the contents of PythonQuizApp.py.

Next, add your Python files as well as any other supporting project files you used to build your application. Here we will not be adding PythonQuizApp.py as it will only be used to run our desktop application:

How To Create A Python Quiz Application With DelphiFMX The files

Now, right-click on PythonQuizApp_Android.py file and set it to the main file. But before we can build an .apk, let’s make some important changes to our code. 

First off, Android applications are initialized automatically by Delphi applications. Because the program will be initialized automatically, we don’t need to initialize it or set a title manually. The Application.MainForm doesn’t need to be initialized either but just needs a variable. Lastly, like initialization, we don’t need to launch or destroy the program because Android handles both tasks.

Here is what our PythonQuizApp_Android.py looks like:

Save the file with the code changes you’ve just made.

You can easily install the app on your mobile device thanks to PythonFMXBuilder by clicking on the Run Project button. We can alternatively also create an .apk file to create an Android application. Click the Build Project button on the toolbar to create your file.

How To Create A Python Quiz Application With DelphiFMX Building the project

You will be prompted with a “Build process done” notification in the Messages box once the .apk has been entirely generated.

How To Create A Python Quiz Application With DelphiFMX The messages window

Your generated file will be in the pathtoPythonFMXBuilderGUIexeFolderappsPythonQuizAppbin folder.

How To Create A Python Quiz Application With DelphiFMX Where to find the exported APK

Just copy and paste this .apk file into your phone’s storage. If you access this file from your phone and click on it, you will be prompted with an installation message, which you should proceed with. Then, your PythonQuizApp will be ready for use.

Does the App Work on Your Android Device?

Here is what our Python quiz app looks like on an Android device:

How To Create A Python Quiz Application With DelphiFMX Our example app running on an Android device

When we press the start button, we are taken to the next form or the first question.

How To Create A Python Quiz Application With DelphiFMX Screen 2

Now, if we select the Next button without selecting any answer, we will not proceed and will get an error prompt.

How To Create A Python Quiz Application With DelphiFMX Screen 3

As soon as we select a question, we get to know whether our answer is correct or not.

How To Create A Python Quiz Application With DelphiFMX Screen 5

Then we can even click Next to move forward.

How To Create A Python Quiz Application With DelphiFMX Screen 6 the results screen

After attempting the entire quiz, we get an updated score and a level based on the score.

What Are the Key Takeaways From this Tutorial?

How To Create A Python Quiz Application With DelphiFMX A laptop on a chair with the PyScripter free download page on display

Congratulations on completing this tutorial. We hope it has helped you realize how easy Python GUI development is when using the correct tools.

Developing a Python quiz application with DelphiFMX is a fantastic method to demonstrate your programming prowess and evaluate your language proficiency. DelphiFMX makes it simple to construct cross-platform programs that can operate on various devices thanks to its user-friendly interface and robust functionality. This project is a fun approach to developing your abilities and broadening your knowledge, regardless of whether you are an experienced programmer or just getting started with Python.

After seeing how to use the appropriate Python GUI tools to create Android apps, you’re ready to create apps of your own. So, click here to download Delphi and start making Python-based apps immediately!

What Are the FAQs Regarding This Topic?

How To Create A Python Quiz Application With DelphiFMX The FAQ page opened on a laptop

What is DelphiFMX?

Using the Delphi programming language, developers can construct cross-platform apps utilizing the DelphiFMX software development environment. In addition to a library of pre-built components for typical programming tasks, it offers a visual designer for developing user interfaces. You can use it to create several applications such as ToDo Apps, TicTacToe Apps, or Currency Converters.

Which programming language was employed in developing the quiz application in this article?

Python was used to develop the article’s quiz application. The popular high-level programming language Python is praised for being straightforward and readable. It is employed in various applications, including artificial intelligence, data analysis, and web building.

What is the purpose of creating a quiz application with DelphiFMX?

The goal of using DelphiFMX to construct a quiz application is to produce a cross-platform quiz application that can be used on both Windows and Android. DelphiFMX is the perfect option for building cross-platform apps since it offers a straightforward method for creating user interfaces that function across several platforms.

What are the features of the quiz application created in this article?

The quiz application created in this article allows users to take quizzes, view their scores, and review their answers. They can reset the quiz if they want to retake it.

Why is PyScripter a popular IDE?

When PyScripter was launched, it was a simple IDE that provided a reliable scripting solution for Delphi programs. It evolved into a complete standalone Python IDE over time. Delphi was used to develop it. Python4Delphi, however, enables the addition of Python programs (P4D). It is lighter than other IDEs because it was created in a compiled language. You can use PyScripter’s project management, file explorer, Python interpreter, and debugger, among other things.

Related posts
CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And OpenCV Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Radial Basis Function Networks (RBFNs)

CodeIDELearn PythonPythonPython GUITkinter

How To Make More Than 20 ChatGPT Prompts Work With Python GUI Builders And NumPy Library?

CodeIDEProjectsPythonWindows

Unlock the Power of Python for Deep Learning with Generative Adversarial Networks (GANs) - The Engine behind DALL-E

Leave a Reply

Your email address will not be published. Required fields are marked *