Grandma Luda2021-03-26 10:22:15
Grandma Luda, 2021-03-26 10:22:15

When creating an oval on a canvas I can move the oval, but when I create a second oval I can't move the first oval only the second one?

When I create an oval, I can move it with the mouse cursor, but creating a second one, I cannot move the first oval! I need to be able to move all the created ovals.

here is the code that moves each created oval

import tkinter as tk
import sqlite3 as sql

root = tk.Tk()

canvas = tk.Canvas(root)
canvas.pack(fill="both", expand=True)

def oval(event):# рисует овал
    global x1
    global y1
    global x2
    global y2
    global shape
    x1 = event.x - 50
    y1 = event.y - 50
    x2 = event.x + 50
    y2 = event.y + 50
    #x1 = 0
    #y1 = 0
    #x2 = 100
    #y2 = 100
    shape = canvas.create_oval(x1, y1, x2, y2)

def move_oval(event):# перемещает овал
    global x1
    global y1
    global x2
    global y2
    if (x1 <= event.x ) and (x2 >= event.x) and (y1 <= event.y) and (y2 >=event.y):# проверка находится ли курсор мышки                                                                             
        x1 = event.x - 50                                                          #там где находится овал чтобы переместить его
        y1 = event.y - 50
        x2 = event.x + 50
        y2 = event.y + 50
        canvas.coords(shape, x1, y1, x2, y2)  

canvas.bind('<Button-3>', oval)
canvas.bind('<B1-Motion>', move_oval)


Answer the question

In order to leave comments, you need to log in

2 answer(s)
Anton fon Faust, 2018-03-21

For example, like this:

$('a[href="#contact_form_pop"]').on('click', function(e){
$('#contact_form_pop input').first().val(e.target.parent().parent().find('h4.modal-title').first().text())

o5a, 2021-03-26

Can it be like this

def move_oval(event):# перемещает овал
    canvas = event.widget
    # находим ближайший объект
    obj = canvas.find_closest(event.x, event.y)[0]
    # для оценки направления движения возьмем центр объекта
    x1,y1,x2,y2 = canvas.coords(obj)
    cx = (x2+x1)//2
    cy = (y2+y1)//2
    canvas.move(obj, event.x-cx, event.y-cy)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question