GUI with calculator.

Reply

Join Date: Feb 2007
Posts: 55
Reputation: fredzik is an unknown quantity at this point 
Solved Threads: 4
fredzik fredzik is offline Offline
Junior Poster in Training

GUI with calculator.

 
0
  #1
Mar 26th, 2007
G'day,

I've been working with Pyhton for about six months and have been wanting to put a program into a seperate Tkinter GUI but have had no luck.
I need to have a Tk GUI that has vertical scrolling exactly like the normal IDE except that positioned in the upper right corner will sit a tiny calculator.
This tiny calculator does not need to have the "x" or the "+" or the "-" etc or any of the other signs at all.
The reason is because the program is a simple high\low game and only needs to have the numbers from 0 to 9. The only other functions the calculator needs is the backspace (in case of mistakes) and an entry button to enter the number onto the left hand side of the page. Also a quit button to finish.
I have XP on my Compaq Evo desktop.
The program works fine except that when I try to put it into a different GUI all sorts of problems occur. Is this because I need to use pyw instead of py? Or do I just import it as is into the new GUI? I realize that "run==exit" will have to be replaced by quit or suchlike.
Thanks for any help (this is my first time on any forum).

import random
number = random.randint(1, 10000)
running = True
run=""
attempts=0
while running:
guess = int(raw_input('Enter an integer : '))
print
attempts = attempts + 1
WORDS=("Get it before ten tries or the world will explode!",\
"The next one is the 70th try, hurry!", "400 guesses later...",\
"A guess a day keeps the doctor away!", "Count to ten , backwards",\
"Wake me up when you're finished.", "Better luck next time.",\
"It's getting late, hurry up!", "One more and we're finished...",\
"I'm running out of things to say.", "So close, yet so far!",\
"Nearly got it, maybe it's the next guess.")
if attempts==attempts >0:
print random.choice(WORDS)
print "----------------------------------------------------------------------"
print " "
if attempts == attempts > 13:
guess == number
import winsound
import time
time.sleep(1.5)
winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
print " "
print "(The number was %i)" % (number)
print " "
print "***********************"
print "BETTER LUCK NEXT TIME."
print "***********************"
print " "
attempts=0
run = raw_input("\n'run' again or 'exit' ")
if run == "exit":
break
if guess == number:
print " "
print "********************************************"
print 'CONGRATULATIONS, YOU GUESSED IT IN %i TRIES!' % (attempts)
print "********************************************"
attempts=0
run = raw_input("\n'run' again or 'exit' ")
if run == "exit":
break
elif run == "run":
number = random.randint(1, 10000)
random.choice(WORDS)
continue
elif guess < number:
print 'NO, HIGHER THAN %d. (Turn No.%i of 13)' % (guess, attempts)
else:
print 'NO, LOWER THAN %d. (Turn No.%i of 13)' % (guess, attempts)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,523
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 168
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: GUI with calculator.

 
0
  #2
Mar 26th, 2007
First of all, welcome to the forum!

To preserve the code's indentations, please use the [code=python] and [/code] tag pair to enclose your python code.
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 55
Reputation: fredzik is an unknown quantity at this point 
Solved Threads: 4
fredzik fredzik is offline Offline
Junior Poster in Training

Re: GUI with calculator.

 
0
  #3
