New Drawing methods for Graph Element. Demo program on drawing on Graph Elements

This commit is contained in:
MikeTheWatchGuy 2018-09-18 11:10:17 -04:00
parent 54049efa7c
commit 9db96fd10f
2 changed files with 53 additions and 3 deletions

30
Demo_Graph_Drawing.py Normal file
View File

@ -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)

View File

@ -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