[go: up one dir, main page]

Menu

Diff of /math.py [000000] .. [r1]  Maximize  Restore

Switch to side-by-side view

--- a
+++ b/math.py
@@ -0,0 +1,430 @@
+from array import array
+import wx
+import random
+import wx.lib.dragscroller
+#import wx.html as html
+import  sqlite3 as lite
+
+class MinFrame(wx.Frame):
+    def __init__(self,parent,id, vsize):
+        wx.Frame.__init__(self,parent, id,  size=vsize)
+        self.parent =parent
+        self.SetMinSize(vsize)
+        
+class AnswerWindow(wx.ScrolledWindow):
+    def __init__(self,parent,count):
+        self.parent = parent
+        id = -1
+        wx.ScrolledWindow.__init__(self, parent,id)
+        self.Bind(wx.EVT_PAINT, self.OnPaint)
+        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
+        self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)
+        self.SetScrollbars(1, 1, 1000, 48*count, 0, 0)
+        self.SetBackgroundColour("WHITE")
+        parent.SetTitle("Answer Sheet")
+        
+    def OnRightDown(self, event):
+        self.scroller.Start(event.GetPosition())
+
+    def OnRightUp(self, event):
+        self.scroller.Stop()
+        
+    def OnPaint(self,event):
+        dc = wx.PaintDC(self)
+        self.DoPrepareDC(dc)
+        pen = wx.Pen(wx.BLACK, 1)
+        dc.SetPen(pen)
+        dc.Clear()
+        te = dc.GetTextExtent("Answer  WorkSheet")
+        w,h = self.GetSize()
+        font = dc.GetFont()
+        dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
+        dc.DrawText("Answer  WorkSheet",(w - te[0])/2,20)
+        #dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
+        dc.SetFont(font)
+        dc.DrawLine(20, 40, w -20, 40)
+        dc.DrawLine(20, 100, 620, 100)
+        dc.DrawLine(20, 100, 20, 140)
+        dc.DrawText("No.",40,120)
+        dc.DrawLine(80, 100, 80, 140)
+        dc.DrawText("Problem",100,120)
+        dc.DrawLine(220, 100, 220, 140)
+        dc.DrawText("Your Answer",260,120)
+        dc.DrawLine(360, 100, 360, 140)
+        dc.DrawText("Correct Answer",400,120)
+        dc.DrawLine(520, 100, 520, 140)
+        dc.DrawText("Result",560,120)
+        dc.DrawLine(620, 100, 620, 140)
+        dc.DrawLine(20, 140, 620, 140)
+        count = self.parent.mywindow.count
+        win = self.parent.mywindow
+        #con = lite.connect('math')
+        #cur = con.cursor()
+
+        for i in range(win.count):
+            dc.DrawText(str(i +1),40,160+40*i)
+            num1 = win.num_first[i]
+            num2 = win.num_sec[i]
+            op = win.oper
+            dc.DrawText(str(num1)+op+str(num2),100,160+40*i)
+            if op == "+":
+                correct_answer = num1 + num2
+            if op == "-":
+                correct_answer = num1 - num2
+            if op == "x":    
+                correct_answer = num1 * num2
+            if op == u"÷" :   
+                correct_answer = num1 / num2
+                
+            dc.DrawText(str(correct_answer),420, 160+40*i)   
+            dc.DrawLine(20, 180 +i*40, 620, 180 +i*40)
+            your_answer =win.d[10+i].GetValue()
+            dc.DrawText(str(your_answer),240, 160+40*i)
+            #yes =u'√'
+            yes = "OK"
+            no="X"
+            new_answer = your_answer.lstrip()
+            your_answer1 = new_answer.rstrip()
+            result = 0
+            if your_answer1 != "":
+                answer = int(your_answer1)
+                if int(your_answer1 )== int(correct_answer):
+                    result = 1
+                    dc.SetTextForeground("GREEN")
+                    dc.DrawText(yes,540, 160+40*i)
+                    dc.SetTextForeground("BLACK")
+                else:
+                    dc.SetTextForeground("RED")
+                    dc.DrawText(no,540, 160+40*i)
+                    dc.SetTextForeground("BLACK")
+            else:
+                answer = 0;
+                dc.SetTextForeground("RED")
+                dc.DrawText(no,540, 160+40*i)
+                dc.SetTextForeground("BLACK")
+            problem = str(num1)+op+str(num2)
+            #cur.execute('insert into answer_sheet(problem,answer,result) values(?,?,?)',(problem,answer,result))
+            
+            
+        dc.DrawLine(20, 140 , 20, 140 + count*40)
+        dc.DrawLine(80, 140 , 80, 140 + count*40)
+        dc.DrawLine(220, 140 , 220, 140 + count*40)
+        dc.DrawLine(360, 140 , 360, 140 + count*40)
+        dc.DrawLine(520, 140 , 520, 140 + count*40)
+        dc.DrawLine(620, 140 , 620, 140 + count*40)
+        # con.commit()
+        #cur.close()
+        #con.close()
+
+class MyWindow(wx.ScrolledWindow):
+    def __init__(self,parent,ops,kind,count):
+        
+        self.parent = parent
+        id = -1
+        wx.ScrolledWindow.__init__(self, parent,id)
+        self.Bind(wx.EVT_PAINT, self.OnPaint)
+        self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
+        self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)
+        self.SetScrollbars(1, 1, 1000, 40*count, 0, 0)
+        self.SetBackgroundColour("WHITE")
+        self.scroller = wx.lib.dragscroller.DragScroller(self)
+        self.SetOps(ops)
+        parent.SetTitle(self.oper_str +"  Worksheet")
+        self.SetOpsType(kind)
+        self.count = count
+        self.num_first = array('i')
+        self.num_sec = array('i')
+        self.answer = array('i')
+        self.d = {}
+        for i in range(self.count):
+            num1 = random.randint(self.start1,self.end1)
+            num2 = random.randint(self.start2,self.end2)
+            if num1 < num2:
+                temp = num1
+                num1 = num2
+                num2 = temp
+            if self.ops == 3:
+                if num2 ==0:
+                   num2 =1   
+                self.num_first.append(num1*num2)
+            else:
+                self.num_first.append(num1)
+            self.num_sec.append(num2)
+        for i in range(self.count/5):
+            for column in range(5):   
+                text =  wx.TextCtrl(self,10+5*i +column,"",wx.Point( column*200+10,100*i +150),wx.Size(40,20))
+                self.d[10+5*i +column] = text
+                      
+        self.answer = wx.Button(self, 12, "Get Answers", wx.Point(410 , 20*self.count +100))
+        self.close = wx.Button(self, 13, "Close", wx.Point(410 , 20*self.count +140))
+        wx.EVT_BUTTON(self, 12, self.OnAnswer)
+        wx.EVT_BUTTON(self,13,self.OnClose)
+        
+
+    def OnAnswer(self,event):
+        frame2 = MinFrame(self,-1,(720,620))
+        frame2.mywindow = self
+        panel1 = AnswerWindow(frame2,self.count)
+        frame2.Show(1)
+        
+    def OnClose(self,event):
+        self.parent.Destroy()
+        
+    def SetOps(self,ops):
+        self.ops = ops
+        if ops == 0:
+            self.oper = "+"
+            self.oper_str = "Addition"
+        if ops == 1:
+            self.oper = "-"
+            self.oper_str = "Subtraction"
+        if ops == 2:
+            self.oper = "x"
+            self.oper_str = "Multiplication"
+        if ops == 3:
+            #self.oper = "/"
+            self.oper = u"÷"
+            self.oper_str = "Division"
+            
+    def SetOpsType(self,kind):
+        if kind == 0:
+            self.start1 = 0
+            self.start2 = 0
+            self.end1 = 12
+            self.end2 = 12
+            
+        elif kind == 1:
+            self.start1 = 10
+            self.start2 = 0
+            self.end1   = 99
+            self.end2 = 9
+            
+        elif kind == 2:
+            self.start1 = 10
+            self.start2 = 10
+            self.end1   = 99
+            self.end2   = 99
+            
+        elif kind == 3:
+            self.start2 = 0
+            self.start1 = 100            
+            self.end1   = 999
+            self.end2   = 9
+            
+        elif kind == 4:
+            self.start1 = 100
+            self.start2 = 10
+            self.end1   = 999
+            self.end2   = 99
+            
+        else:
+            self.start1 = 100
+            self.start2 = 100
+            self.end1   = 999
+            self.end2   = 999    
+        
+    def OnPaint(self,event):
+        dc = wx.PaintDC(self)
+        self.DoPrepareDC(dc)
+        pen = wx.Pen(wx.BLACK, 1)
+        dc.SetPen(pen)
+        dc.Clear()
+        te = dc.GetTextExtent(self.oper_str+"  WorkSheet")
+        w,h = self.GetSize()
+        dc.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.BOLD))
+        dc.DrawText(self.oper_str+"  WorkSheet",(w - te[0])/2,20)
+        dc.DrawLine(20, 40, w -20, 40)
+        for i in range(self.count/5):
+            for column in range(5):
+                index = i*5 + column
+                num1 = self.num_first[index]
+                num2 = self.num_sec[index]
+                
+                len_diff = len(str(num1)) -len(str(num2))
+                if len_diff > 0:
+                    sec = " "*len_diff + str(num2)
+                    first =str(num1)
+                    space = len_diff
+                else:
+                    first = " "+str(num1)
+                    sec = str(num2)
+                    space =2
+                pen = wx.Pen(wx.BLACK, 1) 
+                dc.SetPen(pen)
+                dc.SetFont(wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD))
+                dc.DrawText(str(i*5 + column +1)+".",( column*200 +10),100*i +80)
+                dc.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL))
+                dc.DrawText(first,(30 + column*200) ,100*i +100)
+                dc.DrawText(self.oper+sec,(column*200 +30-1-space),100*i +120)
+                dc.SetTextForeground(wx.BLACK)
+                dc.DrawLine(10+column*200,100*i +140,50 + column*200,100*i +140)
+                
+    def OnRightDown(self, event):
+        self.scroller.Start(event.GetPosition())
+
+    def OnRightUp(self, event):
+        self.scroller.Stop()
+
+
+class MyFrame(wx.Frame):
+    def __init__(self, parent, id, title):
+        wx.Frame.__init__(self, parent, id, title, size=(490, 450))
+
+        panel = wx.Panel(self, -1)
+        vbox = wx.BoxSizer(wx.VERTICAL)
+        font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT)
+
+        # Add panel 1
+        panel1 = wx.Panel(panel, -1)
+        vbox1 = wx.BoxSizer(wx.VERTICAL)
+        title = wx.StaticText(panel1, -1,"Math Worksheet")
+        title.SetFont(font)
+        vbox1.Add(title,0,wx.ALIGN_CENTRE)
+        vbox1.Add((-1, 40))
+        line = wx.StaticLine(panel1, -1, (25, 50), (400,1) )
+        vbox1.Add(line,1,wx.EXPAND|wx.LEFT|wx.RIGHT,20)
+        vbox1.Add((-1, 40))
+        panel1.SetSizer(vbox1)
+        vbox.Add(panel1, 0, wx.BOLD|wx.ALIGN_CENTRE | wx.TOP, 9)
+        vbox.Add((-1, 20))
+
+        # Add panel 2
+        panel2 = wx.Panel(panel,-1)
+        hbox2 = wx.BoxSizer(wx.HORIZONTAL)
+        panel2.SetSizer(hbox2)
+        date = wx.StaticText(panel2, -1, 'Date')
+        date.SetFont(font)
+        hbox2.Add(date,0,wx.RIGHT,20)
+        self.todate = wx.TextCtrl(panel2, -1)
+        hbox2.Add(self.todate,1)
+        vbox.Add(panel2,1,wx.EXPAND|wx.LEFT|wx.RIGHT,20)
+        
+        # Add panel 3
+        panel3 = wx.Panel(panel, -1)
+        hbox3 = wx.BoxSizer(wx.HORIZONTAL)
+        panel3.SetSizer(hbox3)
+        op = wx.StaticText(panel3, -1, 'Operation')
+        op.SetFont(font)
+        self.sampleList = ['Addition', 'Subtraction', 'Multiplication', 'Division']
+        op_name = wx.ComboBox(panel3, 30, "", wx.Point(0, 0), wx.Size(95, -1),
+                   self.sampleList, wx.CB_DROPDOWN)
+        hbox3.Add(op,0,wx.RIGHT,20)
+        op_font = op_name.GetFont()
+        op_font.SetWeight(wx.BOLD)
+        op_name.SetFont(op_font)
+        hbox3.Add(op_name,1)
+        vbox.Add(panel3,1,wx.EXPAND|wx.LEFT|wx.RIGHT,20)
+        vbox.Add((-1, 10))
+        
+        # Add panel 4
+        panel4 = wx.Panel(panel,-1)
+        hbox4 = wx.BoxSizer(wx.HORIZONTAL)
+        prob1 = wx.StaticBox(panel4,-1,'Type of problem')
+        prob1.SetFont(font)
+        sizer11 = wx.StaticBoxSizer(prob1,orient=wx.HORIZONTAL)
+        hbox4.Add(sizer11,1,wx.RIGHT,20)
+        btn_777 = wx.RadioButton(panel4, 1, 'upto 12')
+        font_777 = btn_777.GetFont()
+        font_777.SetWeight(wx.BOLD)
+        btn_777.SetFont(font_777)
+
+        btn_787 = wx.RadioButton(panel4, 2, '2 by 1')
+        font_787 = btn_787.GetFont()
+        font_787.SetWeight(wx.BOLD)
+        btn_787.SetFont(font_787)
+
+        btn_pil = wx.RadioButton(panel4, 3, '2 by 2')
+        font_pil = btn_pil.GetFont()
+        font_pil.SetWeight(wx.BOLD)
+        btn_pil.SetFont(font_pil)
+        
+        sizer11.Add(btn_777)
+        sizer11.Add(btn_787)
+        sizer11.Add(btn_pil)
+        #sizer11.Add(wx.RadioButton(panel4, 4, ''))
+        #sizer11.Add(wx.RadioButton(panel4, 5, '3 by 3'))
+        #sizer11.Add(wx.RadioButton(panel4, 6, '4 by 4'))
+        panel4.SetSizer(hbox4)
+        vbox.Add(panel4,1,wx.EXPAND|wx.LEFT|wx.RIGHT,20)
+        vbox.Add((-1, 10))
+
+        # Add panel 5
+        panel5 = wx.Panel(panel,-1)
+        hbox5 = wx.BoxSizer(wx.HORIZONTAL)
+        prob2 = wx.StaticBox(panel5,5,'Number of problems ')
+        prob2.SetFont(font)
+        sizer21 = wx.StaticBoxSizer(prob2,orient=wx.HORIZONTAL)
+        hbox5.Add(sizer21,1,wx.RIGHT,20)
+        panel5.btn1 = wx.RadioButton(panel5, 7, '10')
+        sizer21.Add(panel5.btn1)
+        sizer21.Add(wx.RadioButton(panel5, 8, '20'))
+        sizer21.Add(wx.RadioButton(panel5, 9, '30'))
+        sizer21.Add(wx.RadioButton(panel5, 10, '40'))
+        sizer21.Add(wx.RadioButton(panel5, 11, '50'))
+        panel5.SetSizer(hbox5)
+        vbox.Add(panel5,1,wx.EXPAND|wx.LEFT|wx.RIGHT,20)
+
+        # Add panel 6
+        panel6 = wx.Panel(panel,-1)
+        hbox6 = wx.BoxSizer(wx.HORIZONTAL)
+        creater = wx.Button(panel6, 20, "Create")
+        leaver  = wx.Button(panel6, 21, "Quit")
+        hbox6.Add((191, -1), 1, wx.EXPAND | wx.ALIGN_RIGHT)
+        hbox6.Add(creater,1,wx.ALIGN_RIGHT|wx.RIGHT,10)
+        hbox6.Add(leaver,1,wx.ALIGN_RIGHT)
+        panel6.SetSizer(hbox6)
+        vbox.Add(panel6,1,wx.EXPAND|wx.RIGHT,20)
+        panel.SetSizer(vbox)
+
+        #Add event handlers
+        self.Bind(wx.EVT_COMBOBOX, self.OnSelect)
+        wx.EVT_RADIOBUTTON(self,1,self.OnType)
+        wx.EVT_RADIOBUTTON(self,2,  self.OnType)
+        wx.EVT_RADIOBUTTON(self, 3, self.OnType)
+        #wx.EVT_RADIOBUTTON(self, 4, self.OnCount)
+        #wx.EVT_RADIOBUTTON(self, 5, self.OnCount)
+        #wx.EVT_RADIOBUTTON(self, 6, self.OnCount)
+        wx.EVT_RADIOBUTTON(self, 7, self.OnCount)
+        wx.EVT_RADIOBUTTON(self, 8, self.OnCount)
+        wx.EVT_RADIOBUTTON(self, 9, self.OnCount)
+        wx.EVT_RADIOBUTTON(self, 10, self.OnCount)
+        wx.EVT_RADIOBUTTON(self, 11, self.OnCount)
+        wx.EVT_BUTTON(self, 20, self.OnCreate)
+        wx.EVT_BUTTON(self, 21, self.OnQuit)
+        self.ops = 0
+        self.count = 10
+        self.type = 0
+        self.Centre()
+        self.Show(True)
+        
+    def OnQuit(self,event):
+        self.Destroy()
+        
+    def OnCreate(self,event):
+        frame1 = MinFrame(self,-1,(1020,620))
+        #self.edname.AppendText(" Test Id %d\n" %event.GetId())
+        panel1 = MyWindow(frame1,self.ops,self.type,self.count)
+        frame1.Show(1)
+        #con = lite.connect('math')
+        #cur = con.cursor()
+        #grade = 1
+        #cur.execute('insert into student(name,grade) values(?,?)',(self.edname.GetValue(),grade))
+        #cur.execute('insert into test(op,count,correct,date) values(?,?,?,?)',(self.ops,self.count,0,self.todate.GetValue()))
+        #con.commit()
+        #cur.close()
+        #con.close()
+    def OnCount(self,event):
+        id = event.GetId()
+        self.count = 10*(id-6) #Convert from id to real count
+                
+    def OnType(self,event):
+        self.type = event.GetId()-1
+        
+    def OnSelect(self,event):
+        self.ops = event.GetSelection()
+        
+        
+app = wx.PySimpleApp()
+frame = MyFrame(None, -1,"MATH Worksheet")
+frame.Show(1)
+app.MainLoop()