commit
						393d3fddb3
					
				
					 3 changed files with 108 additions and 57 deletions
				
			
		
							
								
								
									
										30
									
								
								Demo_Graph_Drawing.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										30
									
								
								Demo_Graph_Drawing.py
									
										
									
									
									
										Normal 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) | ||||||
|  | @ -1,56 +1,43 @@ | ||||||
| from tkinter import * | import PySimpleGUI as sg | ||||||
| from random import randint | 
 | ||||||
| import PySimpleGUI as g | # g.SetOptions(button_color=g.COLOR_SYSTEM_DEFAULT)   # because some people like gray buttons | ||||||
| from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, FigureCanvasAgg | 
 | ||||||
| from matplotlib.figure import Figure | # Demonstrates a number of PySimpleGUI features including: | ||||||
| import matplotlib.backends.tkagg as tkagg | #   Default element size | ||||||
| import tkinter as Tk | #   auto_size_buttons | ||||||
|  | #   ReadFormButton | ||||||
|  | #   Dictionary return values | ||||||
|  | #   Update of elements in form (Text, Input) | ||||||
|  | #   do_not_clear of Input elements | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| def main(): | # create the 2 Elements we want to control outside the form | ||||||
|     fig = Figure() |  | ||||||
| 
 | 
 | ||||||
|     ax = fig.add_subplot(111) | layout = [[sg.Text('Enter Your Passcode')], | ||||||
|     ax.set_xlabel("X axis") |           [sg.Input(size=(10, 1), do_not_clear=True, key='input')], | ||||||
|     ax.set_ylabel("Y axis") |           [sg.ReadFormButton('1'), sg.ReadFormButton('2'), sg.ReadFormButton('3')], | ||||||
|     ax.grid() |           [sg.ReadFormButton('4'), sg.ReadFormButton('5'), sg.ReadFormButton('6')], | ||||||
|  |           [sg.ReadFormButton('7'), sg.ReadFormButton('8'), sg.ReadFormButton('9')], | ||||||
|  |           [sg.ReadFormButton('Submit'), sg.ReadFormButton('0'), sg.ReadFormButton('Clear')], | ||||||
|  |           [sg.Text('', size=(15, 1), font=('Helvetica', 18), text_color='red', key='out')], | ||||||
|  |           ] | ||||||
| 
 | 
 | ||||||
|     layout = [[g.Text('Animated Matplotlib', size=(40, 1), justification='center', font='Helvetica 20')], | form = sg.FlexForm('Keypad', default_button_element_size=(5, 2), auto_size_buttons=False, grab_anywhere=False) | ||||||
|               [g.Canvas(size=(640, 480), key='canvas')], |  | ||||||
|               [g.ReadFormButton('Exit', size=(10, 2), pad=((280, 0), 3), font='Helvetica 14')]] |  | ||||||
| 
 |  | ||||||
|     # create the form and show it without the plot   |  | ||||||
|     form = g.FlexForm('Demo Application - Embedding Matplotlib In PySimpleGUI') |  | ||||||
| form.Layout(layout) | form.Layout(layout) | ||||||
|     form.ReadNonBlocking() |  | ||||||
| 
 | 
 | ||||||
|     canvas_elem = form.FindElement('canvas') | # Loop forever reading the form's values, updating the Input field | ||||||
|  | keys_entered = '' | ||||||
|  | while True: | ||||||
|  |     button, values = form.Read()  # read the form | ||||||
|  |     if button is None:  # if the X button clicked, just exit | ||||||
|  |         break | ||||||
|  |     if button is 'Clear':  # clear keys if clear button | ||||||
|  |         keys_entered = '' | ||||||
|  |     elif button in '1234567890': | ||||||
|  |         keys_entered = values['input']  # get what's been entered so far | ||||||
|  |         keys_entered += button  # add the new digit | ||||||
|  |     elif button is 'Submit': | ||||||
|  |         keys_entered = values['input'] | ||||||
|  |         form.FindElement('out').Update(keys_entered)  # output the final string | ||||||
| 
 | 
 | ||||||
