diff --git a/DemoPrograms/Demo_Graph_Drawing_And_Dragging_Figures.py b/DemoPrograms/Demo_Graph_Drawing_And_Dragging_Figures.py index 81c9a755..7e594a2f 100644 --- a/DemoPrograms/Demo_Graph_Drawing_And_Dragging_Figures.py +++ b/DemoPrograms/Demo_Graph_Drawing_And_Dragging_Figures.py @@ -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 mouse to "drag" and draw rectangles and circles. + + Copyright 2020 PySimpleGUI.org """ 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('Bring to front', 1, key='-FRONT-', 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-')], ] @@ -46,8 +48,8 @@ def main(): key="-GRAPH-", enable_events=True, background_color='lightblue', - drag_submits=True), sg.Col(col) ], - [sg.Text(key='info', size=(60, 1))]] + drag_submits=True), sg.Col(col, key='-COL-') ], + [sg.Text(key='-INFO-', size=(60, 1))]] window = sg.Window("Drawing and Moving Stuff Around", layout, finalize=True) @@ -58,17 +60,17 @@ def main(): dragging = False start_point = end_point = prior_rect = None graph.bind('', '+RIGHT+') + while True: event, values = window.read() print(event, values) if event == sg.WIN_CLOSED: break # exit + 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-'): - # graph.set_cursor(cursor='left_ptr') # not yet released method... coming soon! - graph.Widget.config(cursor='left_ptr') + graph.set_cursor(cursor='left_ptr') # not yet released method... coming soon! if event == "-GRAPH-": # if there's a "Graph" event, then it's a mouse x, y = values["-GRAPH-"] @@ -109,12 +111,15 @@ def main(): elif values['-BACK-']: for fig in drag_figures: 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 - info = window["info"] - info.update(value=f"grabbed rectangle from {start_point} to {end_point}") + window["-INFO-"].update(value=f"grabbed rectangle from {start_point} to {end_point}") start_point, end_point = None, None # enable grabbing a new rect dragging = False prior_rect = None + elif event.endswith('+RIGHT+'): # Righ click + window["-INFO-"].update(value=f"Right clicked location {values['-GRAPH-']}") elif event == '-SAVE-': # filename = sg.popup_get_file('Choose file (PNG, JPG, GIF) to save to', save_as=True) filename=r'test.jpg' diff --git a/PySimpleGUI.py b/PySimpleGUI.py index ff43fe03..1c33784a 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -1,5 +1,5 @@ #!/usr/bin/python3 -version = __version__ = "4.34.0.14 Unreleased\nSDK Help Expanded to init & update parms, SDK Help function search, files_delimiter added to FilesBrowse & popup_get_file, SDK help sort by case, popup_get_file fixed default_extension not being passed to button correctly, changed themes so that spaces can be used in defined name, addition of subprocess non-blocking launcher, fix for Debug button color, set_option for default user_settings path to override normal default, define a truly global PySimpleGUI settings path, theme_global() gets the theme for all progams, execute_subprocess_nonblocking bug fix, mark when strout/stderr is restored in multiline elem, Listbox element convert values to list when updated, Column will expand row if y expand set to True, Added color/c parm to debug print, update graph coordinates if a user bound event happens" +version = __version__ = "4.34.0.15 Unreleased\nSDK Help Expanded to init & update parms, SDK Help function search, files_delimiter added to FilesBrowse & popup_get_file, SDK help sort by case, popup_get_file fixed default_extension not being passed to button correctly, changed themes so that spaces can be used in defined name, addition of subprocess non-blocking launcher, fix for Debug button color, set_option for default user_settings path to override normal default, define a truly global PySimpleGUI settings path, theme_global() gets the theme for all progams, execute_subprocess_nonblocking bug fix, mark when strout/stderr is restored in multiline elem, Listbox element convert values to list when updated, Column will expand row if y expand set to True, Added color/c parm to debug print, update graph coordinates if a user bound event happens, another attempt at graphs with user events" __version__ = version.split()[0] # For PEP 396 and PEP 345 @@ -1067,7 +1067,7 @@ class Element(): key_suffix = self.user_bind_dict.get(bind_string, '') self.user_bind_event = event if self.Type == ELEM_TYPE_GRAPH: - self.button_press_call_back(event) + self._user_bound_event_callback(event) if self.Key is not None: if isinstance(self.Key, str): key = self.Key + str(key_suffix) @@ -5078,6 +5078,19 @@ class Graph(Element): _exit_mainloop(self.ParentForm) self.MouseButtonDown = True + + # user bound event callback + def _user_bound_event_callback(self, event): + """ + Not a user callable method. Indirectly called by tkinter when any user bound event happens + + :param event: (event) event info from tkinter. Contains the x and y coordinates of a click + """ + + self.ClickPosition = self._convert_canvas_xy_to_xy(event.x, event.y) + self.ParentForm.LastButtonClickedWasRealtime = self.DragSubmits + + # button callback def motion_call_back(self, event): """