Creating Shapes with PySimpleGUI

Posted by

PySimpleGUI – Drawing Shapes

PySimpleGUI – Drawing Shapes

PySimpleGUI is a Python library that provides a simple way to create GUI applications. With the PySimpleGUI-Drawing module, you can easily draw shapes and lines on the screen.

Installation

To use PySimpleGUI-Drawing, you’ll need to install the PySimpleGUI library. You can do this using pip:


pip install PySimpleGUI

Drawing Shapes

To draw shapes using PySimpleGUI, you’ll first need to create a window using the sg.Window() function. Then, you can use the draw_line(), draw_rectangle(), and draw_circle() functions to draw lines, rectangles, and circles, respectively.


import PySimpleGUI as sg

layout = [[sg.Graph(canvas_size=(400, 400), graph_bottom_left=(0, 0), graph_top_right=(400, 400), background_color='white', key='graph')]]

window = sg.Window('Drawing Shapes', layout)

graph = window['graph']

graph.draw_line((0, 0), (200, 200))
graph.draw_rectangle((100, 100), (300, 300))
graph.draw_circle((200, 200), 50)

event, values = window.read()

window.close()

In the example above, we create a window with a graph element that we can use to draw shapes. We then use the draw_line(), draw_rectangle(), and draw_circle() functions to draw a line, a rectangle, and a circle on the screen. Finally, we call the window.read() function to display the window and wait for the user to interact with it.

Conclusion

PySimpleGUI makes it easy to draw shapes and lines on the screen using the PySimpleGUI-Drawing module. By following the simple installation and usage instructions, you can quickly create interactive GUI applications with the ability to draw various shapes and lines.

0 0 votes
Article Rating
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@keithlohmeyer
11 months ago

Nice. You aliased window['-GRAPH-'] as graph. Is there a reason you did not use the alias when referencing it in the while True?
Thanks for the video.

@latavish
11 months ago

I have learned so much from your py simple GUI tutorial series! Thank you!! Please keep this gold coming 😊