0

For a project, I need to create an educational program and I have chosen a quiz. I am having trouble making the program recognise that flag has been incremented to 3. Is it an issue with how I am incrementing flag or something else?. I am not too familiar with python (or tkinter for that matter) so please don't be too harsh.

from tkinter import *
from tkinter import ttk

root = Tk()

notebook = ttk.Notebook(root)

frame1 = ttk.Frame(notebook)
frame2 = ttk.Frame(notebook)
frame3 = ttk.Frame(notebook)

notebook.add(frame1, text="Q1")
notebook.add(frame2, text="Q2")
notebook.add(frame3, text="Q3")

def displayscore(total):
    score = Tk()
    score.geometry("760x450")
    score.title("Score")
    score.configure(background="#000138")
    Label(score, text="Your score is:", fg="Black", font="Arial 36").pack()
    Label(score, textvariable=total, fg="Black", font="Arial 36").pack()


def flagset():
    flag.set(flag.get() + 1)
    if flag == 3:
        displayscore(total)

def totalset():
    total.set(total.get() + 1)

def main():

    #Question 1

    def correct1():
        clear1()
        totalset()
        flagset()

    def clear1():
        list = frame1.grid_slaves()
        for l in list:
            l.destroy()
        Label(frame1, text="Where is the '7' in 1704?").grid(row=0)
        flagset()

    Label(frame1, text="Where is the '7' in 1704?").grid(row=0)

    Button(frame1, text="In the Thousands", command=clear1, width=15).grid(row=1, sticky=W)
    Button(frame1, text="In the Hundreds", command=correct1, width=15).grid(row=2, sticky=W)
    Button(frame1, text="In the Tens", command=clear1, width=15).grid(row=3, sticky=W)
    Button(frame1, text="In the Ones", command=clear1, width=15).grid(row=4, sticky=W)

    #Question2

    def correct2():
        clear2()
        totalset()
        flagset()

    def clear2():
        list = frame2.grid_slaves()
        for l in list:
            l.destroy()
        Label(frame2, text="What is 1704 in words?").grid(row=0)
        flagset()

    Label(frame2, text="What is 1704 in words?").grid(row=0)

    Button(frame2, text="One Million, Seven thousand and Four", command=clear2, width=35).grid(row=1, sticky=W)
    Button(frame2, text="One Hundred and Seventy Four", command=clear2, width=35).grid(row=2, sticky=W)
    Button(frame2, text="One Thousand and Seventy Four", command=clear2, width=35).grid(row=3, sticky=W)
    Button(frame2, text="One Thousand, Seven Hundred and Four", command=correct2, width=35).grid(row=4, sticky=W)

    #Question 3

    def correct3():
        clear3()
        totalset()
        flagset()

    def clear3():
        list = frame3.grid_slaves()
        for l in list:
            l.destroy()
        Label(frame3, text="Find ?: 3 x ? = 18").grid(row=0, column=2)
        flagset()

    Label(frame3, text="Find ?: 3 x ? = 18").grid(row=0, column=2)

    Button(frame3, text="6", command=correct3, width=15).grid(row=1, column=1, sticky=W)
    Button(frame3, text="7", command=clear3, width=15).grid(row=2, sticky=W)
    Button(frame3, text="8", command=clear3, width=15).grid(row=3, sticky=W)
    Button(frame3, text="9", command=clear3, width=15).grid(row=4, sticky=W)

flag = IntVar()
total = IntVar()

main()

notebook.pack()

root.mainloop()
B.Bacon
  • 3
  • 2
  • Welcome to SO. Please provide a **minimal** [mcve]. What this means is isolate your problem to the few lines (still runnable in isolation) which exhibit the problem you are facing. – jpp Aug 22 '18 at 10:06
  • Ok, I'll keep that in mind for next time. – B.Bacon Aug 22 '18 at 10:16

1 Answers1

0

flag is a tkinter IntVar, so flag == 3 will never be true.
Instead, use flag.get() == 3.

Also, when a correct answer is given, you increment flag twice, once in the clear function and once in the correct function.

Also also, you run flagset (from clear) before you run totalset, so with the last answer total will not be updated before you show the score.

fhdrsdg
  • 10,297
  • 2
  • 41
  • 62
  • Now that I've fixed 'flag', there seems to be an issue with displaying what score is. It just prints: "Your total is:" " ". Any help? – B.Bacon Aug 22 '18 at 10:21
  • Ah yes I missed that. The problem is that you make two instances of `Tk()`. Instead, for extra windows you should use `Toplevel()`. Read [this answer](https://stackoverflow.com/a/48045508/3714930) for more information on why you shouldn't have two instances of `Tk()`. – fhdrsdg Aug 22 '18 at 11:31