Colleagues of the children in the first grade, the school assignment to learn the addition and subtraction within 20, and must have a carry or decrement.
Like 18-9=9 or 7+6=13.
Feel specialized to write a bunch of questions do not mean, so with Python+tkinter write a small program, and then use Cx_freeze package into EXE file, can randomly issue, and check whether the answer is correct. Novice on the road, inevitably encountered a lot of problems.
The code is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-from tkinter import *import randomdef main (): A=random.randint (1,18) if a &L T;10:b=random.randint (10-a,9) c=a+b result= "%d +%d"% (a, B) return (RESULT,C) if A>=10:b=random.ran Dint (a-9,9) c=a-b result= "%d-%d"% (a, B) return (RESULT,C) def check (): If Int (Result.get ()) ==int (t[1]): H= "Congratulations! Answer correctly "Hint.set (h) else:h=" Sorry! Answer the wrong! "Hint.set (h) result.set (") def Next (): Global T T=main () E.set (t[0]) result.set (') Hint.set (") master = Tk () master.geometry (' 500x500+100+100 ') master.title (" 20 plus minus the abdication operation ") Label (master,text=" title "). gr ID (row=0) label (master,text= "Answer"). Grid (row=1) label (master,text= "Info"). Grid (row=4) e = Stringvar () Result=stringvar () Hint=stringvar () e1 = Entry (master,textvariable=e) e2 = Entry (master,textvariable=result) E3 = Entry (Master, Textvariable=hint) T=main () E.set (t[0]) result.get () E1.grid (row=0, Column=1) E2.grid (row=1, Column=1) E3.grid (row=4, Column=1) Btn=button (master,text = ' OK ', command = check) Btn2=button (Master,text = ' Next question ', command = next) Btn.grid (row=2, column=2) Btn2.grid (row=2,column=4) Master.mainloop ()
The Python version is 3.3.5 and the Cx_freeze version used is 4.3.2
Packaging when the beginning is the use of Py2exe packaging, found that there is always a problem, the internet said py2exe to Python3 do not support the kind, so change cx_freeze.
The result is also encountered a variety of problems, fortunately basic can be found on the network, to the end, packaged into EXE, double-click EXE file, directly pop up a small black box, and then a flash over, execute exe under CMD file without any error ...
So the various search is what problem, can't find the reason, so changed the Python version, Cx_freeze version, or this situation. When you have to give up, inadvertently see someone else's program added such a piece of code:
Master.mainloop ()
Dead Horse when live horse medicine, the result is really play window ...
Then there is a small problem, that is, the program can be displayed normally, but at the same time there is a small black box is also displayed ... This is a good solution, when packaging exe, the cxfreeze command to specify parameters:
Cxfreeze calc_20.py--base-name=win32gui
Program Interface:
When you enter an answer in the answer box, you will determine whether the answer is correct:
If you answer an error, the contents of the answer box will be emptied to remind you to answer again:
The answer is correct, you can click the next question, you can continue to answer questions.
Interface is ugly, tkinter are now learning now, but they are still relatively satisfied, at least can see their progress. Come on.
Add and subtract carry operations within a Python applet---20