|     graph = FigureCanvasTkAgg(fig, master=canvas_elem.TKCanvas) |     form.FindElement('input').Update(keys_entered)  # change the form to reflect current key string | ||||||
|     canvas = canvas_elem.TKCanvas |  | ||||||
| 
 |  | ||||||
|     dpts = [randint(0, 10) for x in range(10000)] |  | ||||||
|     for i in range(len(dpts)): |  | ||||||
|         button, values = form.ReadNonBlocking() |  | ||||||
|         if button is 'Exit' or values is None: |  | ||||||
|             exit(69) |  | ||||||
| 
 |  | ||||||
|         ax.cla() |  | ||||||
|         ax.grid() |  | ||||||
| 
 |  | ||||||
|         ax.plot(range(20), dpts[i:i + 20], color='purple') |  | ||||||
|         graph.draw() |  | ||||||
|         figure_x, figure_y, figure_w, figure_h = fig.bbox.bounds |  | ||||||
|         figure_w, figure_h = int(figure_w), int(figure_h) |  | ||||||
|         photo = Tk.PhotoImage(master=canvas, width=figure_w, height=figure_h) |  | ||||||
| 
 |  | ||||||
|         canvas.create_image(640 / 2, 480 / 2, image=photo) |  | ||||||
| 
 |  | ||||||
|         figure_canvas_agg = FigureCanvasAgg(fig) |  | ||||||
|         figure_canvas_agg.draw() |  | ||||||
| 
 |  | ||||||
|         tkagg.blit(photo, figure_canvas_agg.get_renderer()._renderer, colormode=2) |  | ||||||
|         # time.sleep(.1) |  | ||||||
| 
 |  | ||||||
| if __name__ == '__main__': |  | ||||||
|     main() |  | ||||||
|  | @ -118,6 +118,14 @@ TABLE_SELECT_MODE_BROWSE = tk.BROWSE | ||||||
| TABLE_SELECT_MODE_EXTENDED = tk.EXTENDED | TABLE_SELECT_MODE_EXTENDED = tk.EXTENDED | ||||||
| DEFAULT_TABLE_SECECT_MODE = TABLE_SELECT_MODE_EXTENDED | DEFAULT_TABLE_SECECT_MODE = TABLE_SELECT_MODE_EXTENDED | ||||||
| 
 | 
 | ||||||
|  | TITLE_LOCATION_TOP = tk.N | ||||||
|  | TITLE_LOCATION_BOTTOM = tk.S | ||||||
|  | TITLE_LOCATION_LEFT = tk.W | ||||||
|  | TITLE_LOCATION_RIGHT = tk.E | ||||||
|  | TITLE_LOCATION_TOP_LEFT = tk.NW | ||||||
|  | TITLE_LOCATION_TOP_RIGHT = tk.NE | ||||||
|  | TITLE_LOCATION_BOTTOM_LEFT = tk.SW | ||||||
|  | TITLE_LOCATION_BOTTOM_RIGHT = tk.SE | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| # DEFAULT_METER_ORIENTATION = 'Vertical' | # DEFAULT_METER_ORIENTATION = 'Vertical' | ||||||
|  | @ -1186,16 +1194,30 @@ class Graph(Element): | ||||||
|     def DrawLine(self, point_from, point_to, color='black', width=1): |     def DrawLine(self, point_from, point_to, color='black', width=1): | ||||||
|         converted_point_from = self._convert_xy_to_canvas_xy(*point_from) |         converted_point_from = self._convert_xy_to_canvas_xy(*point_from) | ||||||
|         converted_point_to = self._convert_xy_to_canvas_xy(*point_to) |         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'): |     def DrawPoint(self, point, size=2, color='black'): | ||||||
|         converted_point = self._convert_xy_to_canvas_xy(*point) |         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): |     def Erase(self): | ||||||
|         self._TKCanvas2.delete('all') |         self._TKCanvas2.delete('all') | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
|     def Update(self, background_color): |     def Update(self, background_color): | ||||||
|         self._TKCanvas2.configure(background=background_color) |         self._TKCanvas2.configure(background=background_color) | ||||||
| 
 | 
 | ||||||
