944,028 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 5491
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 6th, 2006
0

Tkinter Help

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jayzilla is offline Offline
5 posts
since Jul 2006
Jul 6th, 2006
0

Re: Tkinter Help

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:
Python Syntax (Toggle Plain Text)
  1. enter1.bind('<Return>', func=my_function)
The data is then in enter1.get() as a string.
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Jul 6th, 2006
0

Re: Tkinter Help

I managed to fix it. Not sure how

Python Syntax (Toggle Plain Text)
  1. from Tkinter import *
  2. import tkMessageBox
  3. import random
  4. import string
  5.  
  6. def ask():
  7. global num1
  8. num1 = random.randint(1, 12)
  9. global num2
  10. num2 = random.randint(1, 12)
  11. global answer
  12. answer = num1 * num2
  13. label1.config(text='What is ' + str(num1) + 'x' + str(num2) + '?')
  14.  
  15. def checkAnswer():
  16. herAnswer = entry1.get()
  17. if int(herAnswer) != answer:
  18. tkMessageBox.showwarning(message='The answer is: ' + str(answer))
  19. else:
  20. tkMessageBox.showinfo(message='Correct!')
  21.  
  22. #set the window
  23. root = Tk()
  24. root.title("Helena's Multiplication Quiz")
  25. root.geometry('500x500')
  26.  
  27. #add label
  28. label1 = Label(root)
  29. label1.grid(row=2, column=0)
  30.  
  31. #add entry
  32. entry1 = Entry(root)
  33. entry1.grid(row=3, column=0)
  34.  
  35.  
  36. #add button
  37. askBtn = Button(root, text='Ask Me a Question!', command=ask)
  38. askBtn.grid(row=4, column=0)
  39.  
  40. okButton = Button(root, text='OK', command=checkAnswer)
  41. okButton.grid(row=4, column=1)
  42.  
  43. tkMessageBox.showinfo(message="Hi, Helena! Let's do some multiplication problems!")
  44. root.mainloop()
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jayzilla is offline Offline
5 posts
since Jul 2006
Jul 7th, 2006
0

Re: Tkinter Help

Looks like a nice program. On my computer (OS = Windows XP) the last tkMessageBox.showinfo() prevents any focus on entry1, no cursor! Strange indeed!
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 7th, 2006
0

Re: Tkinter Help

Quote originally posted by vegaseat ...
Looks like a nice program. On my computer (OS = Windows XP) the last tkMessageBox.showinfo() prevents any focus on entry1, no cursor! Strange indeed!
very nice indeed, but i have the same issue, (running windows xp) except it does it to me straight away.
Reputation Points: 26
Solved Threads: 24
Junior Poster
a1eio is offline Offline
140 posts
since Aug 2005
Jul 7th, 2006
0

Re: Tkinter Help

That's very odd. o_0. Works just fine on my OS (Linux) but I'll try it on a Windows machine when I have a little time. Thanks for listening to me ramble
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jayzilla is offline Offline
5 posts
since Jul 2006
Jul 7th, 2006
0

Re: Tkinter Help

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)
  1. from Tkinter import *
  2. import tkMessageBox
  3. import random
  4. #import string # not needed!
  5.  
  6. def ask():
  7. global num1
  8. num1 = random.randint(1, 12)
  9. global num2
  10. num2 = random.randint(1, 12)
  11. global answer
  12. answer = num1 * num2
  13. label1.config(text='What is ' + str(num1) + 'x' + str(num2) + '?')
  14. # put the cursor into the Enter-Box
  15. entry1.focus_set()
  16.  
  17. def checkAnswer():
  18. herAnswer = entry1.get()
  19. # if empty give message
  20. if len(herAnswer) == 0:
  21. tkMessageBox.showwarning(message='Need to enter some number!')
  22. return
  23. if int(herAnswer) != answer:
  24. tkMessageBox.showwarning(message='The answer is: ' + str(answer))
  25. else:
  26. tkMessageBox.showinfo(message='Correct!')
  27.  
  28. #set the window
  29. root = Tk()
  30. # add some color
  31. root.tk_bisque()
  32. root.title("Helena's Multiplication Quiz")
  33. root.geometry('500x500')
  34.  
  35. # replaces tkMessageBox.showinfo()
  36. label2 = Label(root, text="Hi, Helena!\n Let's do some multiplication problems!")
  37. # add a colorful font
  38. label2.config(font=('times', 18, 'bold'), fg='red', bg='yellow')
  39. label2.grid(row=0, column=0)
  40.  
  41. #add label
  42. label1 = Label(root)
  43. label1.grid(row=2, column=0)
  44.  
  45. #add entry
  46. entry1 = Entry(root)
  47. entry1.grid(row=3, column=0)
  48. # bind the return key
  49. entry1.bind('<Return>', func=lambda e:checkAnswer())
  50.  
  51. #add button
  52. askBtn = Button(root, text='Ask Me a Question!', bg='green', command=ask)
  53. askBtn.grid(row=4, column=0)
  54.  
  55. okButton = Button(root, text='OK', command=checkAnswer)
  56. okButton.grid(row=4, column=1)
  57.  
  58. # seems to create a problem with the entry1 focus in Windows, replace with label2
  59. #tkMessageBox.showinfo(message="Hi, Helena! Let's do some multiplication problems!")
  60.  
  61. root.mainloop()
Reputation Points: 404
Solved Threads: 180
Nearly a Posting Virtuoso
bumsfeld is offline Offline
1,422 posts
since Jul 2005
Jul 7th, 2006
0

Re: Tkinter Help

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!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Jayzilla is offline Offline
5 posts
since Jul 2006
Jul 10th, 2006
0

Re: Tkinter Help

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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jul 14th, 2006
0

Dumb followup question...

OK, why did you have

entry1.bind('<Return>', func=lambda e:checkAnswer())

instead of

entry1.bind('<Return>', func=checkAnswer)?

Thanks,
Jeff Cagle
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Tuple List
Next Thread in Python Forum Timeline: Newbie Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC