Skip to content

Commit ea29881

Browse files
authored
Merge pull request PySimpleGUI#5251 from PySimpleGUI/Dev-latest
New Demo Program - Wordle GUI
2 parents 4c8abee + 58e0b2c commit ea29881

1 file changed

Lines changed: 98 additions & 0 deletions

File tree

DemoPrograms/Demo_Game_Wordle.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import PySimpleGUI as sg
2+
import copy
3+
4+
"""
5+
Wordle GUI Demo
6+
7+
Enter characters for each position
8+
Press enter or click enter to submit a row
9+
10+
This is a prototype GUI of the front-end for WORDL
11+
It currently:
12+
* Takes input
13+
* Makes sure only characters
14+
* Automatically converts to upper case
15+
* Handles backspace
16+
* Checks for Enter key
17+
* Compares against a word (the constant "answer")
18+
* Color codes the submitted guess
19+
20+
To complete an application, you'll need to:
21+
* Supply a word to guess from list of words
22+
* Check if user's submission is a word (I think this is how WORDLE works)
23+
24+
Copyright 2022 PySimpleGUI
25+
"""
26+
27+
# Insert code to generate a word here
28+
answer = 'WORDS'
29+
30+
31+
def TextChar(value, key):
32+
return sg.Input(value, key=key, font='Courier 22', size=(1,1), disabled_readonly_background_color='gray', border_width=1, p=1, enable_events=True, disabled=True)
33+
34+
def main():
35+
layout = [[sg.Text('Wordle', font='_ 20')],
36+
[[TextChar('', (row, col)) for col in range(5)]for row in range(6)],
37+
[sg.B('Enter', bind_return_key=True)],
38+
[sg.Text('Or press enter', font='_ 10')]]
39+
40+
window = sg.Window("Wordle", layout, finalize=True, element_justification='c')
41+
42+
cur_row, correct = 0, False
43+
[window[(cur_row, col)].update(disabled=False) for col in range(5)]
44+
window.bind('<BackSpace>', '-BACKSPACE-')
45+
while True:
46+
event, values = window.read()
47+
if event == sg.WIN_CLOSED:
48+
break
49+
if isinstance(event, tuple):
50+
if len(values[event]):
51+
row, col = event
52+
char_input = values[event][-1]
53+
if not char_input.isalpha(): # if not a character input, remove the input
54+
window[event].update('')
55+
else:
56+
window[event].update(char_input.upper()[0]) # convert to uppercase
57+
if col < 4:
58+
window[(row, col+1)].set_focus() # Move to next position
59+
elif event == 'Enter' and cur_row < 5:
60+
guess = ''.join([values[(cur_row, j)] for j in range(5)])
61+
answer2 = copy.copy(answer)
62+
for i, letter in enumerate(guess):
63+
if letter == answer2[i]:
64+
window[(cur_row, i)].update(background_color='green', text_color='white')
65+
answer2 = answer2.replace(letter, '*')
66+
elif letter in answer2:
67+
window[(cur_row, i)].update(background_color='#C9B359', text_color='white')
68+
answer2 = answer2.replace(letter, '*')
69+
else:
70+
window[(cur_row, i)].update(background_color='gray', text_color='white')
71+
if guess == answer:
72+
correct = True
73+
break
74+
cur_row += 1 # Move to the next row
75+
[window[(cur_row, col)].update(disabled=False) for col in range(5)] # Enable inputs on next row
76+
window[(cur_row, 0)].set_focus() # Move to first position on row
77+
elif event == 'Enter' and cur_row == 5:
78+
correct = False
79+
break
80+
elif event == '-BACKSPACE-':
81+
current_focus = window.find_element_with_focus()
82+
current_key = current_focus.Key
83+
if isinstance(current_key, tuple):
84+
window[current_key].update('')
85+
if current_key[1] > 0:
86+
window[(current_key[0], current_key[1]-1)].set_focus()
87+
window[(current_key[0], current_key[1]-1)].update('')
88+
89+
90+
if correct:
91+
sg.popup('You win!')
92+
else:
93+
sg.popup(f'Sorry... the answer was {answer}')
94+
95+
window.close()
96+
97+
if __name__ == '__main__':
98+
main()

0 commit comments

Comments
 (0)