Test maker tkinter app: infinite exercises from only one template

Ok, if you want to give different tests to your students, but you do not want to take too much time to do it, here is how you can achieve this result that could seem impossible: different tests less amount of time than making the same test for every student. How it is possible. Lets’s start.

Make a template

Let’s say you got a simple exercize. Just one. It’s in Italian, but it is not difficult to make it in any language.

Acquistate merci per 1.500 euro + IVA 22%

This is it. Calculate the tax of 22% on a good.

We want to make 10 exercize, different from each other, and different for every students. That’s are 100 different exercizes. It could take forever to do this task… or less than a second with Python.

Let’s get the hand on some code to start. We define our template.

from random import choice

template = "Acquistate merci per 1.500 euro + IVA 22%"

Let’s use a GUI:

import os
import tkinter as tk
from random import choice


template = "Acquistate merci per 1.500 euro + IVA 22%"

root = tk.Tk()
l = tk.Label(root, text="Insert template here (ex: How is 2 x 6?")
l.pack()
l['bg'] = "cyan"
v = tk.StringVar()
entry = tk.Entry(root, textvariable=v)
entry.pack(fill=tk.X)
v.set(template)
entry['bg'] = "cyan"

sub1 = tk.Label(root, text="Substitute")
sub1.pack()
v1 = tk.StringVar()
entry_sub1 = tk.Entry(root, textvariable=v1)
v1.set("Acquistate merci")
entry_sub1.pack(fill=tk.X)

with1 = tk.Label(root, text="Substitute with (separate alternatives with commas)")
with1.pack()
v2 = tk.StringVar()
entry_with1 = tk.Entry(root, textvariable=v2)
v2.set("Vendute merci")
entry_with1.pack(fill=tk.X)

def substitute():
	global template
	template = template.replace(entry_sub1.get(), entry_with1.get())
	text.delete("1.0", tk.END)
	text.insert("1.0", template)
	

bt = tk.Button(root, text="Substitute", command = substitute)
bt.pack()

l2 = tk.Label(root, text="Output")
l2.pack()
l2['bg'] = "gold"
text = tk.Text(root, wrap=tk.WORD)
text.pack(fill=tk.BOTH, expand=1)
text.insert("1.0", template)
text['bg'] = "gold"
root.mainloop()

This is the start, now we need to change it to make it really work.

That is how the window looks:

You can change also 1.500 for example with 3.000 or what you want. You can change also 22% with 10% or whatever you want. So, this is the basic code. In the next post we will check out how to create as many exercises as we want, different from each other and for different students.

Tkinter test for students

Tkinter articles

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.