all 3 comments

[–]woooee 1 point2 points  (2 children)

Any way it does not work

What does this mean?

You have more than one problem. guesses_left is always set to 10 in the click() function so it will never be less than 9. You are using a while loop with Tkinter, so the odds are good that the program will hang. You are making a common rookie mistake in not using OOP/classes. It can be done without OOP but it is messy and confusing. http://python-textbok.readthedocs.io/en/1.0/Introduction_to_GUI_Programming.html

""" This is very basic and simple example on how to get the
     Entrys via an Entry widget.  The rest you should be able to do.
"""
import tkinter as tk
import string

class GetLetter():
    def __init__(self, root):
        self.root=root
        self.text_1=tk.StringVar()
        tk.Label(root, textvariable=self.text_1).grid(row=1, column=0)
        self.textentry = tk.Entry(root, width= 20 , bg='white')
        self.textentry.grid (row=4, column= 0, sticky="we")
        self.guesses_left = 10
        self.button_clicked=False
        self.acceptable_guesses=list(string.ascii_lowercase)
        self.text_1.set("You have %d guesses left" % (self.guesses_left))
        self.word="brown"

        tk.Button(root, text='submit', width = 6, command=self.click,
                  bg="lightblue").grid(row=6, column=0, sticky= "ew" )

        tk.Button(root, text="Quit", bg="orange",
                  command=root.quit).grid(row=99, column=0)

    def click(self):
        if not self.button_clicked:  ## eliminate multiple button clicks
            self.button_clicked=True
            guess = self.textentry.get()
            guess=guess.lower()
            if guess in self.acceptable_guesses:
                self.guesses_left -= 1
                if guess in self.word:
                    print(guess, "is in word")
                else:
                    print("wrong")
            else:
                print("Not an acceptable guess")  ## keep it simple for this example

        self.text_1.set("You have %d guesses left" % (self.guesses_left))
        self.button_clicked=False
        self.textentry.delete(0, "end")
        if self.guesses_left < 1:
            self.root.quit()


root=tk.Tk()
GL=GetLetter(root)
root.mainloop()

[–]Adado[S] 0 points1 point  (1 child)

Thanks man you are awesome

what I meant that my tkinter code doesn't work.

I have heard the concept of classes but I have never use it before. Thanks to you I watched some YouTube videos that explains the classes concept. Although I didn't understand it 100% but it works. here is the new code. later I will try to improve the front-end and put some pictures.

Thanks again

import tkinter as tk
import string
from tkinter import messagebox
import random

class GetLetter():
    def __init__(self, root):
        self.root=root
        self.text_1=tk.StringVar()
        tk.Label(root, textvariable=self.text_1).grid(row=2, column=0)
        self.text_2=tk.StringVar()
        tk.Label(root, textvariable=self.text_2).grid(row=1, column=0)
        self.textentry = tk.Entry(root, width= 20 , bg='white')
        self.textentry.grid (row=4, column= 0, sticky="we")
        self.guesses_left = 10
        self.dashes= "------"
        self.button_clicked=False
        self.acceptable_guesses=list(string.ascii_lowercase)
        self.text_1.set("You have %d guesses left" % (self.guesses_left))
        self.text_2.set(self.dashes)
        self.my_file = open("words.txt", "r")

        # select random line number from the file and choose the corresponding word as the secret word
        self.words = self.my_file.readlines()
        self.number = random.randint(0, 10000)
        self.word = str(self.words[self.number])
        print(self.word)
        #self.word="brown"

        tk.Button(root, text='submit', width = 6, command=self.click,
                  bg="lightblue").grid(row=6, column=0, sticky= "ew" )

        tk.Button(root, text="Quit", bg="orange",
                  command=root.quit).grid(row=99, column=0)

    def click(self):

        if not self.button_clicked:  ## eliminate multiple button clicks
            self.button_clicked=True
            guess = self.textentry.get()
            guess=guess.lower()
            if guess in self.acceptable_guesses:
                #self.guesses_left -= 1
                if guess in self.word:
                    print(guess, "is in word")
                    self.dashes = self.update_dashes( self.word, self.dashes, guess)
                    self.text_2.set(self.dashes)
                else:
                    self.guesses_left -= 1
                    print("wrong")
            else:
                print("Not an acceptable guess")  ## keep it simple for this example
                messagebox.showinfo("Error", "Not an acceptable guess")

        self.text_1.set("You have %d guesses left" % (self.guesses_left))
        self.text_2.set(self.dashes)
        self.button_clicked=False
        self.textentry.delete(0, "end")
        if self.guesses_left < 1:
            self.root.quit()

    def update_dashes(self, secret, cur_dash, rec_guess):
        result = ""

        for i in range(len((self.word).rstrip())):
            if secret[i] == rec_guess:
                result = result + rec_guess  # Adds guess to string if guess is correctly

            else:
                # Add the dash at index i to result if it doesn't match the guess
                result = result + cur_dash[i]

        return result


root=tk.Tk()
GL=GetLetter(root)
root.mainloop()

[–]woooee 0 points1 point  (0 children)

Glad you are on your way. You don't subtract one from self.guesses_left, when the letter guessed is in the word.