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()
CodePudding user response:
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()