|  | @ -1205,6 +1227,12 @@ class Graph(Element): | ||||||
|         shift_amount = (shift_converted[0]-zero_converted[0], shift_converted[1]-zero_converted[1]) |         shift_amount = (shift_converted[0]-zero_converted[0], shift_converted[1]-zero_converted[1]) | ||||||
|         self._TKCanvas2.move('all', *shift_amount) |         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 |     @property | ||||||
|     def TKCanvas(self): |     def TKCanvas(self): | ||||||
|         return self._TKCanvas2 |         return self._TKCanvas2 | ||||||
|  | @ -1217,7 +1245,7 @@ class Graph(Element): | ||||||
| #                           Frame                                        # | #                           Frame                                        # | ||||||
| # ---------------------------------------------------------------------- # | # ---------------------------------------------------------------------- # | ||||||
| class Frame(Element): | class Frame(Element): | ||||||
|     def __init__(self, title, layout, text_color=None, background_color=None, relief=DEFAULT_FRAME_RELIEF, size=(None, None), font=None, pad=None, key=None): |     def __init__(self, title, layout, title_color=None, background_color=None, title_location=None , relief=DEFAULT_FRAME_RELIEF, size=(None, None), font=None, pad=None, border_width=None, key=None): | ||||||
| 
 | 
 | ||||||
|         self.UseDictionary = False |         self.UseDictionary = False | ||||||
|         self.ReturnValues = None |         self.ReturnValues = None | ||||||
|  | @ -1230,11 +1258,13 @@ class Frame(Element): | ||||||
|         self.TKFrame = None |         self.TKFrame = None | ||||||
|         self.Title = title |         self.Title = title | ||||||
|         self.Relief = relief |         self.Relief = relief | ||||||
|  |         self.TitleLocation = title_location | ||||||
|  |         self.BorderWidth = border_width | ||||||
|         self.BackgroundColor = background_color if background_color is not None else DEFAULT_BACKGROUND_COLOR |         self.BackgroundColor = background_color if background_color is not None else DEFAULT_BACKGROUND_COLOR | ||||||
| 
 | 
 | ||||||
|         self.Layout(layout) |         self.Layout(layout) | ||||||
| 
 | 
 | ||||||
|         super().__init__(ELEM_TYPE_FRAME, background_color=background_color, text_color=text_color,  size=size, font=font, pad=pad, key=key) |         super().__init__(ELEM_TYPE_FRAME, background_color=background_color, text_color=title_color,  size=size, font=font, pad=pad, key=key) | ||||||
|         return |         return | ||||||
| 
 | 
 | ||||||
|     def AddRow(self, *args): |     def AddRow(self, *args): | ||||||
|  | @ -2815,8 +2845,12 @@ def PackFormIntoFrame(form, containing_frame, toplevel_form): | ||||||
|                     labeled_frame.configure(background=element.BackgroundColor, highlightbackground=element.BackgroundColor, highlightcolor=element.BackgroundColor) |                     labeled_frame.configure(background=element.BackgroundColor, highlightbackground=element.BackgroundColor, highlightcolor=element.BackgroundColor) | ||||||
|                 if element.TextColor != COLOR_SYSTEM_DEFAULT and element.TextColor is not None: |                 if element.TextColor != COLOR_SYSTEM_DEFAULT and element.TextColor is not None: | ||||||
|                     labeled_frame.configure(foreground=element.TextColor) |                     labeled_frame.configure(foreground=element.TextColor) | ||||||
|                 if element.Font != None: |                 if font is not None: | ||||||
|                     labeled_frame.configure(font=element.Font) |                     labeled_frame.configure(font=font) | ||||||
|  |                 if element.TitleLocation is not None: | ||||||
|  |                     labeled_frame.configure(labelanchor=element.TitleLocation) | ||||||
|  |                 if element.BorderWidth is not None: | ||||||
|  |                     labeled_frame.configure(borderwidth=element.BorderWidth) | ||||||
| 
 | 
 | ||||||
|                 # -------------------------  SLIDER Box element  ------------------------- # |                 # -------------------------  SLIDER Box element  ------------------------- # | ||||||
|             elif element_type == ELEM_TYPE_INPUT_SLIDER: |             elif element_type == ELEM_TYPE_INPUT_SLIDER: | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue