Test maker app 2: another basic example

We’ve seen, in the previous post (Test maker tkinter app: infinite exercises from only one template), how to start making a GUI to create infinite different exercises starting from just one exercise. This time I will show you some code that does something similar. This one creates 10 different exercises from the first and it could be extended to a aleatory number. In the next posts I will try to make a full app to manage these kind of tasks easily to make tests for students.

import os
from random import choice
import tkinter as tk

print("(c) https://pythonprogramming.altervista.org Giovanni Gatto 2019")
# =================== 1 Exercises for all ===============
template = "Acquistate merci per 1.500 euro + IVA 22%\n" * 10
template = template.splitlines()
#print(template)

# =============== CHOICES ======================
ch1 = ["Acquistate merci", "Venduti prodotti"]
ch2 = [2000,3000, 4000, 250, 500, 700, 800, 950, 560, 355, 125]

# ================= Exercises generator from template substituting randolmply the words ====
for n, frase in enumerate(template):
	template[n] = str(n+1) + ") " + template[n].replace("Acquistate merci", choice(ch1))
	template[n] = template[n].replace("1.500", str(choice(ch2)))
	template[n] = template[n] + "\n"

# ===================== Create the text joining the list of exercises =====
x = "".join(template)

# ==================== THE GUI THAT SHOWS THE EXERCISES
root = tk.Tk()
root.title("PythonProgramming Test Maker")
text = tk.Text(root, wrap=tk.WORD)
text.pack(fill=tk.BOTH, expand=1)
text.insert("1.0", x)
root.mainloop()
# ======================= (c) pythonprogramming.altervista.org Giovanni Gatto 2019 ====

The GUI

This GUI shows only the result. As you can see there are two things that changes and that makes the exercise different. There could have been also another thing that could have changed, the 22%. This gives you an idea of the purpose of the project. In the next posts I will ad the widgets to generate new exercises and to show them in the browser in form of an html file, so that it is possible to print them (with the right click of the mouse, choosing print from the contestual menu in the browser) for every student, with different exercises for everyone and with the solutions for the teacher too in a different document.

To be contined…

* A little update to the code has made unuseful to load an external txt file.

Testmkr v. 2: generate more exercises

Let’s add some features like generating more exercises.

The new lines of code:

Menu

I added 2 voices in a menu

menu = tk.Menu(root)
menu.add_command(label="Clear", command=clear)
menu.add_command(label="Generate", command=test_maker)
root.config(menu=menu)

clear function

I made a function to clear the text:

def clear():
	text.delete("1.0", tk.END)

test_maker function

I finally wrapped the code to make an exercise into a function so that I can make it on command when I hit the menu voice generate as many times as I need.

import os
from random import choice
import tkinter as tk

print("(c) https://pythonprogramming.altervista.org Giovanni Gatto 2019")

def test_maker():
	# =================== 1 Exercises for all ===============
	template = "Acquistate merci per 1.500 euro + IVA 22%\n" * 10
	template = template.splitlines()
	#print(template)


	# =============== CHOICES ======================
	ch1 = ["Acquistate merci", "Venduti prodotti"]
	ch2 = [2000,3000, 4000, 250, 500, 700, 800, 950, 560, 355, 125]

	# ================= Exercises generator from template substituting randolmply the words ====
	for n, frase in enumerate(template):
		template[n] = str(n+1) + ") " + template[n].replace("Acquistate merci", choice(ch1))
		template[n] = template[n].replace("1.500", str(choice(ch2)))
		template[n] = template[n] + "\n"

	# ===================== Create the text joining the list of exercises =====
	x = "".join(template)
	text.insert("1.0", x + "\n")
	return x

def clear():
	text.delete("1.0", tk.END)

# ==================== THE GUI THAT SHOWS THE EXERCISES
root = tk.Tk()
root.title("PythonProgramming Test Maker")
menu = tk.Menu(root)
menu.add_command(label="Clear", command=clear)
menu.add_command(label="Generate", command=test_maker)
root.config(menu=menu)
text = tk.Text(root, wrap=tk.WORD)
text.pack(fill=tk.BOTH, expand=1)
root.mainloop()
# ======================= (c) pythonprogramming.altervista.org Giovanni Gatto 2019 ====
"""
V.2 added menu to generate new test; created function test_maker including generation of test 14/10/2019
"""

The new GUI

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.