GUI Python - A Guessing Game

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Dec 2007
Posts: 2
Reputation: Hillzy is an unknown quantity at this point 
Solved Threads: 0
Hillzy Hillzy is offline Offline
Newbie Poster

GUI Python - A Guessing Game

 
0
  #1
Dec 3rd, 2007
At the moment i ahve a task where i have to make a guessing game in GUI python. So it will generate a random number and also when a certain number of tries have been reached it will stop. The problems i am having as of now is that:
1) The counter when adding 1 simply goes, 111111
2) it always says lower even if i type 1

Here is my coding for now

# Guessing Game
# Demonstrates text and entry widgets, and the grid layout manager.

from Tkinter import *
import random

class Application(Frame):
""" GUI application which can retrieve an autp number to guess. """
def __init__(self, master):
""" Initialize the frame. """
Frame.__init__(self, master)
self.grid()
self.create_widgets()

def create_widgets(self):
""" Create button, text, and entry widgets. """

""" Instruction Label """

# Create instruction label for Program
self.inst_lbl = Label(self, text = "Follow the Steps")
self.inst_lbl.grid(row = 0, column = 0, columnspan = 2, sticky = W)


""" Player Name """

# Create label for name
self.name_lbl = Label(self, text = "Player Name: ")
self.name_lbl.grid(row = 1, column = 0, sticky = W)

# Create entry widget to accept name
self.name_ent = Entry(self)
self.name_ent.grid(row = 1, column = 1, sticky = W)




""" Guess Input """

# Create label for entering Guess
self.guess_lbl = Label(self, text = "Enter your Guess.")
self.guess_lbl.grid(row = 3, column = 0, sticky = W)

# Create entry widget to accept Guess
self.guess_ent = Entry(self)
self.guess_ent.grid(row = 3, column = 1, sticky = W)

# Create a space
self.gap1_lbl = Label(self, text = " ")
self.gap1_lbl.grid(row = 4, column = 0, sticky = W)

""" Submit Button """

# Create submit button
self.submit_bttn = Button(self, text = "Submit", command = self.reveal)
self.submit_bttn.grid(row = 5, column = 0, sticky = W)

# Create a space
self.gap2_lbl = Label(self, text = " ")
self.gap2_lbl.grid(row = 6, column = 0, sticky = W)

""" Display """

# Create text widget to display welcome_msg
self.display1_txt = Text(self, width = 45, height = 1, wrap = WORD)
self.display1_txt.grid(row = 7, column = 0, columnspan = 2, sticky = W)


# Create text widget to display welcome_msg
self.display2_txt = Text(self, width = 45, height = 1, wrap = WORD)
self.display2_txt.grid(row = 8, column = 0, columnspan = 2, sticky = W)

# Create text widget to display welcome_msg
self.display3_txt = Text(self, width = 45, height = 2, wrap = WORD)
self.display3_txt.grid(row = 9, column = 0, columnspan = 2, sticky = W)

# Create text widget to display welcome_msg
self.display4_txt = Text(self, width = 45, height = 2, wrap = WORD)
self.display4_txt.grid(row = 10, column = 0, columnspan = 2, sticky = W)


def reveal(self):

name = self.name_ent.get()
guess = self.guess_ent.get()
welcome_msg = "Welcome " + name
guess_msg = " Your guess was: " + guess
number = 10

tries = 1
tries = str(tries)
tries_msg = 0
tries_msg = str(tries_msg)
tries_msg = int(tries_msg) + int(tries)

if guess > number:
result_msg = "Lower ..."
elif guess < number:
result_msg = "Higher ..."
else:
result_msg = "You got it."


# Display
self.display1_txt.delete(0.0, END)
self.display1_txt.insert(0.0, welcome_msg)
self.display2_txt.delete(0.0, END)
self.display2_txt.insert(0.0, guess_msg)
self.display3_txt.delete(0.0, END)
self.display3_txt.insert(0.0, result_msg)
self.display4_txt.insert(0.0, tries_msg)


# Main manager
root = Tk()
root.title("Guessing Game")
root.geometry("300x200")

app = Application(root)

root.mainloop()




i believe the error is in the lines

tries = 1
tries = str(tries)
tries_msg = 0
tries_msg = str(tries_msg)
tries_msg = int(tries_msg) + int(tries)

if guess > number:
result_msg = "Lower ..."
elif guess < number:
result_msg = "Higher ..."
else:
result_msg = "You got it."

and this is what i have been changing around for the last hour to various different ways ... i can easily make the guessing game in the python shell but gui is giving me hell, PLEASE HELP.

P.S. It dosnt seem to have indented right ... but my indenting is correct
Last edited by Hillzy; Dec 3rd, 2007 at 6:48 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,292
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 178
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: GUI Python - A Guessing Game

 
0
  #2
Dec 5th, 2007
Put the code between code tags. Read the lightgray message in the background of the Quick Reply field below.

Or read:
http://www.daniweb.com/forums/announcement114-3.html
Last edited by sneekula; Dec 5th, 2007 at 3:59 pm.
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,614
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 131
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: GUI Python - A Guessing Game

 
0
  #3
Dec 6th, 2007
simple problem (you're gunna kick yourself)

in self.reveal, you don't convert guess to an integer (it's still a string, because textbox input are always strings be default). So when you do the incrementation, it concatenates instead, and when it checks if it's greater than it always says yes since it's seen as text and not a number value (does this make sense?)

Anyway, to fix, change that line to
  1. guess = int(self.guess_ent.get())
Which will convert the value in the textbox from a string to an integer. Also, this block:
  1. tries = 1
  2. tries = str(tries)
  3. tries_msg = 0
  4. tries_msg = str(tries_msg)
  5. tries_msg = int(tries_msg) + int(tries)
