Tkinter Help

Thread Solved
Reply

Join Date: Jul 2006
Posts: 5
Reputation: Jayzilla is an unknown quantity at this point 
Solved Threads: 0
Jayzilla's Avatar
Jayzilla Jayzilla is offline Offline
Newbie Poster

Tkinter Help

 
0
  #1
Jul 6th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,205
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 130
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Tkinter Help

 
0
  #2
Jul 6th, 2006
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:
  1. enter1.bind('<Return>', func=my_function)
The data is then in enter1.get() as a string.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 5
Reputation: Jayzilla is an unknown quantity at this point 
Solved Threads: 0
Jayzilla's Avatar
Jayzilla Jayzilla is offline Offline
Newbie Poster

Re: Tkinter Help

 
0
  #3
Jul 6th, 2006
I managed to fix it. Not sure how

  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()
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,862
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Tkinter Help

 
0
  #4
Jul 7th, 2006
Looks like a nice program. On my computer (OS = Windows XP) the last tkMessageBox.showinfo() prevents any focus on entry1, no cursor! Strange indeed!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 138
Reputation: a1eio is an unknown quantity at this point 
Solved Threads: 21
a1eio's Avatar
a1eio a1eio is offline Offline
Junior Poster

Re: Tkinter Help

 
0
  #5
Jul 7th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 5
Reputation: Jayzilla is an unknown quantity at this point 
Solved Threads: 0
Jayzilla's Avatar
Jayzilla Jayzilla is offline Offline
Newbie Poster

Re: Tkinter Help

 
0
  #6
Jul 7th, 2006
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,205
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 130
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: Tkinter Help

 
0
  #7
Jul 7th, 2006
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:
  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()
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 5
Reputation: Jayzilla is an unknown quantity at this point 
Solved Threads: 0
Jayzilla's Avatar
Jayzilla Jayzilla is offline Offline
Newbie Poster

Re: Tkinter Help

 
0
  #8
Jul 7th, 2006
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,862
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 870
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Tkinter Help

 
0
  #9
Jul 10th, 2006
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.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 149
jrcagle jrcagle is offline Offline
Practically a Master Poster

Dumb followup question...

 
0
  #10
Jul 14th, 2006
OK, why did you have

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

instead of

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

Thanks,
Jeff Cagle
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC