how to create a scoring system - python

I'm trying to make my scoring system work. it keeps forgetting after I have changed the score once I go back into the class as it is set as 0. How would I change this so the score changes as appropriate?
code:
import random
from tkinter import *
class TimesTableGUI:
def __init__(self, parent, score = 0):
self.score = score
#chooses a random number
num1 = random.randint(1,10)
num2 = random.randint(1,10)
self.answer = num1 * num2
#making labels
entry_label = Label(parent, text = str(num1) + " * " + str(num2))
entry_label.grid(row = 0, column = 0)
self.e1 = Entry(parent, width = 5)
self.e1.grid(row = 0, column = 1)
b1 = Button(parent, text = "Check Answer", command = self.check_answer)
b1.grid(row = 1, column = 0)
b2 = Button(parent, text = "Next", command = self.new_question)
b2.grid(row = 1, column = 1)
self.b1_output = Label(parent, text = self.score)
self.b1_output.grid(row = 2, column = 0)
def check_answer(self): #callback for Check Answer button
#check if users gets correct answer
f = int(self.e1.get())
if f == self.answer:
self.score +=1
self.b1_output.configure(text = self.score)
else:
self.b1_output.configure(text = self.score)
def new_question(self): #callback for Next button to next question
self.b1_output.configure(text = " ")
radiobuttons = TimesTableGUI(root) #restarts the class for new question
if __name__ == "__main__":
root = Tk()
root.title("Task 3")
radiobuttons = TimesTableGUI(root)
root.mainloop()

Try this:
import random
from tkinter import *
class TimesTableGUI:
def __init__(self, parent, score=0):
self.score = score
self.master = parent
self.ask_new_question()
self.b1_output = Label(parent, text="Score = %i" % self.score)
self.b1_output.grid(row=2, column=1)
def ask_new_question(self):
self.question_frame = Frame(self.master)
self.question_frame.grid(row=1, column=1)
#chooses a random number
num1 = random.randint(1, 10)
num2 = random.randint(1, 10)
self.answer = num1 * num2
#making labels
entry_label = Label(self.question_frame, text="%i*%i" % (num1, num2))
entry_label.grid(row=0, column=0)
self.e1 = Entry(self.question_frame, width=5)
self.e1.grid(row=0, column=1)
self.check_answer_btn = Button(self.question_frame, text="Check Answer",
command=self.check_answer)
self.check_answer_btn.grid(row=1, column=0)
b2 = Button(self.question_frame, text="Next",
command=self.new_question)
b2.grid(row=1, column=1)
def check_answer(self):
user_answer = int(self.e1.get())
if user_answer == self.answer:
self.score += 1
self.b1_output.configure(text="Score = %i" % self.score)
# So the user can't submit more than 1 correct answer
self.check_answer_btn.config(state="disabled")
def new_question(self):
# Remove the frame (and all of its children)
self.question_frame.destroy()
# Display the new question
self.ask_new_question()
if __name__ == "__main__":
root = Tk()
root.title("Task 3")
radiobuttons = TimesTableGUI(root)
root.mainloop()
I moved all of the widgets that are for the question inside a frame. The frame gets destroyed and recreated each time a new question needs to be displayed. I also made sure the user can't just keep clicking on Check Answer after they got the correct answer already (for that question).

The problem is that when you you call radiobuttons = TimesTableGUI(root) score is getting reset too.
Quick solution: pass score when you call radiobuttons = TimesTableGUI(root, score=self.score) in new_question
Better solution: create a method that reset GUI without reinitialize the entire class.

Related

appending checkbox interger to list python tkinter

