In: Computer Science
I did the complete programming in python , just want to use tkinter for GUI. please look at the 2nd part . i did some part of this program using tkinter but could not finis it. Thank you.
import random
image = 'w'
# modified functions which accepts two numbers each and returns the respective
# output
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def kidCalc():
a = random.randint(0, 9)
b = random.randint(0, 9)
print("0. Exit")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
choice = int(input("Enter choice: "))
cnt = 0 # To hold number of tries
if choice == 1:
while True:
print(" what do you think the sum of these two numbers are? ", a, " + ", b)
print("This is how it looks viually", a * image, "+ ", b * image)
sum = input()
answer = int(sum)
if answer == add(a, b):
print("Perfect, the answer is correct...")
cont = input("\nIf you want to solve another question then press 1. If not then any other key ")
if cont == "1":
a = random.randint(0, 9)
b = random.randint(0, 9)
cnt = 0
continue
else:
break
elif answer != add(a, b):
print("I am sorry, your answer is wrong Sean,Please Try again:")
# Incrementing count
cnt = cnt + 1
# Checking count
if cnt == 3:
# Prompting for next try
ans = input("\nMax number of tries reached. Do you want to try another question or quit? (T/Q): ")
# Checking answer
if ans.upper() == "T":
a = random.randint(0, 9)
b = random.randint(0, 9)
continue
else:
print('Bye!')
break
else:
continue
if choice == 2:
while True:
print(" what do you think the sum of these two numbers are? ", a, " - ", b)
print("This is how it looks viually", a * image, "- ", b * image)
sub = input()
answer = int(sub)
if answer == subtract(a, b):
print("Perfect, the answer is correct...")
cont = input("\nIf you want to solve another question then press 1. If not then any other key ")
if cont == "1":
a = random.randint(0, 9)
b = random.randint(0, 9)
cnt = 0
continue
else:
break
elif answer != subtract(a, b):
print("I am sorry, your answer is wrong Sean,Please Try again:")
# Incrementing count
cnt = cnt + 1
# Checking count
if cnt == 3:
# Prompting for next try
ans = input("\nMax number of tries reached. Do you want to try another question or quit? (T/Q): ")
# Checking answer
if ans.upper() == "T":
a = random.randint(0, 9)
b = random.randint(0, 9)
continue
else:
print('Bye!')
break
else:
continue
if choice == 3:
while True:
print(" what do you think the sum of these two numbers are? ", a, " - ", b)
print("This is how it looks viually", a * image, "- ", b * image)
mult = input()
answer = int(mult)
if answer == multiply(a, b):
print("Perfect, the answer is correct...")
cont = input("\nIf you want to solve another question then press 1. If not then any other key ")
if cont == "1":
a = random.randint(0, 9)
b = random.randint(0, 9)
cnt = 0
continue
else:
break
elif answer != multiply(a, b):
print("I am sorry, your answer is wrong Sean,Please Try again:")
# Incrementing count
cnt = cnt + 1
# Checking count
if cnt == 3:
# Prompting for next try
ans = input("\nMax number of tries reached. Do you want to try another question or quit? (T/Q): ")
# Checking answer
if ans.upper() == "T":
a = random.randint(0, 9)
b = random.randint(0, 9)
continue
else:
print('Bye!')
break
else:
continue
kidCalc()
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
from tkinter import *
import random
# FUNCTION
def calc_For_Kids():
# create the tk
var1 = Tk()
var1.title("Kitty Calculator")
var1.geometry('450x450')
# generate the two numbers
numb1 = random.randint(0, 9)
numb2 = random.randint(0, 9)
# ask usr for sum
temp = ("what do you think the sum of these two number is : " + str(numb1) + "+" + str(numb2))
# label
qLabel = Label(var1, text=temp)
qLabel.grid(row=0, column=0)
qLabel1 = Label(var1, text="")
qLabel1.grid(row=1, column=0)
userEntry = Entry(var1)
userEntry.grid(row=2, column=0)
qLabel4 = Label(var1, text="")
# function for checking the result
def func():
textEntered = int(userEntry.get())
if ((numb1 + numb2) == textEntered):
qLabel4.configure(text="Perfect, the answer is correct:")
else:
qLabel4.configure(text="i am so sorry , your answer is wrong kitto: Try again : ")
# label for spacing
qLabel2 = Label(var1, text="")
qLabel2.grid(row=3, column=0)
ok_Button = Button(var1, text="OK", command=func)
ok_Button.grid(row=4, column=0, columnspan=2)
qLabel3 = Label(var1, text="")
qLabel3.grid(row=5, column=0)
# label for result
qLabel4.grid(row=6, column=0)
qLabel5 = Label(var1, text="")
qLabel5.grid(row=7, column=0)
var1.mainloop()
# PROGRAM EXECUTION STARTS HERE
calc_For_Kids()
import random
from tkinter import *
# FUNCTION
def calc_For_Kids():
# create the tk
root = Tk()
root.title("Kitty Calculator")
root.geometry('450x450')
root.config(bg="white")
def addFunc():
def checkAdd():
if (addEntry.get() == str(a + b)):
addlabel3 = Label(root, text="\nPerfect, the answer is correct... \n"
"If you want to solve another question then press option button again.\n"
" If not then press any other option ", bg="white")
addlabel3.grid(row=10, column=0, padx=20)
else:
addlabel3 = Label(root, text="\nSorry, the answer is wrong... Please try again !\n"
"If you want to solve another question then press option button again.\n"
" If not then press any other option ", bg="white")
addlabel3.grid(row=10, column=0, padx=20)
a = random.randint(0, 9)
b = random.randint(0, 9)
addlabel1 = Label(root, text="what do you think the sum of these two numbers are? ", bg="white")
num = f"{a} + {b}"
addlabel2 = Label(root, text=num, bg="white")
addEntry = Entry(root)
addButton = Button(root, text=" Submit ", command=checkAdd)
message = "\n\nIf you want to solve another question then press option button again.\n if not then press any other option "
addlabel3 = Label(root, text=message, bg="white")
addlabel3.grid(row=10, column=0, padx=20)
addlabel1.grid(row=6, column=0, pady=5, padx=20)
addlabel2.grid(row=7, column=0, pady=5, padx=20)
addEntry.grid(row=8, column=0, pady=5, padx=20)
addButton.grid(row=9, column=0, pady=5, padx=20)
def subFunc():
def checkSub():
if (subEntry.get() == str(a - b)):
sublabel3 = Label(root, text="\nPerfect, the answer is correct... \n"
"If you want to solve another question then press option button again.\n"
" If not then press any other option ", bg="white")
sublabel3.grid(row=10, column=0, padx=20)
else:
sublabel3 = Label(root, text="\nSorry, the answer is wrong... Please try again !\n"
"If you want to solve another question then press option button again.\n"
" If not then press any other option ", bg="white")
sublabel3.grid(row=10, column=0, padx=20)
a = random.randint(0, 9)
b = random.randint(0, 9)
sublabel1 = Label(root, text="what do you think the difference of these two numbers are? ", bg="white")
subnum = f"{a} - {b}"
sublabel2 = Label(root, text=subnum, bg="white")
subEntry = Entry(root)
subButton = Button(root, text=" Submit ", command=checkSub)
message = "\n\nIf you want to solve another question then press option button again.\n if not then press any other option "
sublabel3 = Label(root, text=message, bg="white")
sublabel3.grid(row=10, column=0, padx=20)
sublabel1.grid(row=6, column=0, pady=5, padx=20)
sublabel2.grid(row=7, column=0, pady=5, padx=20)
subEntry.grid(row=8, column=0, pady=5, padx=20)
subButton.grid(row=9, column=0, pady=5, padx=20)
def mulFunc():
def checkMul():
if (mulEntry.get() == str(a * b)):
mullabel3 = Label(root, text="\nPerfect, the answer is correct... \n"
"If you want to solve another question then press option button again.\n"
" If not then press any other option ", bg="white")
mullabel3.grid(row=10, column=0, padx=20)
else:
mullabel3 = Label(root, text="\nSorry, the answer is wrong... Please try again !\n"
"If you want to solve another question then press option button again.\n"
" If not then press any other option ", bg="white")
mullabel3.grid(row=10, column=0, padx=20)
a = random.randint(0, 9)
b = random.randint(0, 9)
mullabel1 = Label(root, text="what do you think the difference of these two numbers are? ", bg="white")
mulnum = f"{a} * {b}"
mullabel2 = Label(root, text=mulnum, bg="white")
mulEntry = Entry(root)
mulButton = Button(root, text=" Submit ", command=checkMul)
message = "\n\nIf you want to solve another question then press option button again.\n if not then press any other option "
mullabel3 = Label(root, text=message, bg="white")
mullabel3.grid(row=10, column=0, padx=20)
mullabel1.grid(row=6, column=0, pady=5, padx=20)
mullabel2.grid(row=7, column=0, pady=5, padx=20)
mulEntry.grid(row=8, column=0, pady=5, padx=20)
mulButton.grid(row=9, column=0, pady=5, padx=20)
def exitFunc():
exit()
# menu
label5 = Label(root, text="Please click on the options below : ", bg="white", fg="#003366")
label1 = Button(root, text="1. Add", bg="#003366", fg="white", width=20, command=addFunc)
label2 = Button(root, text="2. Subtract", bg="#003366", fg="white", width=20, command=subFunc)
label3 = Button(root, text="3. Multiply", bg="#003366", fg="white", width=20, command=mulFunc)
label4 = Button(root, text="0. Exit", bg="#003366", fg="white", width=20, command=exitFunc)
label6 = Label(root, bg="white")
label1.grid(row=1, column=0, pady=(5, 5), padx=150)
label2.grid(row=2, column=0, pady=5, padx=20)
label3.grid(row=3, column=0, pady=5, padx=20)
label4.grid(row=4, column=0, pady=5, padx=20)
label5.grid(row=0, column=0, pady=(50, 5), padx=20)
label6.grid(row=5, column=0, pady=5, padx=20)
root.mainloop()
# PROGRAM EXECUTION STARTS HERE
calc_For_Kids()