0% found this document useful (0 votes)
16 views4 pages

Holy Message

Uploaded by

ahmadsalih889
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

Holy Message

Uploaded by

ahmadsalih889
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

from tkinter import *

from tkinter.colorchooser import askcolor

class DrawingApp:
def __init__(self, root):
self.root = root
self.canvas = Canvas(self.root, width=800, height=600, bg="white")
self.canvas.pack(side=BOTTOM, padx=10, pady=10)

# Create buttons frame


button_frame = Frame(self.root)
button_frame.pack(side=TOP)

# Create buttons for shape selection


self.create_shape_button("Line", "line", button_frame)
self.create_shape_button("Rectangle", "rectangle", button_frame)
self.create_shape_button("Circle", "circle", button_frame)
# Create button for tool selection
self.create_tool_button("Select", button_frame)

# Create button frame for color selection, fill, and thickness buttons
button_frame2 = Frame(self.root)
button_frame2.pack(side=TOP)

# Create button for color selection


self.color_button = Button(button_frame2, text="Select Color",
command=self.select_color)
self.color_button.pack(side=LEFT, pady=5)

# Create button for color filling


self.fill_button = Button(button_frame2, text="Fill",
command=self.set_fill_mode)
self.fill_button.pack(side=LEFT, pady=5, padx=5)

# Create button and slider for thickness selection


self.thickness_button = Button(button_frame2, text="Thickness",
command=self.create_thickness_slider)
self.thickness_button.pack(side=LEFT, pady=5)

self.tool = "Draw" # Current tool (Draw or Select)


self.selected_shape = None
self.selected_color = "black" # Default color
self.shapes = []
self.thickness = 1 # Default thickness

# Binding mouse events


self.canvas.bind("<Button-1>", self.handle_mouse_click)
self.canvas.bind("<B1-Motion>", self.handle_mouse_motion)
self.canvas.bind("<ButtonRelease-1>", self.handle_mouse_release)
self.canvas.bind("<Button-3>", self.select_shape)
self.canvas.bind("<B3-Motion>", self.move_shape)
self.root.bind("<Delete>", self.delete_shape)
self.root.bind("<BackSpace>", self.delete_shape)

def create_shape_button(self, text, shape_type, parent_frame):


button = Button(parent_frame, text=text, command=lambda:
self.set_selected_shape(shape_type))
button.pack(side=LEFT, padx=5)
def create_tool_button(self, tool_type, parent_frame):
button = Button(parent_frame, text=tool_type, command=lambda:
self.set_tool(tool_type))
button.pack(side=LEFT, padx=5)

def create_thickness_slider(self):
self.thickness_slider = Scale(self.root, from_=1, to=10, orient=HORIZONTAL,
command=self.update_thickness)
self.thickness_slider.pack(pady=5)

def update_thickness(self, value):


self.thickness = int(value)

def set_selected_shape(self, shape_type):


self.selected_shape = shape_type
self.set_tool("Draw") # Switch to Draw mode when a shape is selected

def set_tool(self, tool_type):


self.tool = tool_type

def set_fill_mode(self):
self.tool = "Fill"

def select_color(self):
color = askcolor(color=self.selected_color)[1]
if color:
self.selected_color = color

def handle_mouse_click(self, event):


if self.tool == "Draw":
self.start_drawing(event)
elif self.tool == "Select":
self.select_shape(event)
elif self.tool == "Fill":
self.fill_shape(event)

def handle_mouse_motion(self, event):


if self.tool == "Draw":
self.draw_shape(event)
elif self.tool == "Select":
self.move_shape(event)

def handle_mouse_release(self, event):


if self.tool == "Draw":
self.end_drawing(event)
elif self.tool == "Select":
pass

def start_drawing(self, event):


self.start_x = event.x
self.start_y = event.y

def draw_shape(self, event):


if self.selected_shape == "line":
self.canvas.delete("temp_shape")
self.canvas.create_line(self.start_x, self.start_y, event.x, event.y,
tags="temp_shape",
fill=self.selected_color, width=self.thickness)
elif self.selected_shape == "rectangle":
self.canvas.delete("temp_shape")
self.canvas.create_rectangle(self.start_x, self.start_y, event.x,
event.y, tags="temp_shape",
fill=self.selected_color,
outline=self.selected_color, width=self.thickness)
elif self.selected_shape == "circle":
self.canvas.delete("temp_shape")
self.canvas.create_oval(self.start_x, self.start_y, event.x, event.y,
tags="temp_shape",
fill=self.selected_color,
outline=self.selected_color, width=self.thickness)

def end_drawing(self, event):


self.canvas.delete("temp_shape")
if self.selected_shape == "line":
shape = self.canvas.create_line(self.start_x, self.start_y, event.x,
event.y, fill=self.selected_color,
width=self.thickness)
elif self.selected_shape == "rectangle":
shape = self.canvas.create_rectangle(self.start_x, self.start_y,
event.x, event.y, fill=self.selected_color,
outline=self.selected_color,
width=self.thickness)
elif self.selected_shape == "circle":
shape = self.canvas.create_oval(self.start_x, self.start_y, event.x,
event.y, fill=self.selected_color,
outline=self.selected_color,
width=self.thickness)
self.shapes.append(shape)

def fill_shape(self, event):


# Get the shape that is under the cursor
selected_shape = self.canvas.find_withtag(CURRENT)
if selected_shape:
if self.canvas.type(selected_shape) == "line":
# Change the color of the line
self.canvas.itemconfig(selected_shape, fill=self.selected_color)
else:
# Change the color of the shape
self.canvas.itemconfig(selected_shape, fill=self.selected_color,
outline=self.selected_color)

def select_shape(self, event):


# Get the shape that is under the cursor
selected_shape = self.canvas.find_withtag(CURRENT)
if selected_shape:
self.selected_shape = selected_shape[0]
self.set_tool("Select") # Switch to Select mode when a shape is
selected

# Save the coordinates of the cursor at the moment of selection


self.selection_point_x = event.x
self.selection_point_y = event.y
else:
self.selected_shape = None

def move_shape(self, event):


if self.selected_shape:
# Calculate the distance the cursor has moved from the point of
selection
delta_x = event.x - self.selection_point_x
delta_y = event.y - self.selection_point_y

# Move the shape by this distance


self.canvas.move(self.selected_shape, delta_x, delta_y)

# Update the selection point to the new cursor position


self.selection_point_x = event.x
self.selection_point_y = event.y

def delete_shape(self, event):


if self.selected_shape:
self.canvas.delete(self.selected_shape)
self.shapes.remove(self.selected_shape)
self.selected_shape = None

if __name__ == "__main__":
root = Tk()
root.title("Drawing App")
app = DrawingApp(root)
root.mainloop()

You might also like