-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape.py
More file actions
52 lines (40 loc) · 1.71 KB
/
shape.py
File metadata and controls
52 lines (40 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import tkinter as tk
class PaintApp:
def __init__(self, root):
self.root = root
self.root.title("Paint Application")
# Create a canvas
self.canvas = tk.Canvas(root, bg="white", width=800, height=600)
self.canvas.pack()
# Shape selection
self.shape = "rectangle" # Default shape
# Create buttons for shape selection
rectangle_button = tk.Button(root, text="Rectangle", command=lambda: self.set_shape("rectangle"))
rectangle_button.pack(side=tk.LEFT)
oval_button = tk.Button(root, text="Oval", command=lambda: self.set_shape("oval"))
oval_button.pack(side=tk.LEFT)
# Bind mouse events
self.canvas.bind("<Button-1>", self.on_left_button_click)
self.canvas.bind("<ButtonRelease-1>", self.on_left_button_release)
# Initialize shape drawing variables
self.start_x = None
self.start_y = None
def set_shape(self, shape):
self.shape = shape
def on_left_button_click(self, event):
# Store the starting coordinates
self.start_x = event.x
self.start_y = event.y
def on_left_button_release(self, event):
# Draw the selected shape on mouse release
if self.start_x is not None and self.start_y is not None:
if self.shape == "rectangle":
self.canvas.create_rectangle(self.start_x, self.start_y, event.x, event.y, outline="black", fill="")
elif self.shape == "oval":
self.canvas.create_oval(self.start_x, self.start_y, event.x, event.y, outline="black", fill="")
def run(self):
self.root.mainloop()
if __name__ == "__main__":
root = tk.Tk()
app = PaintApp(root)
app.run