944,198 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2139
  • Python RSS
Mar 26th, 2007
0

GUI with calculator.

Expand Post »
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)
Similar Threads
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
fredzik is offline Offline
55 posts
since Feb 2007
Mar 26th, 2007
0

Re: GUI with calculator.

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.
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Mar 26th, 2007
0

Re: GUI with calculator.

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:
python Syntax (Toggle Plain Text)
  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]
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
fredzik is offline Offline
55 posts
since Feb 2007
Mar 26th, 2007
0

Re: GUI with calculator.

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?
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
fredzik is offline Offline
55 posts
since Feb 2007
Mar 27th, 2007
0

Re: GUI with calculator.

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:

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Mar 27th, 2007
0

Re: GUI with calculator.

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.
Reputation Points: 10
Solved Threads: 5
Junior Poster in Training
fredzik is offline Offline
55 posts
since Feb 2007

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: collecting ip address
Next Thread in Python Forum Timeline: wxPython tutorials?





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


Follow us on Twitter


© 2011 DaniWeb® LLC