Minor bug
The easy
variable has the word "Hard" in the text description,
and vice versa:
easy = tk.Button(intro, text = 'Hard
hard = tk.Button(intro, text = 'Easy
I think it should be:
easy = tk.Button(intro, text = 'Easy
hard = tk.Button(intro, text = 'Hard
DRY
Lines like these are repeated in a couple places, with only the string changing, and also the string is the same for the 3 lines:
easy.config(state = 'disabled')
hard.config(state = 'disabled')
medium.config(state = 'disabled')
You can avoid the repetition by creating a function like:
def set_all_state(value):
easy .config(state = value)
hard .config(state = value)
medium.config(state = value)
In the difficulty
function, the expression len(word)
appears
several times. You can create a variable so that you only
evaluate the expression once:
word_len = len(word)
Naming
The PEP 8 style guide recommends camel_case
for function names. Change whenGuessed
to when_guessed
.
Layout
Move the functions to the top after the import
lines.
Having them in the middle of the code interrupts the natural flow of the
code (from a human readability standpoint).
Documentation
Add a doctring at the top of the file to summarize the purpose of the code:
"""
Hangman game using tkinter GUI with 3 difficulty levels.
"""
There is a hard-coded directory path buried deep in the code:
file = open(r"C:\Users\d_j_d\Desktop\words.txt", 'r').read().split('\n')
It would be a good idea to mention this in the header documentation for the user.
Add docstrings frfor your functions as well:
def difficulty(minimum, maximum):
"""
Set the difficulty level of the game.
Input minimum: minimum length of the word
Input maximum: maximum length of the word
"""