From 9c59d86d1c9ad54add0d20ff430a17e8ce84e3a9 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Wed, 26 Sep 2018 22:41:49 -0400 Subject: [PATCH 1/2] Added making Axis --- Demo_Graph_Element_Sine_Wave.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Demo_Graph_Element_Sine_Wave.py b/Demo_Graph_Element_Sine_Wave.py index 2609f6a8..bd69551e 100644 --- a/Demo_Graph_Element_Sine_Wave.py +++ b/Demo_Graph_Element_Sine_Wave.py @@ -1,16 +1,28 @@ import math import PySimpleGUI as sg -layout = [[sg.Graph(canvas_size=(400, 400), graph_bottom_left=(-100,-100), graph_top_right=(100,100), background_color='white', key='graph', tooltip='This is a cool graph!')],] +layout = [[sg.Graph(canvas_size=(400, 400), graph_bottom_left=(-105,-105), graph_top_right=(105,105), background_color='white', key='graph', tooltip='This is a cool graph!')],] window = sg.Window('Graph of Sine Function', grab_anywhere=True).Layout(layout).Finalize() graph = window.FindElement('graph') +# Draw axis graph.DrawLine((-100,0), (100,0)) graph.DrawLine((0,-100), (0,100)) +for x in range(-100, 101, 20): + graph.DrawLine((x,-3), (x,3)) + if x != 0: + graph.DrawText( x, (x,-10), color='green') + +for y in range(-100, 101, 20): + graph.DrawLine((-3,y), (3,y)) + if y != 0: + graph.DrawText( y, (-10,y), color='blue') + +# Draw Graph for x in range(-100,100): y = math.sin(x/20)*50 - graph.DrawPoint((x,y), color='red') + graph.DrawCircle((x,y), 1, line_color='red', fill_color='red') button, values = window.Read() From 4208d9d569ab8150a8d8a4d4b1f22dddbcf14679 Mon Sep 17 00:00:00 2001 From: MikeTheWatchGuy Date: Wed, 26 Sep 2018 23:06:06 -0400 Subject: [PATCH 2/2] Warnings added to help people when they missed calling Finalize --- Demo_Graph_Drawing.py | 2 +- PySimpleGUI.py | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/Demo_Graph_Drawing.py b/Demo_Graph_Drawing.py index ee812c52..886d4fa7 100644 --- a/Demo_Graph_Drawing.py +++ b/Demo_Graph_Drawing.py @@ -5,7 +5,7 @@ layout = [ [sg.T('Change circle color to:'), sg.ReadButton('Red'), sg.ReadButton('Blue'), sg.ReadButton('Move')] ] -window = sg.Window('Graph test').Layout(layout) +window = sg.Window('Graph test').Layout(layout).Finalize() graph = window.FindElement('graph') circle = graph.DrawCircle((75,75), 25, fill_color='black',line_color='white') diff --git a/PySimpleGUI.py b/PySimpleGUI.py index fe63bccb..657799f6 100644 --- a/PySimpleGUI.py +++ b/PySimpleGUI.py @@ -1288,48 +1288,88 @@ 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[0], point_from[1]) converted_point_to = self._convert_xy_to_canvas_xy(point_to[0], point_to[1]) + if self._TKCanvas2 is None: + print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***') + print('Call Window.Finalize() prior to this operation') + return None 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[0], point[1]) + if self._TKCanvas2 is None: + print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***') + print('Call Window.Finalize() prior to this operation') + return None 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[0], center_location[1]) + if self._TKCanvas2 is None: + print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***') + print('Call Window.Finalize() prior to this operation') + return None 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[0], top_left[1]) converted_bottom_right = self._convert_xy_to_canvas_xy(bottom_right[0],bottom_right[1]) + if self._TKCanvas2 is None: + print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***') + print('Call Window.Finalize() prior to this operation') + return None return self._TKCanvas2.create_oval(converted_top_left[0], converted_top_left[1], converted_bottom_right[0], converted_bottom_right[1], 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[0], top_left[1] ) converted_bottom_right = self._convert_xy_to_canvas_xy(bottom_right[0], bottom_right[1]) + if self._TKCanvas2 is None: + print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***') + print('Call Window.Finalize() prior to this operation') + return None return self._TKCanvas2.create_rectangle(converted_top_left[0], converted_top_left[1], converted_bottom_right[0], converted_bottom_right[1], fill=fill_color, outline=line_color) def DrawText(self, text, location, color='black', font=None): converted_point = self._convert_xy_to_canvas_xy(location[0], location[1]) + if self._TKCanvas2 is None: + print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***') + print('Call Window.Finalize() prior to this operation') + return None return self._TKCanvas2.create_text(converted_point[0], converted_point[1], text=text, font=font, fill=color) def Erase(self): + if self._TKCanvas2 is None: + print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***') + print('Call Window.Finalize() prior to this operation') + return None self._TKCanvas2.delete('all') def Update(self, background_color): + if self._TKCanvas2 is None: + print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***') + print('Call Window.Finalize() prior to this operation') + return None self._TKCanvas2.configure(background=background_color) def Move(self, 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]) + if self._TKCanvas2 is None: + print('*** WARNING - The Graph element has not been finalized and cannot be drawn upon ***') + print('Call Window.Finalize() prior to this operation') + return None self._TKCanvas2.move('all', shift_amount[0], shift_amount[1]) 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]) + if figure is None: + print('*** WARNING - Your figure is None. It most likely means your did not Finalize your Window ***') + print('Call Window.Finalize() prior to all graph operations') + return None self._TKCanvas2.move(figure, shift_amount[0], shift_amount[1]) @property