Corrected key name to use coding conventions. Added right click info. Display the mouse position in text for all graph events.

This commit is contained in:
PySimpleGUI 2021-02-03 15:48:59 -05:00
parent ed877df74b
commit 14b868caf2
1 changed files with 14 additions and 9 deletions

View File

@ -6,6 +6,8 @@ from PIL import ImageGrab
This demo shows how to use a Graph Element to (optionally) display an image and then use the This demo shows how to use a Graph Element to (optionally) display an image and then use the
mouse to "drag" and draw rectangles and circles. mouse to "drag" and draw rectangles and circles.
Copyright 2020 PySimpleGUI.org
""" """
def save_element_as_file(element, filename): def save_element_as_file(element, filename):
@ -35,7 +37,7 @@ def main():
[sg.R('Send to back', 1, key='-BACK-', enable_events=True)], [sg.R('Send to back', 1, key='-BACK-', enable_events=True)],
[sg.R('Bring to front', 1, key='-FRONT-', enable_events=True)], [sg.R('Bring to front', 1, key='-FRONT-', enable_events=True)],
[sg.R('Move Everything', 1, key='-MOVEALL-', enable_events=True)], [sg.R('Move Everything', 1, key='-MOVEALL-', enable_events=True)],
[sg.R('Move Stuff', 1, True, key='-MOVE-', enable_events=True)], [sg.R('Move Stuff', 1, key='-MOVE-', enable_events=True)],
[sg.B('Save Image', key='-SAVE-')], [sg.B('Save Image', key='-SAVE-')],
] ]
@ -46,8 +48,8 @@ def main():
key="-GRAPH-", key="-GRAPH-",
enable_events=True, enable_events=True,
background_color='lightblue', background_color='lightblue',
drag_submits=True), sg.Col(col) ], drag_submits=True), sg.Col(col, key='-COL-') ],
[sg.Text(key='info', size=(60, 1))]] [sg.Text(key='-INFO-', size=(60, 1))]]
window = sg.Window("Drawing and Moving Stuff Around", layout, finalize=True) window = sg.Window("Drawing and Moving Stuff Around", layout, finalize=True)
@ -58,17 +60,17 @@ def main():
dragging = False dragging = False
start_point = end_point = prior_rect = None start_point = end_point = prior_rect = None
graph.bind('<Button-3>', '+RIGHT+') graph.bind('<Button-3>', '+RIGHT+')
while True: while True:
event, values = window.read() event, values = window.read()
print(event, values) print(event, values)
if event == sg.WIN_CLOSED: if event == sg.WIN_CLOSED:
break # exit break # exit
if event in ('-MOVE-', '-MOVEALL-'): if event in ('-MOVE-', '-MOVEALL-'):
graph.Widget.config(cursor='fleur') graph.set_cursor(cursor='fleur') # not yet released method... coming soon!
# graph.set_cursor(cursor='fleur') # not yet released method... coming soon!
elif not event.startswith('-GRAPH-'): elif not event.startswith('-GRAPH-'):
# graph.set_cursor(cursor='left_ptr') # not yet released method... coming soon! graph.set_cursor(cursor='left_ptr') # not yet released method... coming soon!
graph.Widget.config(cursor='left_ptr')
if event == "-GRAPH-": # if there's a "Graph" event, then it's a mouse if event == "-GRAPH-": # if there's a "Graph" event, then it's a mouse
x, y = values["-GRAPH-"] x, y = values["-GRAPH-"]
@ -109,12 +111,15 @@ def main():
elif values['-BACK-']: elif values['-BACK-']:
for fig in drag_figures: for fig in drag_figures:
graph.send_figure_to_back(fig) graph.send_figure_to_back(fig)
window["-INFO-"].update(value=f"mouse {values['-GRAPH-']}")
elif event.endswith('+UP'): # The drawing has ended because mouse up elif event.endswith('+UP'): # The drawing has ended because mouse up
info = window["info"] window["-INFO-"].update(value=f"grabbed rectangle from {start_point} to {end_point}")
info.update(value=f"grabbed rectangle from {start_point} to {end_point}")
start_point, end_point = None, None # enable grabbing a new rect start_point, end_point = None, None # enable grabbing a new rect
dragging = False dragging = False
prior_rect = None prior_rect = None
elif event.endswith('+RIGHT+'): # Righ click
window["-INFO-"].update(value=f"Right clicked location {values['-GRAPH-']}")
elif event == '-SAVE-': elif event == '-SAVE-':
# filename = sg.popup_get_file('Choose file (PNG, JPG, GIF) to save to', save_as=True) # filename = sg.popup_get_file('Choose file (PNG, JPG, GIF) to save to', save_as=True)
filename=r'test.jpg' filename=r'test.jpg'