is the root of a lot of your problems. Why don't you tell me what it's trying to do so we can re-write it in a better way?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 2
Reputation: Hillzy is an unknown quantity at this point 
Solved Threads: 0
Hillzy Hillzy is offline Offline
Newbie Poster

Re: GUI Python - A Guessing Game

 
0
  #4
Dec 8th, 2007
Hey thx for the help ... i managed to solve both prblems though.
probelm 1
solved by setting tries up the very top and then making it a global variable.
problem 2
set the random number up the top outside of the loop. and also seetin guess as an integer as scru suggested.

so in the end my new code is ...
  1. # Guessing Game
  2. # Demonstrates text and entry widgets, and the grid layout manager.
  3.  
  4. from Tkinter import *
  5. import random
  6. number = random.randrange (100) + 1
  7.  
  8. tries = 0
  9.  
  10. class Application(Frame):
  11. """ GUI application which can retrieve an auto number to guess. """
  12. def __init__(self, master):
  13. """ Initialize the frame. """
  14. Frame.__init__(self, master)
  15. self.grid()
  16. self.create_widgets()
  17.  
  18. def create_widgets(self):
  19. """ Create button, text, and entry widgets. """
  20.  
  21. """ Instruction Label """
  22.  
  23. # Create instruction label for Program
  24. self.inst_lbl = Label(self, text = "Follow the Steps")
  25. self.inst_lbl.grid(row = 0, column = 0, columnspan = 2, sticky = W)
  26.  
  27. """ Player Name """
  28.  
  29. # Create label for name
  30. self.name_lbl = Label(self, text = "Player Name: ")
  31. self.name_lbl.grid(row = 1, column = 0, sticky = W)
  32.  
  33. # Create entry widget to accept name
  34. self.name_ent = Entry(self)
  35. self.name_ent.grid(row = 1, column = 1, sticky = W)
  36.  
  37.  
  38. """ Guess Input """
  39.  
  40. # Create label for entering Guess
  41. self.guess_lbl = Label(self, text = "Enter your Guess.")
  42. self.guess_lbl.grid(row = 2, column = 0, sticky = W)
  43.  
  44. # Create entry widget to accept Guess
  45. self.guess_ent = Entry(self)
  46. self.guess_ent.grid(row = 2, column = 1, sticky = W)
  47.  
  48. # Create a space
  49. self.gap1_lbl = Label(self, text = " ")
  50. self.gap1_lbl.grid(row = 3, column = 0, sticky = W)
  51.  
  52. """ Submit Button """
  53.  
  54. # Create submit button
  55. self.submit_bttn = Button(self, text = "Submit", command = self.reveal)
  56. self.submit_bttn.grid(row = 6, column = 0, sticky = W)
  57.  
  58. # Create a space
  59. self.gap2_lbl = Label(self, text = " ")
  60. self.gap2_lbl.grid(row = 7, column = 0, sticky = W)
  61.  
  62. """ RESET """
  63.  
  64. # Create submit button
  65. self.reset_bttn = Button(self, text = "Reset", command = self.reveal)
  66. self.reset_bttn.grid(row = 6, column = 1, sticky = W)
  67.  
  68. """ Display """
  69.  
  70. # Create text widget to display welcome_msg
  71. self.display1_txt = Text(self, width = 45, height = 1, wrap = WORD)
  72. self.display1_txt.grid(row = 8, column = 0, columnspan = 2, sticky = W)
  73.  
  74. # Create text widget to display guess_msg
  75. self.display2_txt = Text(self, width = 45, height = 1, wrap = WORD)
  76. self.display2_txt.grid(row = 9, column = 0, columnspan = 2, sticky = W)
  77.  
  78. # Create text widget to display result_msg
  79. self.display3_txt = Text(self, width = 45, height = 2, wrap = WORD)
  80. self.display3_txt.grid(row = 10, column = 0, columnspan = 2, sticky = W)
  81.  
  82. # Create text widget to display tries_msg
  83. self.display4_txt = Text(self, width = 45, height = 2, wrap = WORD)
  84. self.display4_txt.grid(row = 11, column = 0, columnspan = 2, sticky = W)
  85.  
  86. def reveal(self):
  87.  
  88. global tries
  89. name = self.name_ent.get()
  90. guess = self.guess_ent.get()
  91.  
  92.  
  93. if int(guess) > int(number):
  94. result_msg = "Lower ..."
  95. tries += 1
  96. if int(guess) < int(number):
  97. result_msg = "Higher ..."
  98. tries += 1
  99. if int(guess) == int(number):
  100. result_msg = "You got it."
  101. tries += 1
  102.  
  103. welcome_msg = "Welcome " + name
  104. guess_msg = " Your guess was: " + guess
  105. tries_msg = "You have had " + str(tries) + " tries."
  106.  
  107. if tries > 10:
  108. welcome_msg = "End of Game."
  109. guess_msg = "You had too many tires."
  110. result_msg = " "
  111. tries_msg = " "
  112.  
  113. # Display
  114. self.display1_txt.delete(0.0, END)
  115. self.display1_txt.insert(0.0, welcome_msg)
  116. self.display2_txt.delete(0.0, END)
  117. self.display2_txt.insert(0.0, guess_msg)
  118. self.display3_txt.delete(0.0, END)
  119. self.display3_txt.insert(0.0, result_msg)
  120. self.display4_txt.delete(0.0, END)
  121. self.display4_txt.insert(0.0, tries_msg)
  122.  
  123. # Main manager
  124. root = Tk()
  125. root.title("Guessing Game")
  126. root.geometry("300x225")
  127.  
  128. app = Application(root)
  129.  
  130. root.mainloop()
Last edited by Hillzy; Dec 8th, 2007 at 4:08 am.
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



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC