Reputation: 13
On the python debugger the score is read first and I have tried globalising it to see if anything would work. Also I know the code is messy and there are better ways of doing it but I just want to solve this problem first. This is my first time using stack overflow so I don't know how to link the zip folders to the question if you are wanting to run the code. The folders contain PNG images which are named what is in the array. That bit works without any problems. This is also my first time using tkinter.
global score
score = 0
from tkinter import *
from PIL import ImageTk, Image
from random import randint
import os
root = Tk()
root.title("Flashcard")
#root.iconbitmap('c:/')
root.geometry("500x500")
global dir_path
dir_path = os.path.dirname(os.path.realpath(__file__))
def score0():
score = 0
def scoreadd():
score = score+1
def scoreminus():
score = score-1
def checkcorrectcs(comp_questions, random_number,):
answer = answer_input.get()
answer = answer.replace(" ", "")
if answer.lower() == comp_questions[random_number]:
checked = "Correct"
scoreadd()
else:
checked = "Incorrect it was " + comp_questions[random_number].title()
scoreminus()
answer_label.config(text=checked)
def checkcorrectmaths(maths, random_number2,):
answer = answer_input.get()
answer = answer.replace(" ", "")
if answer.lower() == math_questions[random_number2]:
checked = "Correct"
scoreadd()
else:
checked = "Incorrect it was " + math_questions[random_number2].title()
scoreminus()
answer_label.config(text=checked)
def checkcorrectph(physics_questions, random_number3,):
answer = answer_input.get()
answer = answer.replace(" ", "")
if answer.lower() == physics_questions[random_number3]:
checked = "Correct"
scoreadd()
else:
checked = "Incorrect it was " + physics_questions[random_number3].title()
scoreminus()
answer_label.config(text=checked)
def checkcorrectbio(biology_questions, random_number4,):
answer = answer_input.get()
answer = answer.replace(" ", "")
if answer.lower() == biology_questions[random_number4]:
checked = "Correct"
scoreadd()
else:
checked = "Incorrect it was " + biology_questions[random_number4].title()
scoreminus()
answer_label.config(text=checked)
def checkcorrectchem(chemistry_questions, random_number5,):
answer = answer_input.get()
answer = answer.replace(" ", "")
if answer.lower() == chemistry_questions[random_number5]:
checked = "Correct"
scoreadd()
else:
checked = "Incorrect it was " + chemistry_questions[random_number5].title()
scoreminus()
answer_label.config(text=checked)
#Computer science function
def computer_science():
hide_any_windows()
computer_science_window.pack(fill="both",expand=1)
title = Label(computer_science_window, text="Computer science").pack()
comp_questions = ["and", "binary", "denary", "hexadecimal", "or"]
random_number = randint(0, len(comp_questions)-1)
random_comp_question = f"{dir_path}/ComputerScienceQuestionBank/" + comp_questions[random_number] +".png"
global comp_question_photo
comp_question_photo = ImageTk.PhotoImage(Image.open(random_comp_question))
show_comp_question = Label(computer_science_window, image=comp_question_photo)
show_comp_question.pack(pady=15)
#answer box
global answer_input
answer_input = Entry(computer_science_window, font=("Comic Sans", 20))
answer_input.pack(pady = 15)
confirm_button = Button(computer_science_window, text ="Confirm", command=lambda: checkcorrectcs(comp_questions, random_number))
confirm_button.pack(pady=5)
random_button = Button(computer_science_window, text= "New question", command=computer_science)
random_button.pack(pady=10)
global answer_label
answer_label = Label(computer_science_window, text="")
answer_label.pack(pady=15)
#Maths function
def maths():
hide_any_windows()
maths_window.pack(fill="both",expand=1)
title = Label(maths_window, text="Maths").pack()
math_questions = ["144", "test2"]
random_number2 = randint(0, len(math_questions)-1)
random_math_question = f"{dir_path}/MathQuestionBank/" + math_questions[random_number2] +".png"
global math_question_photo
math_question_photo = ImageTk.PhotoImage(Image.open(random_math_question))
show_math_question = Label(maths_window, image=math_question_photo)
show_math_question.pack(pady=15)
#answer box
global answer_input
answer_input = Entry(maths_window, font=("Comic Sans", 20))
answer_input.pack(pady = 15)
confirm_button = Button(maths_window, text ="Confirm", command=lambda: checkcorrectcs(math_questions, random_number2))
confirm_button.pack(pady=5)
random_button = Button(maths_window, text= "New question", command=maths)
random_button.pack(pady=10)
global answer_label
answer_label = Label(maths_window, text="")
answer_label.pack(pady=15)
#Physics function
def physics():
hide_any_windows()
physics_window.pack(fill="both",expand=1)
title = Label(physics_window, text="Maths").pack()
physics_questions = ["9.81", "test3", "quarks", "speedoflight"]
random_number3 = randint(0, len(physics_questions)-1)
random_physics_question = f"{dir_path}/PhysicsQuestionBank/" + physics_questions[random_number3] +".png"
global physics_question_photo
physics_question_photo = ImageTk.PhotoImage(Image.open(random_physics_question))
show_physics_question = Label(physics_window, image=physics_question_photo)
show_physics_question.pack(pady=15)
#answer box
global answer_input
answer_input = Entry(physics_window, font=("Comic Sans", 20))
answer_input.pack(pady = 15)
confirm_button = Button(physics_window, text ="Confirm", command=lambda: checkcorrectph(physics_questions, random_number3))
confirm_button.pack(pady=5)
random_button = Button(physics_window, text= "New question", command=physics)
random_button.pack(pady=10)
global answer_label
answer_label = Label(physics_window, text="")
answer_label.pack(pady=15)
#Biology function
def biology():
hide_any_windows()
biology_window.pack(fill="both",expand=1)
title = Label(biology_window, text="Biology").pack()
biology_questions = ["test3", "test4"]
random_number4 = randint(0, len(biology_questions)-1)
random_biology_question = f"{dir_path}/BiologyQuestionBank/" + biology_questions[random_number4] +".png"
global biology_question_photo
biology_question_photo = ImageTk.PhotoImage(Image.open(random_biology_question))
show_biology_question = Label(biology_window, image=biology_question_photo)
show_biology_question.pack(pady=15)
#answer box
global answer_input
answer_input = Entry(biology_window, font=("Comic Sans", 20))
answer_input.pack(pady = 15)
confirm_button = Button(biology_window, text ="Confirm", command=lambda: checkcorrectbio(biology_questions, random_number4))
confirm_button.pack(pady=5)
random_button = Button(biology_window, text= "New question", command=biology)
random_button.pack(pady=10)
global answer_label
answer_label = Label(biology_window, text="")
answer_label.pack(pady=15)
#Chemistry function
def chemistry():
hide_any_windows()
chemistry_window.pack(fill="both",expand=1)
title = Label(chemistry_window, text="Chemistry").pack()
chemistry_questions = ["jameschadwick", "loweractivationenergy", "mendeleev", "postive", "protondonors",]
random_number5 = randint(0, len(chemistry_questions)-1)
random_chemistry_question = f"{dir_path}/ChemistryQuestionBank/" + chemistry_questions[random_number5] +".png"
global chemistry_question_photo
chemistry_question_photo = ImageTk.PhotoImage(Image.open(random_chemistry_question))
show_chemistry_question = Label(chemistry_window, image=chemistry_question_photo)
show_chemistry_question.pack(pady=15)
#answer box
global answer_input
answer_input = Entry(chemistry_window, font=("Comic Sans", 20))
answer_input.pack(pady = 15)
confirm_button = Button(chemistry_window, text ="Confirm", command=lambda: checkcorrectchem(chemistry_questions, random_number5))
confirm_button.pack(pady=5)
random_button = Button(chemistry_window, text= "New question", command=chemistry)
random_button.pack(pady=10)
global answer_label
answer_label = Label(chemistry_window, text="")
answer_label.pack(pady=15)
def hide_any_windows():
for widget in computer_science_window.winfo_children():
widget.destroy()
for widget in maths_window.winfo_children():
widget.destroy()
for widget in physics_window.winfo_children():
widget.destroy()
for widget in biology_window.winfo_children():
widget.destroy()
for widget in chemistry_window.winfo_children():
widget.destroy()
computer_science_window.pack_forget()
maths_window.pack_forget()
physics_window.pack_forget()
biology_window.pack_forget()
chemistry_window.pack_forget()
#menu
my_menu = Menu(root)
root.config(menu=my_menu)
#Subjects for the menu
subjects_menu = Menu(my_menu)
my_menu.add_cascade(label = "Subjects", menu=subjects_menu)
subjects_menu.add_command(label="Computer science", command=computer_science)
subjects_menu.add_command(label="Maths", command=maths)
subjects_menu.add_command(label="Physics", command=physics)
subjects_menu.add_command(label="Biology", command=biology)
subjects_menu.add_command(label="Chemistry", command=chemistry)
subjects_menu.add_separator()
subjects_menu.add_command(label="Exit", command=root.quit)
#Making the window
computer_science_window = Frame(root, width=500, height=500)
maths_window = Frame(root, width=500, height=500)
physics_window = Frame(root, width=500, height=500)
biology_window = Frame(root, width=500, height=500)
chemistry_window = Frame(root, width=500, height=500)
# all_windows = [computer_science_window, maths_window, physics_window, biology_window, chemistry_window]
root.mainloop()
Upvotes: 1
Views: 169
Reputation: 36682
You can either add a global declaration for the variable score
in every function that modifies it. (easy and expedient, but not recommended)
score = 0
def score0():
global score
score = 0
def scoreadd():
global score
score = score + 1
def scoreminus():
global score
score = score - 1
or maybe create a small Score class
to keep score: (recommended)
class Score:
def __init__(self):
self.score = 0
def increment(self):
self.score += 1
def decrement(self):
self.score -= 1
def reset(self):
self.score = 0
that you can use as follows:
the_score = Score()
the_score.increment()
the_score.decrement()
the_score.reset()
Upvotes: 1