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

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

guess = int(self.guess_ent.get())

Which will convert the value in the textbox from a string to an integer. Also, this block:

tries = 1
tries = str(tries)
tries_msg = 0
tries_msg = str(tries_msg)
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?

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 ...

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

from Tkinter import *
import random
number = random.randrange (100) + 1

tries = 0

class Application(Frame):
    """ GUI application which can retrieve an auto 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 = 2, column = 0, sticky = W)

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

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

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

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

        """ RESET """
        
        # Create submit button
        self.reset_bttn = Button(self, text = "Reset", command = self.reveal)
        self.reset_bttn.grid(row = 6, column = 1, 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 = 8, column = 0, columnspan = 2, sticky = W)

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

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

        # Create text widget to display tries_msg
        self.display4_txt = Text(self, width = 45, height = 2, wrap = WORD)
        self.display4_txt.grid(row = 11, column = 0, columnspan = 2, sticky = W)
        
    def reveal(self):

        global tries
        name = self.name_ent.get()
        guess = self.guess_ent.get()


        if int(guess) > int(number):
            result_msg = "Lower ..."
            tries += 1
        if int(guess) < int(number):
            result_msg = "Higher ..."
            tries += 1
        if int(guess) == int(number):
            result_msg = "You got it."
            tries += 1

        welcome_msg = "Welcome " + name 
        guess_msg = " Your guess was: " + guess
        tries_msg = "You have had " + str(tries) + " tries."

        if tries > 10:
            welcome_msg = "End of Game."
            guess_msg = "You had too many tires."
            result_msg = " "
            tries_msg = " "

        # 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.delete(0.0, END)
        self.display4_txt.insert(0.0, tries_msg)
    
# Main manager
root = Tk()
root.title("Guessing Game")
root.geometry("300x225")

app = Application(root)

root.mainloop()

i think you have forgotten to add and define the reset command???

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.