I'm trying to make quiz GUI. The GUI has one button and an entry box. The user presses the button and it asks a random question from using the world database. After 10 questions, it tells you the number correct and incorrect. I'm having a lot of trouble with the code. Here's what i have so far:
[from tkinter import *
import sqlite3
import random
class worldGUI:
# constructor
def __init__(self):
incorrect = 0
correct = 0
win = Tk()
win.title ('World DB')
self.label = Label(win, text = 'This is a geography quiz. ')
self.label.pack()
self.entry = Entry(win)
# use bind method to connect <Return> event to callback
self.entry.bind('<Return>')
self.entry.pack()
# use config method to set button's command option
if (correct + incorrect) < 2:
self.button = Button(win, text = 'Next Question')
self.button.config(command = self.action)
self.button.pack()
if self.action == None:
incorrect += 1
else:
correct += 1
else:
self.label.config(text = 'Number correct %i' %correct)
#display window, wait for events to occur
win.mainloop()
# callback method for button
def action(self):
conn = sqlite3.connect('worldDB')
cursor = conn.cursor()
number = random.randrange(1,3)
if number ==1:
self.label.config(text = 'What is the population of Philadelphia? ')
cityName = self.entry.get()
cursor.execute("select name from city where population = ? and name = 'Philadelphia'", (cityName,))
answer = cursor.fetchone()
return answer
else:
self.label.config(text = 'What is the population of Madrid? ')
cityName = self.entry.get()
cursor.execute("select name from city where population = ? and name = 'Madrid'", (cityName,))
answer = cursor.fetchone()
return answer
# callback method for entry
# note – event parameter is required, but use is optional
# create instance of class to start GUI program running
worldGUI()]