dimanche 5 juillet 2015

Quiz in python with GUI and involving text file

I am noob/newbie in Python and I want to write a code about Quiz competition , in which the following criteria should be followed:

  1. Take the question from a text file e.g. say "capitalsquiz%s.txt" and display it on a GUI(Python Tkinter) . The question contains questions of the form

    1. The capital of China is :

    A : Beijing B : Sydney C : Istanbul D : Moscow E : New Delhi

    A

    1. The capital of Russia is :

    A : Istanbul B : Sofia C : Moscow D : Helsinki E : Melbourne

    C

    1. and so on .......... upto 10 questions Also, display the choice as 5 choices.
  2. Take a student's name at random from another text file, say "students_list%s.txt" and display it on the same GUI(Python Tkinter). Say list has 10 students
  3. Create radio buttons(5 in number) for those 5 choices and display the choice/answer chosen in a text field
  4. Display the student's name, question and the choice chosen(from the radio button) as well as the correct choice in an output text file say, "write_it.txt" and record all choices chosen.

I have written a piece of the code. But it has lots of errors and doesn't quite follow all the criteria mentioned above. I know its a bit lengthy to be a question but any help from fellow programmers will be much appreciated and I will be ever grateful to you.

Program code

from Tkinter import *
import random

root = Tk()

root.title("PYTHON QUIZ")
root.geometry("700x500")
contents = []
items = []

def lines():
        dataFile = open("capitalsquiz%s.txt","r")

        for letters in dataFile:
            start = letters.find("<")
            end = letters.find(">", start)
            unit = letters[start:end]
            items.append(unit)
        return items
        empty[random.randint(0,len(empty)-1)]

ab=lines()

class Application(Frame):

    def __init__(self, master):

        Frame.__init__(self, master)
        self.grid()
        self.create_widgets()



    def create_widgets(self):

        with open("students_list.txt") as rnd:
            for line in rnd:
                line = line.strip()
                contents.append(line)

        with open("students_list.txt") as rnd:
            while line in rnd:
                print contents
                print "random name:", contents[random.randint(0,len(contents)-1)] # This random name


        Label(self,
              text = contents[random.randint(0,len(contents)-1)] # How to link with the above random name
              ).grid(row =0, column =10, sticky = W+E+N+S)


        Label(self,
              text = ab # Question to be taken from the random selection from text file
              ).grid(row =1, column = 11, sticky = W+E+N+S)


        Label(self,
              text = "Choose your Answer : "
              ).grid(row = 5, column = 0, sticky = W+E+N+S)


        Label(self,
              text = "Selecting one : "
              ).grid(row =6, column =0, sticky = W+E+N+S)


        Button()

        self.favourite = StringVar()

        Radiobutton(self,
                    text = "A",
                    variable = self.favourite,
                    value = "A",
                    command = self.update_text
                    ).grid(row = 7, column = 0, sticky = W+E+N+S)
        Radiobutton(self,
                    text = "B",
                    variable = self.favourite,
                    value = "B",
                    command = self.update_text
                    ).grid(row = 8, column = 0, sticky = W+E+N+S)
        Radiobutton(self,
                    text = "C",
                    variable = self.favourite,
                    value = "C",
                    command = self.update_text
                    ).grid(row = 9, column = 0, sticky = W+E+N+S)
        Radiobutton(self,
                    text = "D",
                    variable = self.favourite,
                    value = "D",
                    command = self.update_text
                    ).grid(row = 10, column = 0, sticky = W+E+N+S)
        Radiobutton(self,
                    text = "E",
                    variable = self.favourite,
                    value = "E",
                    command = self.update_text
                    ).grid(row = 11, column = 0, sticky = W+E+N+S)


        self.result = Text(self, width = 50, height =10, wrap = WORD)
        self.result.grid(row =15, column =10, columnspan =3)



    def update_text(self):

        message = "Your choice is : "
        message += self.favourite.get()
        print message



        text_file = open("write_it.txt","w")
        text_file.write(self.favourite.get())

        text_file.close()

        self.result.delete(0.0, END)
        self.result.insert(0.0 , message)


app = Application(root)

root.mainloop()




Aucun commentaire:

Enregistrer un commentaire