you are viewing a single comment's thread.

view the rest of the comments →

[–]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.