- 1 year ago
Draw graphics on click and make them stay
Comment
1 / 1
To create a simple interactive graphics application in a graphical user interface (GUI) using Python, you can use a library like tkinter
. Here's an example of how you can create a window where you can draw graphics on click, and the graphics stay on the canvas:
pythonimport tkinter as tk
class GraphicsApp:
def __init__(self, root):
self.root = root
self.root.title("Interactive Graphics")
self.canvas = tk.Canvas(root, width=400, height=400, bg="white")
self.canvas.pack()
self.canvas.bind("<Button-1>", self.draw_circle)
self.graphics = []
def draw_circle(self, event):
x, y = event.x, event.y
radius = 20
circle = self.canvas.create_oval(
x - radius, y - radius, x + radius, y + radius, fill="blue"
)
self.graphics.append(circle)
if __name__ == "__main__":
root = tk.Tk()
app = GraphicsApp(root)
root.mainloop()
In this example, a simple GUI application is created using tkinter
. When you click on the canvas, a blue circle is drawn at the clicked position. The coordinates and size of the circle are determined by the click event. The circles you draw stay on the canvas.
This is just a basic example to get you started. You can extend this concept to draw more complex graphics, add features like colors or shapes selection, and even implement interactions like moving or resizing the drawn objects.