| | |
Tkinter Help
Thread Solved
![]() |
Hi there,
My niece often asks me to quiz her on her times tables, so I thought I'd spare myself any further effort and write a program to ask her random multiplication questions. I used Tkinter to create a GUI with a label asking the question, an entry box for her to input answers, and a button to ask the next question. I want to bind the Enter key to submit answers, but I can't figure out how to send the date to my Ask method. Any ideas?
Thanks,
Jay
My niece often asks me to quiz her on her times tables, so I thought I'd spare myself any further effort and write a program to ask her random multiplication questions. I used Tkinter to create a GUI with a label asking the question, an entry box for her to input answers, and a button to ask the next question. I want to bind the Enter key to submit answers, but I can't figure out how to send the date to my Ask method. Any ideas?
Thanks,
Jay
Please show your code. It makes it tough to help without it. Others too can learn from your code and the answers.
This would be one way to bind Entry box enter1 to return key:
The data is then in enter1.get() as a string.
This would be one way to bind Entry box enter1 to return key:
Python Syntax (Toggle Plain Text)
enter1.bind('<Return>', func=my_function)
I managed to fix it. Not sure how 

Python Syntax (Toggle Plain Text)
from Tkinter import * import tkMessageBox import random import string 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) + '?') def checkAnswer(): herAnswer = entry1.get() if int(herAnswer) != answer: tkMessageBox.showwarning(message='The answer is: ' + str(answer)) else: tkMessageBox.showinfo(message='Correct!') #set the window root = Tk() root.title("Helena's Multiplication Quiz") root.geometry('500x500') #add label label1 = Label(root) label1.grid(row=2, column=0) #add entry entry1 = Entry(root) entry1.grid(row=3, column=0) #add button askBtn = Button(root, text='Ask Me a Question!', command=ask) askBtn.grid(row=4, column=0) okButton = Button(root, text='OK', command=checkAnswer) okButton.grid(row=4, column=1) tkMessageBox.showinfo(message="Hi, Helena! Let's do some multiplication problems!") root.mainloop()
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:
Python Syntax (Toggle Plain Text)
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()
Thanks very much, bumsfeld. Now I've learned a few things!
I'm now reading about lambda functions, and having a hard time with it, but hopefully I'll fully understand with a bit more reading.
My youngest niece (Susan) was very jealous, so I had to modify Helena's script to display addition problems. Oh boy..these kids are spoiled!
I'm now reading about lambda functions, and having a hard time with it, but hopefully I'll fully understand with a bit more reading.My youngest niece (Susan) was very jealous, so I had to modify Helena's script to display addition problems. Oh boy..these kids are spoiled!
I guess the Tkinter GUI looks a lot nicer on Linux or Unix computers.
On Windows computers it does not take advantage of the native windows design, and the Tkinter widgets appear downright gawky. This might be the reason the wxPython GUI is preferred by Windows users.
In GUI programs, you deal a lot with callback functions and events. There lambda allows you to handle arguments more gracefully.
On Windows computers it does not take advantage of the native windows design, and the Tkinter widgets appear downright gawky. This might be the reason the wxPython GUI is preferred by Windows users.
In GUI programs, you deal a lot with callback functions and events. There lambda allows you to handle arguments more gracefully.
May 'the Google' be with you!
![]() |
Similar Threads
- Tkinter exception (Python)
- TKinter tough time (Python)
- Tkinter: protecting text in the Entry widget (Python)
- Wondering about new windows in Tkinter (Python)
- Need Help with Tkinter (Python)
Other Threads in the Python Forum
- Previous Thread: Tuple List
- Next Thread: Newbie Problem
| Thread Tools | Search this Thread |
alarm ansi anydbm app assignment backend beginner binary bluetooth character cipher cmd coordinates customdialog cx-freeze data decimals development directory dynamic exe feet file float format function generator getvalue gnu graphics halp handling heads homework http ideas input ip itunes java keycontrol leftmouse line linux list lists loop maintain maze millimeter module mouse number numbers output parsing path pointer prime programming progressbar push py2exe pygame pymailer python queue random recursion recursive schedule screensaverloopinactive script slicenotation sqlite ssh statistics string strings sudokusolver text thread time tlapse tuple ubuntu unicode url urllib urllib2 variable ventrilo vigenere web webservice wikipedia write wxpython xlib xlwt






