Stuck in GUI

Thread Solved

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

Stuck in GUI

 
0
  #1
Apr 10th, 2007
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...

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.
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!!!"
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: Stuck in GUI

 
0
  #2
Apr 10th, 2007
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
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: Stuck in GUI

 
0
  #3
Apr 10th, 2007
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.

  1. import random
  2. from Tkinter import *
  3. root = Tk()
  4. number = random.randint(1, 10000)
  5. running = True
  6. run=""
  7. attempts=0
  8. while running:
  9. guess = int(raw_input('Enter an integer : '))
  10. print
  11. attempts = attempts + 1
  12. WORDS=("Get it before ten tries or the world will explode!",\
  13. "The next one is the 70th try, hurry!", "400 guesses later...",\
  14. "A guess a day keeps the doctor away!", "Count to ten , backwards",\
  15. "Wake me up when you're finished.", "Better luck next time.",\
  16. "It's getting late, hurry up!", "One more and we're finished...",\
  17. "I'm running out of things to say.", "So close, yet so far!",\
  18. "Nearly got it, maybe it's the next guess.",)
  19. if attempts==attempts >0:
  20. print random.choice(WORDS)
  21. print "----------------------------------------------------------------------"
  22. print " "
  23. answer1 = Button(root, text = 'NO, HIGHER THAN %d. (Turn No.%i of 13)', fg = 'red')
  24. answer1.grid()
  25. answer2 = Button(root, text = 'NO, HIGHER THAN %d. (Turn No.%i of 13)', fg = 'green')
  26. answer2.grid()
  27. if attempts == attempts > 13:
  28. guess == number
  29. import winsound
  30. import time
  31. time.sleep(1.5)
  32. winsound.PlaySound("SystemExit", winsound.SND_ALIAS)
  33. print " "
  34. print "(The number was %i)" % (number)
  35. print " "
  36. print "***********************"
  37. print "BETTER LUCK NEXT TIME."
  38. print "***********************"
  39. print " "
  40. attempts=0
  41. run = raw_input("\n'run' again or 'exit' ")
  42. if run == "exit":
  43. break
  44. if guess == number:
  45. print " "
  46. print "********************************************"
  47. print 'CONGRATULATIONS, YOU GUESSED IT IN %i TRIES!' % (attempts)
  48. print "********************************************"
  49. attempts=0
  50. run = raw_input("\n'run' again or 'exit' ")
  51. if run == "exit":
  52. break
  53. elif run == "run":
  54. number = random.randint(1, 10000)
  55. random.choice(WORDS)
  56. continue
  57. elif guess < number:
  58. print 'NO, HIGHER THAN %d. (Turn No.%i of 13)' % (guess, attempts)
  59. else:
  60. print 'NO, LOWER THAN %d. (Turn No.%i of 13)' % (guess, attempts)
Thanks for your reply.
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: Stuck in GUI

 
0
  #4
Apr 10th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,977
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: 920
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: Stuck in GUI

 
1
  #5
Apr 11th, 2007
In a GUI print and input are replaced by label and entry components. Here is a typical example ...
  1. # a simple Tkinter number guessing game
  2.  
  3. from Tkinter import *
  4. import random
  5.  
  6. def click():
  7. # get the number in the entry area
  8. num = int(enter1.get())
  9. # check it out
  10. if num > rn:
  11. label3['text'] = 'Guessed too high! Enter another number!'
  12. elif num < rn:
  13. label3['text'] = 'Guessed too low! Enter another number!'
  14. else:
  15. label3['text'] = 'Guessed correctly!'
  16.  
  17. # create the main/root window
  18. root = Tk()
  19. root.title()
  20.  
  21. # pick a random integer from 1 to 10
  22. rn = random.randrange(1, 11)
  23.  
  24. # let the user know what is going on
  25. label1 = Label(root, text="Guess a number between 1 and 10")
  26. label1.grid(row=1, column=1, padx=8, pady=8)
  27.  
  28. # prompt the user
  29. label2 = Label(root, text="Enter an integer number: ")
  30. label2.grid(row=2, column=1, padx=20, pady=8)
  31.  
  32. # this your input area
  33. enter1 = Entry(root, width=5)
  34. enter1.grid(row=2, column=2, padx=8)
  35. enter1.focus()
  36.  
  37. # this is your mouse action button, right-click it to execute command
  38. button1 = Button(root, text=" Press to check number ", command=click)
  39. button1.grid(row=3, column=1, pady=8)
  40.  
  41. # the result displays here
  42. label3 = Label(root, text="", bg='yellow')
  43. label3.grid(row=4, column=1, columnspan=2, pady=8)
  44.  
  45. # start the mouse/keyboard event loop
  46. root.mainloop()
May 'the Google' be with you!
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: Stuck in GUI

 
0
  #6
Apr 11th, 2007
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...
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