Helena is lucky to have her own programmer! I too had Windows problem and solved it by putting message in label. I couldn't help myself, just had to add some colour. I hope Helena is not colorblind. I also show you how to bind the return key. Here is your program with some colour:
from Tkinter import *
import tkMessageBox
import random
#import string # not needed!
def ask():
global num1
num1 = random.randint(1, 12)
global num2
num2 = random.randint(1, 12)
global answer
answer = num1 * num2
label1.config(text='What is ' + str(num1) + 'x' + str(num2) + '?')
# put the cursor into the Enter-Box
entry1.focus_set()
def checkAnswer():
herAnswer = entry1.get()
# if empty give message
if len(herAnswer) == 0:
tkMessageBox.showwarning(message='Need to enter some number!')
return
if int(herAnswer) != answer:
tkMessageBox.showwarning(message='The answer is: ' + str(answer))
else:
tkMessageBox.showinfo(message='Correct!')
#set the window
root = Tk()
# add some color
root.tk_bisque()
root.title("Helena's Multiplication Quiz")
root.geometry('500x500')
# replaces tkMessageBox.showinfo()
label2 = Label(root, text="Hi, Helena!\n Let's do some multiplication problems!")
# add a colorful font
label2.config(font=('times', 18, 'bold'), fg='red', bg='yellow')
label2.grid(row=0, column=0)
#add label
label1 = Label(root)
label1.grid(row=2, column=0)
#add entry
entry1 = Entry(root)
entry1.grid(row=3, column=0)
# bind the return key
entry1.bind('<Return>', func=lambda e:checkAnswer())
#add button
askBtn = Button(root, text='Ask Me a Question!', bg='green', command=ask)
askBtn.grid(row=4, column=0)
okButton = Button(root, text='OK', command=checkAnswer)
okButton.grid(row=4, column=1)
# seems to create a problem with the entry1 focus in Windows, replace with label2
#tkMessageBox.showinfo(message="Hi, Helena! Let's do some multiplication problems!")
root.mainloop()
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
Offline 1,422 posts
since Jul 2005