so i tried adding a defintion for the reset command but it doesn't seemed like it works
other method was moving the defintion around the localfunction which didn't work and also affects other elements

# Guessing Game
from tkinter import *
from tkinter import ttk
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):

        """ 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 = E)
        """ 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 = E)
        # 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 = ttk.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 Reset button
        self.reset_bttn = ttk.Button(self, text = "Reset", command = self.reset)
        self.reset_bttn.grid(row = 6, column = 1, sticky = E)

        """ 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 = E)
        # 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 = E)
        # 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 = E)
        # 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 = E)

    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
        elif int(guess) < int(number):
                result_msg = "Higher..."
                tries += 1
        elif 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. Come back later and try again."
            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)

    def reset(self):
        global number
        number = random.randrange (100) + 1
        result_msg = "Game reset. Start guessing!!!"

# Main manager
root = Tk()
root.title("Guessing Game")
root.geometry("365x225")
app = Application(root)
root.mainloop()

Recommended Answers

You can write the reset function like this

    def reset(self):
        global number, tries
        number = random.randrange (100) + 1
        result_msg = "Game reset. Start guessing!!!"
        tries = 0
        self.update_display(guess='', result=result_msg, tries='')

where the update_display() method fills the Text widgets

    def update_display(self, …
Jump to Post

Well exactly where you put this function in the above code.

Jump to Post

Yes it looks correct. Does it work the way you want?

Jump to Post

All 10 Replies

def reset(self):
global number
number = random.randrange (100) + 1
result_msg = "game reset. start guessing"

this is the def i want to implement

You can write the reset function like this

    def reset(self):
        global number, tries
        number = random.randrange (100) + 1
        result_msg = "Game reset. Start guessing!!!"
        tries = 0
        self.update_display(guess='', result=result_msg, tries='')

where the update_display() method fills the Text widgets

    def update_display(self, **kwargs):
        for key, display in self.display_map.items():
            if key in kwargs:
                display.delete(0.0, END)
                display.insert(0.0, kwargs[key])

For this to work, you need to build a dictionary at the end of the create_widgets() method

        self.display_map = {
            'welcome': self.display1_txt,
            'guess': self.display2_txt,
            'result': self.display3_txt,
            'tries': self.display4_txt,
        }

You can then replace lines 87-95 in your code with

        # Display
        self.update_display(
            welcome=welcome_msg, guess=guess_msg,
            result=result_msg, tries=tries_msg)

Many things can be improved, for example you could have a list of text widgets instead of display1_txt ....

and where shoud i put the def reset function?

Well exactly where you put this function in the above code.

is this right? sorry i am not too sure on this stuff

# Guessing Game
from tkinter import *
from tkinter import ttk
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):

        """ 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 = E)
        """ 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 = E)
        # 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 = ttk.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)

        self.reset_bttn = ttk.Button(self, text = "Reset", command = self.reset)
        self.reset_bttn.grid(row = 6, column = 0, sticky = E)

        """ 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 = E)
        # 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 = E)
        # 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 = E)
        # 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 = E)
        self.display_map = {
            'welcome': self.display1_txt,
            'guess': self.display2_txt,
            'result': self.display3_txt,
            'tries': self.display_txt,
            }   

    def update_display(slef, **kwargs):
        for key, display in self.display_map.items():
            if key in kwargs:
                display.delete(0.0, END)
                display.insert(0.0, kwargs[key])

    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
        elif int(guess) < int(number):
                result_msg = "Higher..."
                tries += 1
        elif 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. Come back later and try again."
            result_msg = " "
            tries_msg = " "

        # Display
        self.update_display(
            welcome=welcome_msg, guess=guess_msg, result=result_msg, tries=tries_msg)

    def reset(self):
        global number, tries
        number = random.randrange (100) + 1
        result_msg = "Game reset. Start guessing!!!"
        tries = 0
        self.update_display(guess='', result=result_msg, tries='')    

# Main manager
root = Tk()
root.title("Guessing Game")
root.geometry("365x225")
app = Application(root)
root.mainloop()

Yes it looks correct. Does it work the way you want?

File "C:\Users\mrbee\Desktop\SDD PROJ\TE.py", line 102, in reveal
    welcome=welcome_msg, guess=guess_msg, result=result_msg, tries=tries_msg)
  File "C:\Users\mrbee\Desktop\SDD PROJ\TE.py", line 70, in update_display
    for key, display in self.display_map.items():
NameError: name 'self' is not defined

this shows up when i submit the guess

and before the application shows up this pops up

Traceback (most recent call last):
  File "C:\Users\mrbee\Desktop\SDD PROJ\TE.py", line 116, in <module>
    app = Application(root)
  File "C:\Users\mrbee\Desktop\SDD PROJ\TE.py", line 13, in __init__
    self.create_widgets()
  File "C:\Users\mrbee\Desktop\SDD PROJ\TE.py", line 64, in create_widgets
    'tries': self.display_txt,
AttributeError: 'Application' object has no attribute 'display_txt'

It is a typo at line 64, it should be display4_txt. Try to understand the code.

all good now thank you very much.

Be a part of the DaniWeb community

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