MTS
MTS

Reputation: 33

Creating and Moving Shapes On Click with TKINTER and canvas

I hope this isn't too stupid for you guys

Am kinda stuck in this project first time working with canvas and tkinter all I know is the basic tutorial.

what I want is how to create an on click event after precision of the shape I want with buttons and move the form (updating the positing)

 from tkinter import *
 from tkinter import messagebox
 def Click(event) :
     can.coords(cercle, event.x, event.y, event.x+10, event.y+10)
 def Rectangle() :
     can.create_rectangle(10, 10, 70, 70, fill='white', outline='blue', width=3)
 def Delete() :
      msg = messagebox.askyesnocancel('Info','Delete canevas ?')
      if msg == None :
        return
      elif msg == True :
        can.delete(ALL)
 def Line():
      can.create_line(200,200,100,100,fill='red', width=5)

 def Cercle():
      can.create_oval(10, 10, 70, 70, fill='orange', outline='blue')

 fenetre = Tk()
 fenetre.title('Dessin des objets')
 fenetre.resizable(width=False,height=False)
 fenetre.geometry('400x200+100+50')
 fenetre.configure(bg = 'light green')
 can = Canvas(fenetre, bg='white', height=300, width=300)
 can.pack(side = RIGHT)
 btnLine = Button(fenetre,text='Line',width=30,command=Line)
 btnLine.pack()
 btnRectangle = Button(fenetre,text='Rectangle',width=30,command=Rectangle)
 btnRectangle.pack()
 btnDelete = Button(fenetre, text='Effacer', width=30,command=Effacer)
 btnDelete.pack()

 btnCercle = Button(fenetre,text='Cercle',width=30,command=Cercle)
 btnCercle.pack()
 can.bind("<Button-1>", Click)

 can.pack(side = RIGHT)
 cercle = can.create_oval(10, 10, 10+10, 10+10, fill = 'orange', outline = 'gold')
 fenetre.mainloop()

Am looking for a way to create shapes on click after click on a button, and I started to realise I went off road with this code. here is a pic of what I did enter image description here

thanks in advance

Upvotes: 0

Views: 4225

Answers (1)

furas
furas

Reputation: 142734

create_oval() and other methods return object ID

 object_id = can.create_oval(..)

which you can use to work with this object.

 can.move(object_id, offset_x, offset_y)

 can.coords(object_id, (new_x, new_y)) 

More on effbot.org: Canvas

If you create object in function then use global variable to keep it

def Cercle():
    global object_id

    object_id = can.create_oval(10, 10, 70, 70, fill='orange', outline='blue')

def Click(event) :
    can.coords(object_id, event.x, event.y, event.x+10, event.y+10)

Working example

import tkinter as tk

from tkinter import messagebox

def click(event):
    if object_id is not None:
        coord = can.coords(object_id)
        width = coord[2] - coord[0]
        height = coord[3] - coord[1]

        can.coords(object_id, event.x, event.y, event.x+width, event.y+height)

def delete():
    msg = messagebox.askyesnocancel('Info', 'Delete canvas ?')
    if msg == True:
       can.delete(tk.ALL)

def create_rectangle():
    global object_id

    object_id = can.create_rectangle(10, 10, 70, 70, fill='white', outline='blue', width=3)


def create_line():
    global object_id

    object_id = can.create_line(200, 200, 100, 100, fill='red', width=5)

def create_circle():
    global object_id

    object_id = can.create_oval(10, 10, 70, 70, fill='orange', outline='blue')

# --- main ---

object_id = None

fenetre = tk.Tk()
fenetre.title('Dessin des objets')
fenetre.resizable(width=False, height=False)
fenetre.geometry('400x200+100+50')
fenetre.configure(bg='light green')

can = tk.Canvas(fenetre, bg='white', height=300, width=300)
can.pack(side=tk.RIGHT)
can.bind("<Button-1>", click)

btn_line = tk.Button(fenetre, text='Line', width=30, command=create_line)
btn_line.pack()

btn_rectangle = tk.Button(fenetre, text='Rectangle', width=30, command=create_rectangle)
btn_rectangle.pack()

btn_circle = tk.Button(fenetre, text='Circle', width=30, command=create_circle)
btn_circle.pack()

btn_delete = tk.Button(fenetre, text='Delete', width=30, command=delete)
btn_delete.pack()

fenetre.mainloop()

Upvotes: 1

Related Questions