Mar 26th, 2007
Thanks for your welcome Ene. I knew beforehand that I had to wrap the code somehow, but as this is not explained by an example or suchlike I was puzzled by how to do it. Be that as it may here's the code:
  1. import random
  2. number = random.randint(1, 10000)
  3. running = True
  4. run=""
  5. attempts=0
  6. while running:
  7. guess = int(raw_input('Enter an integer : '))
  8. print
  9. attempts = attempts + 1
  10. WORDS=("Get it before ten tries or the world will explode!",\
  11. "The next one is the 70th try, hurry!", "400 guesses later...",\
  12. "A guess a day keeps the doctor away!", "Count to ten , backwards",\
  13. "Wake me up when you're finished.", "Better luck next time.",\
  14. "It's getting late, hurry up!", "One more and we're finished...",\
  15. "I'm running out of things to say.", "So close, yet so far!",\
  16. "Nearly got it, maybe it's the next guess.")
  17. if attempts==attempts >0:
  18. print random.choice(WORDS)
  19. print "----------------------------------------------------------------------"
  20. print " "
  21. if attempts == attempts > 13:
  22. guess == number
  23. import winsound
  24. import time
  25. time.sleep(1.5)
  26. winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
  27. print " "
  28. print "(The number was %i)" % (number)
  29. print " "
  30. print "***********************"
  31. print "BETTER LUCK NEXT TIME."
  32. print "***********************"
  33. print " "
  34. attempts=0
  35. run = raw_input("\n'run' again or 'exit' ")
  36. if run == "exit":
  37. break
  38. if guess == number:
  39. print " "
  40. print "********************************************"
  41. print 'CONGRATULATIONS, YOU GUESSED IT IN %i TRIES!' % (attempts)
  42. print "********************************************"
  43. attempts=0
  44. run = raw_input("\n'run' again or 'exit' ")
  45. if run == "exit":
  46. break
  47. elif run == "run":
  48. number = random.randint(1, 10000)
  49. random.choice(WORDS)
  50. continue
  51. elif guess < number:
  52. print 'NO, HIGHER THAN %d. (Turn No.%i of 13)' % (guess, attempts)
  53. else:
  54. print 'NO, LOWER THAN %d. (Turn No.%i of 13)' % (guess, attempts)[B][/B]
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 55
Reputation: fredzik is an unknown quantity at this point 
Solved Threads: 4
fredzik fredzik is offline Offline
Junior Poster in Training

Re: GUI with calculator.

 
0
  #4
Mar 26th, 2007
NOTE: "import random" should be in line with "number=..." as it is in my program window. For some reason it's come out indented.
Also at the very end of the program the two b's enclosed by the brackets are not part of my program at all. Maybe I did something wrong when I enclosed the code?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: GUI with calculator.

 
0
  #5
Mar 27th, 2007
I like the Windows shutdown sound when you lose. It threw me for half a second.

Here's a basic framework to get you started, and then a link to the manual that I use for Tkinter:

  1. from Tkinter import *
  2.  
  3. mainw = Tk()
  4. mainw.frame = Frame(mainw)
  5.  
  6. mainw.frame.label = Label(mainw.frame,text="Guess a Number!")
  7. mainw.frame.label.grid(row=0,column=0)
  8. mainw.frame.entry = Entry(mainw.frame)
  9. mainw.frame.entry.grid(row=0,column=1,columnspan=2)
  10.  
  11. mainw.frame.text = Text(mainw.frame,width=35,height=10)
  12. mainw.frame.text.grid(row=1,columnspan=2)
  13. mainw.frame.scroll = Scrollbar(mainw.frame,orient=VERTICAL,
  14. command=mainw.frame.text.yview)
  15. mainw.frame.text['yscrollcommand']=mainw.frame.scroll.set
  16. mainw.frame.scroll.grid(row=1,column=2,sticky=N+S)
  17.  
  18. mainw.frame.rbutton = Button(mainw.frame,text="Restart")
  19. mainw.frame.rbutton.grid(row=2,column=0,sticky=E+W)
  20. mainw.frame.qbutton = Button(mainw.frame,text="Quit")
  21. mainw.frame.qbutton.grid(row=2,column=1,sticky=E+W)
  22.  
  23. mainw.frame.grid()
  24. mainw.mainloop()

http://www.nmt.edu/tcc/help/pubs/tkinter/
http://www.nmt.edu/tcc/help/pubs/tkinter.pdf

Just as general pointers:

* You'll need to figure out how to disable the Text widget so the user can't type into it.
* You'll need to figure out how to bind the <Return> key to the Entry widget so that the user can hit Enter and get the appropriate obnoxious message from the program :lol:
* You'll probably want to configure the app differently so it looks better.
* You'll need to figure out how to supply callbacks to the Reset and Quit buttons.

Hope it helps, and post your final product!
Jeff
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 55
Reputation: fredzik is an unknown quantity at this point 
Solved Threads: 4
fredzik fredzik is offline Offline
Junior Poster in Training

Re: GUI with calculator.

 
0
  #6
Mar 27th, 2007
Thanks Jeff for your reply, you really have saved me a lot of time. :cheesy: I'll get to work on it as soon as I can...actually I forgot I had put the winsound in there as I havn't looked at it closely for a couple of weeks. Thanks again.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC