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)

Recommended Answers

All 5 Replies

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.

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:

[/B] 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)[B]

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?

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:

from Tkinter import *

mainw = Tk()
mainw.frame = Frame(mainw)

mainw.frame.label = Label(mainw.frame,text="Guess a Number!")
mainw.frame.label.grid(row=0,column=0)
mainw.frame.entry = Entry(mainw.frame)
mainw.frame.entry.grid(row=0,column=1,columnspan=2)

mainw.frame.text = Text(mainw.frame,width=35,height=10)
mainw.frame.text.grid(row=1,columnspan=2)
mainw.frame.scroll = Scrollbar(mainw.frame,orient=VERTICAL,
                               command=mainw.frame.text.yview)
mainw.frame.text['yscrollcommand']=mainw.frame.scroll.set
mainw.frame.scroll.grid(row=1,column=2,sticky=N+S)

mainw.frame.rbutton = Button(mainw.frame,text="Restart")
mainw.frame.rbutton.grid(row=2,column=0,sticky=E+W)
mainw.frame.qbutton = Button(mainw.frame,text="Quit")
mainw.frame.qbutton.grid(row=2,column=1,sticky=E+W)

mainw.frame.grid()
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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.