G'day,

I've been googling for the last three weeks :eek: trying to get an example of a program larger than a simple "Hello World" that works inside it's own GUI.

I've been working on the "GUI with calculator" in this same section of Daniweb, but as soon as I put this program into a seperate GUI the main Idle GUI takes over and the other GUI disappears...:sad:

Thanks to everyone for their help so far...but what I really need to look at is a seperate working program that is longer than say twenty lines of code and has been put into a GUI of its own so that I can work out how to organize my own GUI. As I've said there seems to be no programs longer than a "Hello World" example to work with. I've only had about three weeks experience with the Tk code but most of the documentation is about building your own GUI but nothing is there about HOW to put a seperate working program into a GUI.:sad:
If someone could post an example/s of of how to do this I would be most grateful for their help.:lol:

"Ooie Gooie was a worm, a worm was Ooie Gooie. One day Ooie was lying on a train line when along came a train...OOOOOie GOOOOie!!!"

I'm confused about your problem. Can you post what you have so far? Perhaps it's just a matter of linking callbacks to your buttons.

Jeff

G'day Jeff,

Below is one version of the code I have tried. I've tried about 20 different versions but can't seem to get it to work at all because the main Idle GUI keeps taking over.

import random
from Tkinter import *
root = Tk()
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 " "
    answer1 = Button(root, text = 'NO, HIGHER THAN %d. (Turn No.%i of 13)', fg = 'red')
    answer1.grid()
    answer2 = Button(root, text = 'NO, HIGHER THAN %d. (Turn No.%i of 13)', fg = 'green')
    answer2.grid()
    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)

Thanks for your reply.

P.S. Jeff,

I have tried using you're example code in my program but it will not "take" at all. Maybe because they're working on two different threads as I'm sure that the "while" statement is interfering with the Tk GUI in some way...and I really don't know how to use "callbacks" etc. That's why I wanted to see other working codes that had been put into an independent GUI to see how they have worked their "callbacks" etc. Thanks again Jeff, if you're still unsure about what I'm trying to do please don't hesitate to post.

In a GUI print and input are replaced by label and entry components. Here is a typical example ...

# a simple Tkinter number guessing game
 
from Tkinter import *
import random
 
def click():
    # get the number in the entry area
    num = int(enter1.get())
    # check it out
    if num > rn:
        label3['text'] = 'Guessed too high!  Enter another number!'
    elif num < rn:
        label3['text'] = 'Guessed too low!  Enter another number!'
    else:
        label3['text'] = 'Guessed correctly!'
    
# create the main/root window
root = Tk()
root.title()
 
# pick a random integer from 1 to 10
rn = random.randrange(1, 11)
 
# let the user know what is going on
label1 = Label(root, text="Guess a number between 1 and 10")
label1.grid(row=1, column=1, padx=8, pady=8)
 
# prompt the user
label2 = Label(root, text="Enter an integer number: ")
label2.grid(row=2, column=1, padx=20, pady=8)
 
# this your input area
enter1 = Entry(root, width=5)
enter1.grid(row=2, column=2, padx=8)
enter1.focus()
 
# this is your mouse action button, right-click it to execute command
button1 = Button(root, text=" Press to check number ", command=click)
button1.grid(row=3, column=1, pady=8)
 
# the result displays here
label3 = Label(root, text="", bg='yellow')
label3.grid(row=4, column=1, columnspan=2, pady=8)
 
# start the mouse/keyboard event loop
root.mainloop()
commented: A lifesaver, very helpful. +1

Hello wall,

Many thanks vegaseat and also Jeff. This'll help me as I couldn't work out why the Python code wouldn't just open in an independent Tk GUI as is...I guess I'll have to learn a lot more about the Tk code.
I'll try to re-adjust my program following both your guidelines...

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.