0

我的代码应该在画布中创建多个图像。我应该可以加入他们的行列。图像已启用拖放功能。我想我需要在画布中处理多个事件。每次我想在两者之间画一条线时,图像也会被拖动。我不知道如何将一个事件专门调用到我的画布 Pease 帮助并为糟糕的代码感到抱歉。

from Tkinter import *
import tkMessageBox


def callback():
        if tkMessageBox.askokcancel("Quit", "Do you really wish to quit?"):
            root.destroy()

class AppUI(Frame):
        def __init__(self, master):

            Frame.__init__(self, master, background="white", relief=FLAT, bd=2, height=768, width=1024)

            self.menubar = Menu(self)

            menu = Menu(self.menubar)
            self.menubar.add_cascade(label="File", menu=menu)
            menu.add_command(label="New")
            menu.add_command(label="Open")
            menu.add_command(label="Save As")
            menu.add_command(label="Save Copy of")
            menu.add_separator()
            menu.add_command(label="exit")

            menu = Menu(self.menubar)
            self.menubar.add_cascade(label="Edit", menu=menu)
            menu.add_command(label="Cut")
            menu.add_command(label="Copy")
            menu.add_command(label="Paste")

            menu = Menu(self.menubar)
            self.menubar.add_cascade(label="Debug", menu=menu)
            menu.add_command(label="Open Debugger")
            menu.add_command(label="Auto-debug")
            menu.add_command(label="Revert")

            menu = Menu(self.menubar)
            self.menubar.add_cascade(label="Run", menu=menu)
            menu.add_command(label="Stimulation")
            menu.add_command(label="Check")
            menu.add_command(label="Scan Module")

            menu = Menu(self.menubar)
            self.menubar.add_cascade(label="Help", menu=menu)
            menu.add_command(label="Help files")
            menu.add_command(label="FTA site")
            menu.add_separator()
            menu.add_command(label="Credits")

            self.master.config(menu=self.menubar)      

            f0 = Frame(root, background="white")

            b1 = Button(f0, bd=2, padx=15, pady=15, justify = LEFT)
            photo1 = PhotoImage(file="Images/p1.gif")
            b1.config(image = photo1,width="50",height="50", command=self.create_image1)
            b1.image = photo1
            b1.pack(side=LEFT)

            b2 = Button(f0, bd=2, padx=15, pady=15, justify = LEFT)
            photo2 = PhotoImage(file="Images/p4.gif")
            b2.config(image=photo2, width="50",height="50", command = self.create_image2)
            b2.image = photo2
            b2.pack(side=LEFT)

            b3 = Button(f0,padx=15, bd=2, pady=15, justify = LEFT)
            photo3 = PhotoImage(file="Images/p8.gif")
            b3.config(image=photo3, width="50",height="50", command = self.create_image3)
            b3.image = photo3
            b3.pack(side=LEFT)

            b4 = Button(f0,padx=15, bd=2, pady=15, justify = LEFT)
            photo4 = PhotoImage(file="Images/p7.gif")
            b4.config(image=photo4, width="50",height="50", command = self.create_image4)
            b4.image = photo4
            b4.pack(side=LEFT)

            b5 = Button(f0,padx=15, bd=2, pady=15, justify = LEFT)
            photo5 = PhotoImage(file="Images/p5.gif")
            b5.config(image=photo5, width="50",height="50", command = self.printtheline)
            b5.image = photo5
            b5.pack(side=LEFT)

            f0.pack(anchor=NW, side=TOP)

            self.imglist = []

            self.canvas = Canvas(self, height=750, width=1500, bg="white")
            self.canvas.pack(side=LEFT)

    def create_image1(self):
            photos1 = PhotoImage(file="Images/p1.gif")
            self.photos1=photos1
            self.imglist.append(photos1)
            self.img1=self.canvas.create_image(60, 60, image=photos1)
            self.canvas.bind("<B1-Motion>", self.move_image1)
            self.canvas.bind("<ButtonRelease-1>")

    def create_image2(self):
            photos2 = PhotoImage(file="Images/p4.gif")
            self.photos2=photos2
            self.imglist.append(photos2)
            self.img2=self.canvas.create_image(60, 60, image=photos2)
            self.canvas.bind("<B1-Motion>", self.move_image2)

    def create_image3(self):
            photos3 = PhotoImage(file="Images/p8.gif")
            self.photos3=photos3
            self.imglist.append(photos3)
            self.img3=self.canvas.create_image(60, 60, image=photos3)
            self.canvas.bind("<B1-Motion>", self.move_image3)

    def create_image4(self):
            photos4 = PhotoImage(file="Images/p7.gif")
            self.photos4=photos4
            self.imglist.append(photos4)
            self.img4=self.canvas.create_image(60, 60, image=photos4)
            self.canvas.bind("<B1-Motion>", self.move_image4)


    def move_image1(self, event):
            self.canvas.delete(self.img1)
            x = event.x
            y = event.y
            self.img1 = self.canvas.create_image(x, y, image=self.photos1, anchor='nw')
            self.canvas.update()
    def move_image2(self, event):
            self.canvas.delete(self.img2)
            x = event.x
            y = event.y
            self.img2 = self.canvas.create_image(x, y, image=self.photos2, anchor='nw')
            self.canvas.update()
    def move_image3(self, event):
            self.canvas.delete(self.img3)
            x = event.x
            y = event.y
            self.img3 = self.canvas.create_image(x, y, image=self.photos3, anchor='nw')
            self.canvas.update()
    def move_image4(self, event):
            self.canvas.delete(self.img4)
            x = event.x
            y = event.y
            self.img4 = self.canvas.create_image(x, y, image=self.photos4, anchor='nw')
            self.canvas.update()

    def getevent(self, event):
            self.x = event.x
            self.y = event.y

    def createline(self, event):
            x0,y0 = (self.x, self.y)
            x1,y1 = (event.x, event.y)
            self.canvas.create_line(x0,y0,x1,y1, fill="black")

    def printtheline(self):
            self.canvas.bind("<Button-1>", self.getevent)
            self.canvas.bind("<ButtonRelease-1>", self.createline)

root = Tk()
root.protocol("WM_DELETE_WINDOW", callback)

app = AppUI(root)
app.pack()

root.mainloop()
4

1 回答 1

0

每次您调用时,self.canvas.bind("<B1-Motion>", ...)您都在破坏您拥有的任何旧绑定并创建一个新绑定。因此,您最后一次调用它是每次都触发的那个。您也绝对不想update从绑定调用的函数中调用。

您需要做的是在鼠标按钮上创建三个不同的绑定。在按下按钮时,您需要确定光标下的对象并保存对它的引用,以及起始 x、y。在按钮运动时,您使用保存的参考并canvas_move使用当前 x,y 调用以计算与上次移动时间的增量。最后,在鼠标按下时,您可以完成任何需要完成的操作。

有关完整工作示例,请参阅问题板绘图代码以移动椭圆的此答案

于 2012-12-08T14:02:31.557 回答