Skip to content

Instantly share code, notes, and snippets.

@clupasq
Created January 16, 2021 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clupasq/8d953140852b7afa298db9e1cdd8eb66 to your computer and use it in GitHub Desktop.
Save clupasq/8d953140852b7afa298db9e1cdd8eb66 to your computer and use it in GitHub Desktop.
[RO][4kids] Python script to only run an app if some equations are first solved.
# Usage:
#
# Run `some_app` only if 10 math puzzles are solved first:
# python3 compute_exercise.py && some_app
#
# Only 7 puzzles instead of the default 10:
# python3 compute_exercise.py 7 && some_app
#
# Only 5 puzzles, but force exit after 10min:
# python3 compute_exercise.py 5 && timeout 600 some_app
from tkinter import *
from tkinter import messagebox
from random import randint
import sys
font = ("Arial Bold", 20)
PUZZLE_COUNT = 10
try:
PUZZLE_COUNT = int(sys.argv[1])
except:
pass
PUZZLES = [(randint(0, 10), randint(0, 10))
for _ in range(PUZZLE_COUNT)]
def get_results():
res = []
for t in txts:
try:
res.append(int(t.get("1.0", "end-1c")))
except:
res.append(None)
return res
def is_correct():
for (x, y), r in zip(PUZZLES, get_results()):
if x + y != r:
return False
return True
def gata():
if is_correct():
window.destroy()
exit(0)
messagebox.showwarning("Nu e corect", """
Unul sau mai multe rezultate nu sunt corecte.
Te rog sa le verifici si sa le corectezi.
""")
window = Tk()
window.title("test")
lbl = Label(window, text="""Salut!
Vrei sa joci un joc?
Rezolva mai intai calculele de mai jos:""",
font=font)
lbl.grid(column=0, row=0, columnspan=2)
lrow = 1
txts = []
for p in PUZZLES:
x, y = p
l = Label(window, text=f"{x} + {y} = ",
font=font)
l.grid(column=0, row=lrow)
t = Text(window, width=10, height=1, font=font)
t.grid(column=1, row=lrow)
txts.append(t)
lrow += 1
btnSubmit = Button(window, text="Gata!", command=gata, font=font)
btnSubmit.grid(column=0, row=lrow, columnspan=2)
def on_closing():
if messagebox.askokcancel("Quit", "Chiar vrei sa iesi?"):
window.destroy()
exit(5)
window.protocol("WM_DELETE_WINDOW", on_closing)
window.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment