Я новичок в Python и столкнулся с этой проблемой.
Я хочу удалить линию с помощью события щелчка мыши, например, если я рисую линию, когда мышь приближается к линии, щелкнувЛиния будет удалена.
Я пытался нарисовать линию, взять координаты и сохранить их в массиве, и когда я нажму кнопку стирания, он нарисует другую линию с тем же цветом фона.
Но со мной это не сработало.
# Graphics Program
from tkinter import * # We read the graphic library
import tkinter as tk
import math
# Initilize the TKInter
root = Tk()
root.title('Mouse based graphics program') # This is the title of the window
# **********************************************************************************************
# Define the Window sizes and draw 2 frames (plot and control windows) inside a large window
# **********************************************************************************************
# Window size
window_width = int(800)
window_height = int(600)
# Window - setup and draw
canvas = Canvas(root, width=window_width, height=window_height)
line_array_x=[]
line_array_y=[]
# ***************************************************************************
# Window 1 - Large external frame
x_and_y_offsets = float(0.05) # Space between window and external frame
x_left_top_large_frame = window_width * x_and_y_offsets
y_left_top_large_frame = window_height * x_and_y_offsets
x_right_bottom_large_frame = window_width - (window_width * x_and_y_offsets)
y_right_bottom_large_frame = window_height - (window_height * x_and_y_offsets)
# Draw the frame with a rectangle
canvas.create_rectangle(x_left_top_large_frame, y_left_top_large_frame,
x_right_bottom_large_frame, y_right_bottom_large_frame,
outline="blue", fill="grey", width='2')
# ***************************************************************************
# Window 2 - Plot frame in the large window - A function is used to redraw this window to clean the area
# Plot the "PLOT WINDOW" - Use this function to clean the picture in it - We will erase the curve and both Axes with labels
def draw_plot_window(x_left_offset, x_right_offset, y_top_bottom_offsets):
global x_left_top_plot_frame, x_right_bottom_plot_frame, y_left_top_plot_frame, y_right_bottom_plot_frame
x_left_top_plot_frame = window_width * x_left_offset
y_left_top_plot_frame = window_height * y_top_bottom_offsets
x_right_bottom_plot_frame = window_width - (window_width * x_right_offset)
y_right_bottom_plot_frame = window_height - (window_height * y_top_bottom_offsets)
# Draw the frame with a rectangle
canvas.create_rectangle(x_left_top_plot_frame, y_left_top_plot_frame,
x_right_bottom_plot_frame, y_right_bottom_plot_frame,
outline="red", fill="white", width='2')
# Define boundaries and call the plot window drawing function
x_left_plot_frame_offset = float(0.1) # Space between window and plot frame
x_right_plot_frame_offset = float(0.3) # Space between window and plot frame
y_top_bottom_plot_frame_offsets = float(0.1) # Space between window and plot frame
# Draw the window with above boundaries by using the above function
draw_plot_window(x_left_plot_frame_offset, x_right_plot_frame_offset, y_top_bottom_plot_frame_offsets)
# ***************************************************************************
# Window 3 - Control button frame in the window
x_left_control_offset = float(0.72) # Space between window and plot frame
x_right_control_offset = float(0.08) # Space between window and plot frame
y_top_bottom_control_offsets = float(0.1) # Space between window and plot frame
x_left_top_control_frame = window_width * x_left_control_offset
y_left_top_control_frame = window_height * y_top_bottom_control_offsets
x_right_bottom_control_frame = window_width - (window_width * x_right_control_offset)
y_right_bottom_control_frame = window_height - (window_height * y_top_bottom_control_offsets)
# Draw the frame with a rectangle
canvas.create_rectangle(x_left_top_control_frame, y_left_top_control_frame,
x_right_bottom_control_frame, y_right_bottom_control_frame,
outline="red", fill="white", width='2')
# ***************************************************************************
# Set the default values to draw the geometric functions
# Put the range of the x axis in radian
\
x_button_offset = x_left_control_offset + 0.02 # Percentage of the width of the window
y_button_offset = y_top_bottom_control_offsets + 0.02 # Percentage of the height of the window
y_button_space = 30 # Exact distance between the buttons and input boxes in the control frame
x_button_location = window_width * x_button_offset
y_button_location = window_height * (y_button_offset + 0.02)
canvas.create_text(x_button_location, y_button_location,
anchor=W, font=('verdana', 12), fill='blue',
text="Select")
canvas.create_text(x_button_location, y_button_location + y_button_space,
anchor=W, font=('verdana', 12), fill='blue',
text="option")
# No text box will be used in this program - Following lines are availible if needed with any purpose
# textBox1=Text(root, height=1, width=8)
# textBox1.place(x = x_button_location, y = y_button_location+y_button_space)
##########textBox1.pack()
# textBox1.insert(tk.END, x_start)
# textBox2=Text(root, height=1, width=8)
# textBox2.place(x = x_button_location, y = y_button_location+y_button_space*2)
#########textBox1.pack()
# textBox2.insert(tk.END, x_end)
# Function to draw a line on the canvas, Step 1 & 4
# Function to draw a line on the canvas, Step 1 & 4
def draw_line():
canvas.bind('<Button-1>', draw_line_1)
# Function to draw a line on the canvas, Step 2
def draw_line_1(event):
global control
control = 1
global x, y
x = event.x
y = event.y
# Evaluate if the point is at the plot window
global x_left_top_plot_frame, x_right_bottom_plot_frame, y_left_top_plot_frame, y_right_bottom_plot_frame
if x > x_left_top_plot_frame and x < x_right_bottom_plot_frame and y > y_left_top_plot_frame and y < y_right_bottom_plot_frame:
global cursor1, cursor2
cursor1 = canvas.create_line(x - 5, y, x + 5, y, fill='red', width=1)
cursor2 = canvas.create_line(x, y - 5, x, y + 5, fill='red', width=1)
line_array_x.append(x)
line_array_y.append(y)
canvas.bind('<Button-1>', draw_line_2)
# Function to draw a line on the canvas, Step 3
def draw_line_2(event):
global control
global cursor1, cursor2
global x, y, x1, y1
x1 = event.x
y1 = event.y
# Evaluate if the point is at the plot window
global x_left_top_plot_frame, x_right_bottom_plot_frame, y_left_top_plot_frame, y_right_bottom_plot_frame
if x1 > x_left_top_plot_frame and x1 < x_right_bottom_plot_frame and y1 > y_left_top_plot_frame and y1 < y_right_bottom_plot_frame:
# Erase the cursor which marks the location of first click
canvas.delete(cursor1, cursor2)
if control == 1:
canvas.create_line(x, y, x1, y1, fill="black", width=3)
control = 0
# Function to clear the plotting area - Redraw that frame
def clear():
global x_left_top_plot_frame, x_right_bottom_plot_frame, y_left_top_plot_frame, y_right_bottom_plot_frame
# Draw the window with above boundaries by using the above function
draw_plot_window(x_left_plot_frame_offset, x_right_plot_frame_offset, y_top_bottom_plot_frame_offsets)
# Function to draw a line on the canvas, Step 3
def click(event):
global control
global x, y
x = event.x
y = event.y
if control == 1:
canvas.create_line(line_array_x[1], line_array_y[1], line_array_x[0], line_array_y[0], fill="blue", width="10")
control = 0
# *******************************************************************************************************
# *******************************************************************************************************
# ******************************** MAIN ACTION *********************************************************
# *******************************************************************************************************
# *******************************************************************************************************
# To avoid problems at the first run - It will be 1 after the first point in the functions
global cursor_control
global control_live_line
global control_cursor_live_line
global control_live_line_option
control_cursor = 0
control_live_line = 0
control_cursor_live_line = 0
control_live_line_option = 0
# Draw the sine wave with defined boundaries first time
line_button = Button(canvas, text="ERASE", command=click, height=1, width=8, bg="yellow", compound=LEFT)
line_button.place(x=x_button_location + 70, y=y_button_location + y_button_space * 3)
# ***************************************************************************
# Button to clear the drawing area
line_button = Button(canvas, text="Clear", command=clear, height=1, width=8, bg="yellow", compound=LEFT)
line_button.place(x=x_button_location, y=y_button_location + y_button_space * 3)
# ***************************************************************************
# Button to draw line
line_button = Button(canvas, text="Line", command=draw_line, height=1, width=8, bg="yellow", compound=LEFT)
line_button.place(x=x_button_location, y=y_button_location + y_button_space * 4)
# ***************************************************************************
canvas.pack()
root.mainloop()