MCPcopy Index your code
hub / github.com/PySimpleGUI/PySimpleGUI / main

Function main

DemoPrograms/Demo_Graph_Drawing_And_Dragging_Figures.py:28–139  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

26
27
28def main():
29
30 sg.theme('Dark Blue 3')
31
32 col = [[sg.T('Choose what clicking a figure does', enable_events=True)],
33 [sg.R('Draw Rectangles', 1, key='-RECT-', enable_events=True)],
34 [sg.R('Draw Circle', 1, key='-CIRCLE-', enable_events=True)],
35 [sg.R('Draw Line', 1, key='-LINE-', enable_events=True)],
36 [sg.R('Draw points', 1, key='-POINT-', enable_events=True)],
37 [sg.R('Erase item', 1, key='-ERASE-', enable_events=True)],
38 [sg.R('Erase all', 1, key='-CLEAR-', enable_events=True)],
39 [sg.R('Send to back', 1, key='-BACK-', enable_events=True)],
40 [sg.R('Bring to front', 1, key='-FRONT-', enable_events=True)],
41 [sg.R('Move Everything', 1, key='-MOVEALL-', enable_events=True)],
42 [sg.R('Move Stuff', 1, key='-MOVE-', enable_events=True)],
43 [sg.B('Save Image', key='-SAVE-')],
44 ]
45
46 layout = [[sg.Graph(
47 canvas_size=(400, 400),
48 graph_bottom_left=(0, 0),
49 graph_top_right=(800, 800),
50 key="-GRAPH-",
51 enable_events=True,
52 background_color='lightblue',
53 drag_submits=True,
54 right_click_menu=[[],['Erase item',]]
55 ), sg.Col(col, key='-COL-') ],
56 [sg.Text(key='-INFO-', size=(60, 1))]]
57
58 window = sg.Window("Drawing and Moving Stuff Around", layout, finalize=True)
59
60 # get the graph element for ease of use later
61 graph = window["-GRAPH-"] # type: sg.Graph
62 graph.draw_image(data=logo200, location=(0,400))
63
64 dragging = False
65 start_point = end_point = prior_rect = None
66 # graph.bind('<Button-3>', '+RIGHT+')
67
68 while True:
69 event, values = window.read()
70 print(event, values)
71 if event == sg.WIN_CLOSED:
72 break # exit
73
74 if event in ('-MOVE-', '-MOVEALL-'):
75 graph.set_cursor(cursor='fleur') # not yet released method... coming soon!
76 elif not event.startswith('-GRAPH-'):
77 graph.set_cursor(cursor='left_ptr') # not yet released method... coming soon!
78
79 if event == "-GRAPH-": # if there's a "Graph" event, then it's a mouse
80 x, y = values["-GRAPH-"]
81 if not dragging:
82 start_point = (x, y)
83 dragging = True
84 drag_figures = graph.get_figures_at_location((x,y))
85 lastxy = x, y

Calls 15

readMethod · 0.95
closeMethod · 0.95
draw_imageMethod · 0.80
delete_figureMethod · 0.80
move_figureMethod · 0.80
draw_rectangleMethod · 0.80
draw_circleMethod · 0.80
draw_lineMethod · 0.80
draw_pointMethod · 0.80
eraseMethod · 0.80
bring_figure_to_frontMethod · 0.80

Tested by

no test coverage detected