I've been coding a program that would let you practice multiplication, on the program, I've made it so the user can select what numbers they want to practice with checkboxes. I've managed to get it working but I was wondering if there is a more iterative way of doing it since i want to go from 1 to 12. I've seen ways of making checkboxes from a list but I haven't been able to get it to work.
from msilib.schema import CheckBox
from tkinter import *
import random
from tkinter import messagebox
class Timestable:
def __init__(self, parent):
self.f1 = Frame(parent)
self.f1.grid()
self.f2 = Frame(parent)
self.f2.grid()
self.f2.grid_forget()
self.f3 = Frame(parent)
self.f3.grid()
self.f3.grid_forget()
#frame 1 ========================================================
Label(self.f1,text="Multiplication Practice").grid()
Label(self.f1,text="Name:").grid()
self.name = Entry (self.f1)
self.name.grid()
Label(self.f1,text="Age").grid()
self.age = Entry (self.f1)
self.age.grid()
self.user = []
self.incorrect=[]
self.checked1 = IntVar()
self.checked2 = IntVar()
self.c1 = Checkbutton(self.f1, text='1',variable=self.checked1,onvalue=1,offvalue=0,command=self.save)
self.c1.grid()
self.c2 = Checkbutton(self.f1, text='2', variable=self.checked2,onvalue=1,offvalue=0,command=self.save)
self.c2.grid()
self.w = Spinbox(self.f1, from_=1, to=5)
self.w.grid()
Button(self.f1,text="Start", command=self.start).grid()
#frame 2 ========================================================
Label(self.f2,text="Frame 2 ").grid()
self.x=0
self.correct=0
sub = lambda: Timestable.Submit(self)
Button(self.f2,text="submit", command=sub).grid()
self.entryWidget = Entry (self.f2)
self.entryWidget.grid()
#frame 3 ========================================================
Label(self.f3,text="Frame 3 ").grid()
# ===============================================================
def save(self):
if self.checked1.get() == 1:
self.user.append(1)
if self.checked2.get() == 1:
self.user.append(2)
#self.user.append(self.checked1.get())
def clear_text(self):
self.entryWidget.delete(0, 'end')
def Questions(self):
number1 = random.choice(self.user)
number2 = random.randrange(1,12)
self.answer = number1 * number2
self.prompt = (str(number1) + " X " + str(number2))
quest = Label(self.f2, text=self.prompt, width=len(self.prompt))
quest.grid()
return self.answer
def start(self):
self.f1.grid_forget()
self.f2.grid()
self.Questions()
def results(self):
self.f2.grid_forget()
self.f3.grid()
def Submit(self):
if self.entryWidget.get() == "":
messagebox.showerror("Error", "Please enter a number.")
elif self.entryWidget.get() == str:
messagebox.showerror("Error", "Please enter a number, not letters.")
else:
if self.answer != int(self.entryWidget.get().strip()):
messagebox.showinfo("Answer", "INCORRECT! Answer: " + str(self.answer))
self.incorrect.append(self.prompt)
else:
messagebox.showinfo("Answer", "CORRECT!")
self.correct = self.correct +1
self.x=self.x+1
if self.x < int(self.w.get()):
self.Questions()
self.clear_text()
else:
self.results()
self.percent = round(self.correct/self.x*100)
Label(self.f3,text="Congrats, you got "+ str(self.percent) +"% of the questions correct" ).grid()
Label(self.f3,text = self.incorrect).grid()
root = Tk()
root.geometry("200x300")
Timestable(root)
root.mainloop()
Instead of creating the checkboxes one by one, you can use a for loop to create them:
class Timestable:
def __init__(self, parent):
...
self.user = []
self.incorrect = []
# -- create those checkboxes
# list to store the tkinter variables for those checkboxes
self.checked = []
# suggest to create those checkboxes inside a frame
# and put 3 checkboxes in a row
frame = Frame(self.f1)
frame.grid()
for i in range(12):
self.checked.append(IntVar()) # store the tkinter variable for the checkbox
Checkbutton(frame, text=i+1, variable=self.checked[-1], onvalue=i+1, offvalue=0, anchor='w').grid(row=i//3, column=i%3, sticky='ew')
...
Note that I don't require the save(). You can build the self.user inside start():
def start(self):
# get the user selected numbers
self.user = [v.get() for v in self.checked if v.get()]
# you can check the user list
#print(self.user)
if self.user:
# only start the game when there are numbers selected
self.f1.grid_forget()
self.f2.grid()
self.Questions()

I can't change the background of the tkinter canvas in python

Despite using the config() method, my canvas's background color won't change. I've made sure the if statement is correct by using some print statements, I've done some research and this is the only way to change the color of an existing canvas-
google search results for how to change the canvas background color
screenshot of program when executed (the canvas is the white thing with the question text, the score is a label, the check mark and X buttons are buttons, and I've used the grid() methods to make them display)
In addition, after browsing some old questions, one of the potential causes for this was that a new canvas was being created after each iteration, and since I've defined the cavas in the innit() method, this isn't the case.
So what exactly should I do?
QuizBrain Class-
import html
class QuizBrain:
def __init__(self, q_list):
self.question_number = 0
self.score = 0
self.question_list = q_list
self.current_question = None
def still_has_questions(self):
return self.question_number < len(self.question_list)
def next_question(self):
self.current_question = self.question_list[self.question_number]
self.question_number += 1
q_text = html.unescape(self.current_question.text)
return f"Q.{self.question_number}: {q_text}"
def check_answer(self, user_answer):
correct_answer = self.current_question.answer
if user_answer.lower() == correct_answer.lower():
self.score += 1
return True
else:
return False
QuizInterface Class- look at the def give_feedback(self, is_right: bool) method, it's responsible for changing the canvas background which tells the user if they got it right(green) or wrong(red). I've also shared the other classes (above and below) for context and incase the problem is there.
from tkinter import *
from quiz_brain import QuizBrain
THEME_COLOR = "#375362"
class QuizInterface:
def __init__(self, quiz_brain: QuizBrain):
self.quiz = quiz_brain
self.window = Tk()
self.window.config(background=THEME_COLOR, padx=20, pady=20)
self.window.title("Quiz")
self.score_label = Label(text="score: 0", font=("Arial", 20, "italic"), padx=20, pady=20, bg=THEME_COLOR,
fg="white")
self.score_label.grid(row=0, column=1)
self.canvas = Canvas(width=300, height=250, background="white")
self.question_text = self.canvas.create_text(150, 125, text="SAMPLE",
font=("Arial", 20, "italic"), fill="black", width=250)
self.canvas.grid(column=0, row=1, columnspan=2, pady=40)
true_image = PhotoImage(file="images/true.png")
false_image = PhotoImage(file="images/false.png")
self.true_button = Button(image=true_image, command=self.true_pressed)
self.true_button.grid(row=2, column=0)
self.false_button = Button(image=false_image, command=self.false_pressed)
self.false_button.grid(row=2, column=1)
self.get_next_question()
self.window.mainloop()
def get_next_question(self):
question_text = self.quiz.next_question()
self.canvas.itemconfig(self.question_text, text=question_text)
def true_pressed(self):
is_right = self.quiz.check_answer("True")
self.give_feedback(is_right)
def false_pressed(self):
is_right = self.quiz.check_answer("False")
self.give_feedback(is_right)
def give_feedback(self, is_right: bool):
print("Called")
if is_right:
print("Called-2")
self.canvas.configure(bg="green")
print("Executed")
elif not is_right:
print("called-3")
self.canvas.configure(bg="red")
print("Executed")
self.window.after(3000, self.get_next_question)
self.canvas.config(background="white")
Question Class-
class Question:
def __init__(self, q_text, q_answer):
self.text = q_text
self.answer = q_answer
How I get my questions-
import requests
parameters = {
"amount": 10,
"type": "boolean"
}
quiz_data = requests.get(url="https://opentdb.com/api.php", params=parameters)
quiz_data.raise_for_status()
quiz_questions = quiz_data.json()
question_data = quiz_questions["results"]
Main.py-
from question_model import Question
from data import question_data
from quiz_brain import QuizBrain
from ui import QuizInterface
question_bank = []
for question in question_data:
question_text = question["question"]
question_answer = question["correct_answer"]
new_question = Question(question_text, question_answer)
question_bank.append(new_question)
quiz = QuizBrain(question_bank)
quiz_interface = QuizInterface(quiz)
#
# while quiz.still_has_questions():
# quiz.next_question()
#
# print("You've completed the quiz")
# print(f"Your final score was: {quiz.score}/{quiz.question_number}")
Move the line
self.canvas.config(background="white")
from give_feedback function to get_next_question function.

Python Random Math Equation Generator program

I've currently been creating a math equation generator program that will ask you random multiplication questions. I have been having trouble with the if statements where ans == answer will be equal dependent on the user's input (correct answer). However, my program does not see it as equal despite being the same value with example from printing ans and answer which shows they are the same value. I don't know if I am doing something wrong and I would like to know a method of fixing my issue.
Also when I tried to create for and while loops for the generating the equations, it would print them all out at once, is there a way to also make it so that the loop will not print a new random equation until the user gets the answer right?
from tkinter import *
from random import *
import random
import time
import tkinter as tk
import math
import operator
from tkinter import messagebox
#This is for creating the tkinter window and fixing the specified dimensions into place
root = tk.Tk()
root.geometry("900x600")
#This section creates the canvas and its specifications
canvas_width = 900
canvas_height = 500
c = Canvas(root, width=canvas_width, height=canvas_height, bg="pink")
c.pack()
def quitgame():
root.destroy()
class Game_Images:
#Background Image
bg = PhotoImage(file="../Data/sidescroll background flip.gif")
bg = bg.zoom(2)
c.create_image(0,0, anchor=NW, image=bg)
#Insert Image of Enemy
enemy = PhotoImage(file="../Data/monke2.png")
enemy1 = c.create_image(0,260, anchor=NW, image=enemy)
#Insert image of playable character
player = PhotoImage(file="../Data/monke2.png")
player1 = c.create_image(0,325, anchor=NW, image=player)
g = Game_Images()
score = 0
x = 1
def game_start():
global answer, question
int_1 = random.randint(1, 12)
int_2 = random.randint(1, 12)
displayQuestion = "What is "+str(int_1)+ "*" + str(int_2)+"?"
operator = ["*"]
ops = random.choice(operator)
c.create_rectangle(353,0,550,75, fill = "white")
c.create_text(450, 50, font = ("Helvetica", 15), fill="pink", text = displayQuestion)
question = str(int_1) + str(ops) + str(int_2)
answer = int_1 * int_2
def generateQ():
ans = e1.get()
e1.delete(0, END)
if ans == answer:
score += 1
x += 1
print("correct")
print(ans)
print(answer)
else:
print("wrong")
print(ans)
print(answer)
#Buttons show up below the canvas to run commands when pressed
Button(root, text = "Commence Forth",width = 15, command = game_start).place(x=10, y=570)
Button(root, text = "Quit",width = 11, command = quitgame).place(x=800, y=570)
e1 = Entry(root)
e1.pack(padx=30, pady=30)
b=Button(root,text="Enter", width=5, font=("Helvetica", 12), command = generateQ)
b.place(x=550, y=534)
root.mainloop()
Change function generateQ() like this -
def generateQ():
global score,x
ans = e1.get()
e1.delete(0, END)
if int(ans) == int(answer):
score += 1
x += 1
print("correct")
print(ans)
print(answer)
else:
print("wrong")
print(ans)
print(answer)
Use int() so that they are of same data type. And use global score,x because it shows error. You could also write score and x as arguments.

Tkinter ISBN Converter Need a way of printing results on Tkinter page

import sys
from tkinter import *
def main():
mtext = ment.get()
mlabel2 = Label(top, text=mtext).pack()
def isbn():
digits = [(11 - i) * num for i, num in enumerate(map(int, list()))]
digit_11 = 11 - (sum(digits) % 11)
if digit_11 == 10:
digit_11 = 'X'
digits.append(digit_11)
isbn_number = "".join(map(str, digits))
label2 = Label(top, text = "Your ISBN number is",).pack()
top = Tk()
top.geometry("450x450+500+300")
top.title("Test")
button = Button(top,text="OK", command = isbn, fg = "red", bg="blue").pack()
label = Label(top, text = "Please enter the 10 digit number").pack()
ment= IntVar()
mEntry = Entry(top,textvariable=ment).pack()
Hello, I the code at the moment is a working stage, I just need a way of the results printing because at the moment it is not. I would also like the converter to work proper ally with 10 digits and the 11th digit being the number that the converter finds outs. Thanls
Note how the button's command calls the function to perform your operation:
from tkinter import *
def find_isbn(isbn, lbl):
if len(isbn) == 10:
mult = 11
total = 0
for i in range(len(isbn)):
total += int(isbn[i]) * mult
mult -= 1
digit_11 = 11 - (total % 11)
if digit_11 == 10:
digit_11 = 'X'
isbn += str(digit_11)
lbl.config(text=isbn)
top = Tk()
top.geometry("450x450+500+300")
top.title("Test")
button = Button(top, text="OK", command=lambda: find_isbn(mEntry.get(), mlabel2), fg="red", bg="blue")
button.pack()
label = Label(top, text="Please enter the 10 digit number")
label.pack()
mEntry = Entry(top)
mEntry.pack()
mlabel2 = Label(top, text='')
mlabel2.pack()
top.mainloop()
You also need to call .mainloop(), on your master widget to get the whole things started. It's also better to call .pack() on an object on a separate line. pack() and grid() return nothing so you won't be able to use other methods on the object once you do that.

Creating a Tkinter math Quiz

I am learning python and am having trouble getting this program to work correctly.
from Tkinter import*
import time
import tkMessageBox
import random
def Questions():
number1 = random.randrange(1,25,1)
number2 = random.randrange(1,50,2)
answer = number1 + number2
prompt = ("Add " + str(number1) + " and " + str(number2))
label1 = Label(root, text=prompt, width=len(prompt), bg='yellow')
label1.pack()
return answer
def start():
global count_flag
Questions()
count_flag = True
count = 0.0
while True:
if count_flag == False:
break
# put the count value into the label
label['text'] = str(count)
# wait for 0.1 seconds
time.sleep(0.1)
# needed with time.sleep()
root.update()
# increase count
count += 0.1
def Submit(answer, entryWidget):
""" Display the Entry text value. """
global count_flag
count_flag = False
print answer
if entryWidget.get().strip() == "":
tkMessageBox.showerror("Tkinter Entry Widget", "Please enter a number.")
if int(answer) != entryWidget.get().strip():
tkMessageBox.showinfo("Answer", "INCORRECT!")
else:
tkMessageBox.showinfo("Answer", "CORRECT!")
# create a Tkinter window
root = Tk()
root.title("Math Quiz")
root["padx"] = 40
root["pady"] = 20
# Create a text frame to hold the text Label and the Entry widget
textFrame = Frame(root)
#Create a Label in textFrame
entryLabel = Label(textFrame)
entryLabel["text"] = "Answer:"
entryLabel.pack(side=LEFT)
# Create an Entry Widget in textFrame
entryWidget = Entry(textFrame)
entryWidget["width"] = 50
entryWidget.pack(side=LEFT)
textFrame.pack()
#directions
directions = ('Click start to begin. You will be asked a series of questions like the one below.')
instructions = Label(root, text=directions, width=len(directions), bg='orange')
instructions.pack()
# this will be a global flag
count_flag = True
answer = Questions()
Sub = lambda: Submit(answer, entryWidget)
# create needed widgets
label = Label(root, text='0.0')
btn_submit = Button(root, text="Submit", command = Sub)
btn_start = Button(root, text="Start", command = start)
btn_submit.pack()
btn_start.pack()
label.pack()
# start the event loop
root.mainloop()
It just says "INCORRECT!" every time I push submit regardless of what I enter into the text box. Any suggestions would be appreciated. Thanks, Scott
Left side is an integer, right side is a string, so it's always False:
int(answer) != entryWidget.get().strip()
You can try:
int(answer) != int(entryWidget.get().strip())

Categories