2019-10-23 20:10:03 +00:00
|
|
|
import PySimpleGUI as sg
|
2018-11-04 18:21:58 +00:00
|
|
|
import random
|
|
|
|
import string
|
|
|
|
|
|
|
|
"""
|
|
|
|
Demo application to show how to draw rectangles and letters on a Graph Element
|
|
|
|
This demo mocks up a crossword puzzle board
|
|
|
|
It will place a letter where you click on the puzzle
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
BOX_SIZE = 25
|
|
|
|
|
|
|
|
layout = [
|
2019-10-23 20:10:03 +00:00
|
|
|
[sg.Text('Crossword Puzzle Using PySimpleGUI'), sg.Text('', key='-OUTPUT-')],
|
|
|
|
[sg.Graph((800, 800), (0, 450), (450, 0), key='-GRAPH-',
|
|
|
|
change_submits=True, drag_submits=False)],
|
|
|
|
[sg.Button('Show'), sg.Button('Exit')]
|
|
|
|
]
|
2018-11-04 18:21:58 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window = sg.Window('Window Title', layout, finalize=True)
|
2018-11-04 18:21:58 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
g = window['-GRAPH-']
|
2018-11-04 18:21:58 +00:00
|
|
|
|
|
|
|
for row in range(16):
|
|
|
|
for col in range(16):
|
2019-10-23 20:10:03 +00:00
|
|
|
if random.randint(0, 100) > 10:
|
|
|
|
g.draw_rectangle((col * BOX_SIZE + 5, row * BOX_SIZE + 3), (col * BOX_SIZE + BOX_SIZE + 5, row * BOX_SIZE + BOX_SIZE + 3), line_color='black')
|
2018-11-04 18:21:58 +00:00
|
|
|
else:
|
2019-10-23 20:10:03 +00:00
|
|
|
g.draw_rectangle((col * BOX_SIZE + 5, row * BOX_SIZE + 3), (col * BOX_SIZE + BOX_SIZE + 5, row * BOX_SIZE + BOX_SIZE + 3), line_color='black', fill_color='black')
|
2018-11-04 18:21:58 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
g.draw_text('{}'.format(row * 6 + col + 1),
|
|
|
|
(col * BOX_SIZE + 10, row * BOX_SIZE + 8))
|
2018-11-04 18:21:58 +00:00
|
|
|
|
|
|
|
while True: # Event Loop
|
2019-10-23 20:10:03 +00:00
|
|
|
event, values = window.read()
|
2018-11-04 18:21:58 +00:00
|
|
|
print(event, values)
|
2019-10-23 20:10:03 +00:00
|
|
|
if event in (None, 'Exit'):
|
2018-11-04 18:21:58 +00:00
|
|
|
break
|
2019-10-23 20:10:03 +00:00
|
|
|
mouse = values['-GRAPH-']
|
2018-11-04 18:21:58 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
if event == '-GRAPH-':
|
2018-11-04 18:21:58 +00:00
|
|
|
if mouse == (None, None):
|
|
|
|
continue
|
|
|
|
box_x = mouse[0]//BOX_SIZE
|
|
|
|
box_y = mouse[1]//BOX_SIZE
|
|
|
|
letter_location = (box_x * BOX_SIZE + 18, box_y * BOX_SIZE + 17)
|
|
|
|
print(box_x, box_y)
|
2019-10-23 20:10:03 +00:00
|
|
|
g.draw_text('{}'.format(random.choice(string.ascii_uppercase)),
|
|
|
|
letter_location, font='Courier 25')
|
2018-11-04 18:21:58 +00:00
|
|
|
|
2019-10-23 20:10:03 +00:00
|
|
|
window.close()
|