From 9db96fd10f3c6a5bb441cc355941d1fd7c080f4b Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Tue, 18 Sep 2018 11:10:17 -0400 Subject: [PATCH] New Drawing methods for Graph Element. Demo program on drawing on Graph Elements --- Demo_Graph_Drawing.py | 30 ++++++++++++++++++++++++++++++ PySimpleGUI.py | 26 +++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 3 deletions(-) create mode 100644 Demo_Graph_Drawing.py diff --git a/Demo_Graph_Drawing.py b/Demo_Graph_Drawing.py new file mode 100644 index 00000000..fc4ca3c8 --- /dev/null +++ b/Demo_Graph_Drawing.py @@ -0,0 +1,30 @@ +import PySimpleGUI as sg + +layout = [ + [sg.Graph(canvas_size=(400, 400), graph_bottom_left=(0,0), graph_top_right=(400, 400), background_color='red', key='graph')], + [sg.T('Change circle color to:'), sg.ReadFormButton('Red'), sg.ReadFormButton('Blue'), sg.ReadFormButton('Move')] + ] + +form = sg.FlexForm('Canvas test') +form.Layout(layout) +form.Finalize() + +graph = form.FindElement('graph') +circle = graph.DrawCircle((75,75), 25, fill_color='black',line_color='white') +point = graph.DrawPoint((75,75), 10, color='green') +oval = graph.DrawOval((25,300), (100,280), fill_color='purple', line_color='purple' ) +rectangle = graph.DrawRectangle((25,300), (100,280), line_color='purple' ) + +while True: + button, values = form.Read() + if button is None: + break + if button is 'Blue': + graph.TKCanvas.itemconfig(circle, fill = "Blue") + elif button is 'Red': + graph.TKCanvas.itemconfig(circle, fill = "Red") + elif button is 'Move': + graph.MoveFigure(point, 10,10) + graph.MoveFigure(circle, 10,10) + graph.MoveFigure(oval, 10,10) + graph.MoveFigure(rectangle, 10,10) diff --git a/PySimpleGUI.py b/PySimpleGUI.py index 219601e8..03852e30 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -1194,16 +1194,30 @@ class Graph(Element): def DrawLine(self, point_from, point_to, color='black', width=1): converted_point_from = self._convert_xy_to_canvas_xy(*point_from) converted_point_to = self._convert_xy_to_canvas_xy(*point_to) - self._TKCanvas2.create_line(converted_point_from, converted_point_to, width=width, fill=color) + return self._TKCanvas2.create_line(converted_point_from, converted_point_to, width=width, fill=color) def DrawPoint(self, point, size=2, color='black'): converted_point = self._convert_xy_to_canvas_xy(*point) - self._TKCanvas2.create_oval(converted_point[0]-size, converted_point[1]-size, converted_point[0]+size, converted_point[1]+size, fill=color, outline=color ) + return self._TKCanvas2.create_oval(converted_point[0]-size, converted_point[1]-size, converted_point[0]+size, converted_point[1]+size, fill=color, outline=color ) + + def DrawCircle(self, center_location, radius, fill_color=None, line_color='black'): + converted_point = self._convert_xy_to_canvas_xy(*center_location) + return self._TKCanvas2.create_oval(converted_point[0]-radius, converted_point[1]-radius, converted_point[0]+radius, converted_point[1]+radius, fill=fill_color, outline=line_color) + + def DrawOval(self, top_left, bottom_right, fill_color=None, line_color=None): + converted_top_left = self._convert_xy_to_canvas_xy(*top_left) + converted_bottom_right = self._convert_xy_to_canvas_xy(*bottom_right) + return self._TKCanvas2.create_oval(*converted_top_left, *converted_bottom_right, fill=fill_color, outline=line_color) + + + def DrawRectangle(self, top_left, bottom_right, fill_color=None, line_color=None): + converted_top_left = self._convert_xy_to_canvas_xy(*top_left) + converted_bottom_right = self._convert_xy_to_canvas_xy(*bottom_right) + return self._TKCanvas2.create_rectangle(*converted_top_left, *converted_bottom_right, fill=fill_color, outline=line_color) def Erase(self): self._TKCanvas2.delete('all') - def Update(self, background_color): self._TKCanvas2.configure(background=background_color) @@ -1213,6 +1227,12 @@ class Graph(Element): shift_amount = (shift_converted[0]-zero_converted[0], shift_converted[1]-zero_converted[1]) self._TKCanvas2.move('all', *shift_amount) + def MoveFigure(self, figure, x_direction, y_direction): + zero_converted = self._convert_xy_to_canvas_xy(0,0) + shift_converted = self._convert_xy_to_canvas_xy(x_direction, y_direction) + shift_amount = (shift_converted[0]-zero_converted[0], shift_converted[1]-zero_converted[1]) + self._TKCanvas2.move(figure, *shift_amount) + @property def TKCanvas(self): return self._TKCanvas2