﻿from Tkinter import *
from tkMessageBox import *
import sqlite3
import random

def QueryDB( Wid ):

    conn = sqlite3.connect('wordlist.db')
    c = conn.cursor()
    if( Wid == 0):
         c.execute('select * from words')
         for row in c:
             print row
    else:
          t = (Wid,)
          c.execute('select * from words where id=?',t)
          entry = c.fetchone()
#          print "Word = %s"%(entry[1])
#          print "Description = %s"%(entry[2])
          

    c.close()
    conn.close()
    return (entry[1],entry[2])


class App:
	def __init__(self, master):
    		self.frame = Frame(master)
	    	self.frame.pack()

		self.AskQuestion( )

		
	
	def AddLabel( self,frame, textToDisplay ):
		self.label = Label(frame, text=textToDisplay)
		self.label.pack()

	def AddRadioButtons( self, frame, options ):
		self.buttons = 4*[None]
		
		self.v = IntVar()
		
		self.buttons[0] =  Radiobutton(frame,text=options[0] , variable=self.v,value=0,command=lambda:self.CheckOption(0)) 
		self.buttons[1] =  Radiobutton(frame,text=options[1] , variable=self.v,value=1,command=lambda:self.CheckOption(1)) 
		self.buttons[2] =  Radiobutton(frame,text=options[2] , variable=self.v,value=2,command=lambda:self.CheckOption(2)) 
		self.buttons[3] =  Radiobutton(frame,text=options[3] , variable=self.v,value=3,command=lambda:self.CheckOption(3)) 

		for i in range(0,4):
			self.buttons[i].pack(anchor='w')
		

	def CheckOption( self, i ):
		if self.answer == i:
			showinfo( "correct")
		else:
			showerror( "Incorrect, correct option is %d"%(self.answer))

		
	
	def AskQuestion( self ):
		options = 4*[None]
		self.answer = 0

		while( True ):
        		wd = QueryDB( random.randint(1,698) )
        		answer = random.randint(0,3)

        		question = wd[0]
       			options[answer] = wd[1]

        		l = [0,1,2,3]
        		l.remove(answer)
    
        		for i in l:
            			wd = QueryDB( random.randint(1,698) )
            			options[i] = wd[1]

		self.AddLabel( self.frame, question )
		self.AddRadioButtons( frame, options )

root = Tk()

app = App(root)

root.mainloop()


	#	b = Radiobutton(master, text=text,variable=v, value=mode,command=lambda:CheckOption(mode))
		




