|
First Summer 2006 |
Today's lab will be about the material in Chapter 10 in the Python text.
This is the program you should try out:
This is where you can find information about the
Tkinter module:
This is how the page looks:http://www.pythonware.com/library/tkinter/introduction/
Here's the code of the program again:
from Tkinter import *
import random
class Application(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.grid()
self.good = 0
self.total = 0
self.create_widgets()
def create_widgets(self):
self.n1 = random.randrange(-50, 50)
self.n2 = random.randrange(-50, 50)
self.q = StringVar()
self.question = Label(self, textvariable=self.q)
self.q.set("What is" + str(self.n1) + " + " + str(self.n2) + "?")
self.question.grid(row=0,column=0,columnspan=2)
self.label = Label(self, text="Enter your answer here: ")
self.label.grid(row=1,column=0)
self.user = Entry(self)
self.user.grid(row=1,column=1)
self.anotherLabel = Label(self, text="When done please press")
self.anotherLabel.grid(row=2,column=0)
self.submit_bttn = Button(self,text="Submit", command=self.reveal)
self.submit_bttn.grid(row=2,column=1)
self.v = StringVar()
self.answer = Label(self, textvariable=self.v)
self.answer.grid(row=3,column=0,columnspan=2)
self.v.set("The game so far: 0 out of 0 correct.")
def reveal(self):
guess = self.user.get()
if int(guess) == self.n1 + self.n2:
self.good += 1;
self.total += 1;
self.v.set("The game so far: " + str(self.good) + " out of " + str(self.total) + " correct.")
self.n1 = random.randrange(-50, 50)
self.n2 = random.randrange(-50, 50)
self.q.set("What is" + str(self.n1) + " + " + str(self.n2) + "?")
self.user.delete(0, END)
root= Tk()
root.title("Quiz program")
root.geometry("250x150")
app = Application(root)
root.mainloop()