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()]

You forgot code tags!

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()]

Looks like you are only asking 2 questions.

=====>  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)

And, you might consider a dictionary for the cities, using the number, (1,2, etc) as the key pointing to the name.

if number ==1:
            self.label.config(text = 'What is the population of Philadelphia? ')
##
##   replace with something along the lines of
        self.label.config(text = 'What is the population of %s? ' % (city_dict[number]))

Yeah i was only checking it for 2 questions for now and i was gonna change it to 10 later. For some reason the accumulator doesn't work though, it just keeps repeating questions.

This may be handy:

>>> city_dict={'Philadelphia':123456,'Madrid':234567}
>>> import random
>>> city=random.choice(city_dict.keys())
>>> city
'Madrid'
>>> city_dict[city]
234567
>>>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.