使用TKINTER和画布单击创建和移动形状

时间:2017-12-07 17:22:18

标签: python canvas tkinter

我希望这对你们来说不是太愚蠢

在第一次使用canvas和tkinter工作时,有点困在这个项目中我所知道的只是基础教程。

我想要的是如何使用按钮精确绘制我想要的形状并移动表单(更新定位)

 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()

在点击按钮后,我正在寻找一种在点击时创建形状的方法,我开始意识到我已经离开了这段代码。 这是我所做的照片 enter image description here

提前致谢

1 个答案:

答案 0 :(得分:1)

create_oval()和其他方法返回object ID

 object_id = can.create_oval(..)

可用于处理此对象。

 can.move(object_id, offset_x, offset_y)

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

有关effbot.org的更多信息:Canvas

如果你在函数中创建对象,那么使用全局变量来保持它

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)

工作示例

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()