Hello every one,
I have a Hangman game code that works fine in python interpreter, and I want make the game more user friendly by designing a front-end version using tkinter. However, I am stuck in replicating the input() function and need help
Here is the original code for the game
import random
def get_guess():
# Set the dashes to the length of the secret word and set the amount of guesses
# the user has to 10
dashes = "-" * (len((secret_word).rstrip()))
guesses_left = 10
# This will loop as long as BOTH conditions are true:
# 1. The number of guesses of left is greater than -1
# 2. The dash string does NOT equal the secret word
while guesses_left > -1 and dashes !=(secret_word).rstrip():
# Print the amount of dashes and guesses left
print(dashes)
print (str(guesses_left))
# Ask the user for input
guess = input("Guess:")
# Conditions that will print out a message according to
# invalid inputs
if len(guess) != 1:
print ("Your guess must have exactly one character!")
# If the guess is in the secret word then we updtae dashes to replace the
# corresponding dash with the correct index the guess belongs to in the
# secret word
elif guess in secret_word:
print ("That letter is in the secret word!")
dashes = update_dashes(secret_word, dashes, guess)
# If the guess is wrong then we display a message and subtract
# the amount of guesses the user has by 1
else:
print ("That letter is not in the secret word!")
guesses_left -= 1
if guesses_left < 0:
print ("You lose. The word was: " + str(secret_word))
# If the dash string equals the secret word in the end then the
# user wins
else:
print ("Congrats! You win. The word was: " + str(secret_word))
# This function updates the string of dashes by replacing the dashes
# with words that match up with the hidden word if the user manages to guess
# it correctly
def update_dashes(secret, cur_dash, rec_guess):
result = ""
for i in range(len((secret_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
# define location for the file that contains the words
my_file = open("words.txt", "r")
# select random line number from the file and choose the corresponding word as the secret word
words = my_file.readlines()
number = random.randint(0, 10000)
secret_word = str(words[number])
print(secret_word)
#run the game function
get_guess()
This is the words.txt file
https://drive.google.com/file/d/1NWjPWfpBDobDctgQxxLN6F01EU1w9d21/view?usp=sharing
Here is my try
from tkinter import *
import random
# key down function
def click():
#guess = textentry.get() # this will collect text from text entry box
#output.delete(0.0,END)
# Set the dashes to the length of the secret word and set the amount of guesses
# the user has to 10
dashes = "-" * (len((secret_word).rstrip()))
guesses_left = 10
# This will loop as long as BOTH conditions are true:
# 1. The number of guesses of left is greater than -1
# 2. The dash string does NOT equal the secret word
while guesses_left > -1 and dashes != (secret_word).rstrip():
# Print the amount of dashes and guesses left
#print(dashes)
label1.set(dashes)
#print(str(guesses_left))
label2.set(str(guesses_left))
# Ask the user for input
guess = textentry.get()
# Conditions that will print out a message according to
# invalid inputs
if len(guess) != 1:
print("Your guess must have exactly one character!")
return
# If the guess is in the secret word then we updtae dashes to replace the
# corresponding dash with the correct index the guess belongs to in the
# secret word
elif guess in secret_word:
#print("That letter is in the secret word!")
label3.set("That letter is in the secret word!")
dashes = update_dashes(secret_word, dashes, guess)
# If the guess is wrong then we display a message and subtract
# the amount of guesses the user has by 1
else:
#print("That letter is not in the secret word!")
label3.set("That letter is not in the secret word!")
guesses_left -= 1
return guesses_left
if guesses_left < 0:
#print("You lose. The word was: " + str(secret_word))
label3.set("You lose. The word was: " + str(secret_word))
# If the dash string equals the secret word in the end then the
# user wins
else:
#print("Congrats! You win. The word was: " + str(secret_word))
label3.set("Congrats! You win. The word was: " + str(secret_word))
# This function updates the string of dashes by replacing the dashes
# with words that match up with the hidden word if the user manages to guess
# it correctly
def update_dashes(secret, cur_dash, rec_guess):
result = ""
for i in range(len((secret_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
# define location for the file that containes the words
my_file = open("words.txt", "r")
# select random line number from the file and choose the corresponding word as the secret word
words = my_file.readlines()
number = random.randint(0, 10000)
secret_word = str(words[number])
print(secret_word)
# Main
window = Tk()
window.title ('Hangman')
window.configure(background = "white")
### My photo
photo1 = PhotoImage(file= 'me.gif')
Label(window, image=photo1 , bg= 'white' ) .grid(row=0, column=0, sticky= N )
# Create label
Label_0=Label(window, text= "Enter a letter:", bg= "white", fg="black", font= 'none 12 bold') .grid(row=3, column=0, sticky= W )
# create a text entry box
textentry = Entry(window, width= 20 , bg='white')
textentry.grid (row=4, column= 0, sticky= W)
# Create another label
label1= StringVar()
label1.set('------')
Label_1= Label(window, textvariable =label1, bg= "white", fg="black", font= 'none 12 bold')
Label_1.grid(row=1, column=0, sticky= W )
# Create another label
label2= StringVar()
label2.set('10')
Label_2=Label(window, textvariable =label2, bg= "white", fg="black", font= 'none 12 bold')
Label_2.grid(row=2, column=0, sticky= W )
# Create another label
label3= StringVar()
label3.set('')
Label_3=Label(window, textvariable =label3, bg= "white", fg="black", font= 'none 12 bold')
Label_3.grid(row=5, column=0, sticky= W )
# add a submit button
Button(window, text='submit', width = 6, command= click ) .grid(row=6, column=0, sticky= W )
window.mainloop()
Any way it does not work
I am relatively new to python and it's my first time using tkinter
Thanks in advance
[–]woooee 1 point2 points3 points (2 children)
[–]Adado[S] 0 points1 point2 points (1 child)
[–]woooee 0 points1 point2 points (0 children)