Tk Canvas对象无法绑定

时间:2013-02-04 06:03:25

标签: canvas binding python-3.x tkinter tk

我正在整理一个类似绘画的程序,为某些属于组的对象进行可视化表示。我想让'pencilButton'工作,这样我至少可以知道出了什么问题。

class Diagram:  
""
""
def __init__(self, parent, group):
    self.myParent = parent
    self.group = group
    self.whiteBoard = Canvas(self.myParent, width = 500, height = 500, background="white")
    self.rectButton = ttk.Button(parent, text="rectangle", command=self.bindRectangle)
    self.rectButton.grid()
    self.ovalButton = ttk.Button(parent, text="oval", command=self.bindOval)
    self.ovalButton.grid()
#   self.lineButton =
    self.pencilButton = ttk.Button(parent, text="pencil", command=self.bindPencil)
    self.pencilButton.grid()
#   self.undoButton = 
#   self.picButton =
#   self.thickButton =
#   self.mediumButton =
#   self.thinButton =
#   self.memberButton =
#   self.stepButton =
#   self.clearButton =
#   self.redButton =
#   self.blueButton =
#   self.yellowButton =
#   self.greenButton =
#   self.orangeButton =
#   self.purpleButton =
#   self.blackButton =
#       self.stepMessage =
#   self.memberMessage = tkk.Label(parent, text="")
    self.savedSteps = []
    self.savedMembers = []
    self.saveMemberButton = ttk.Button(parent, text="save member", command=self.saveMember).grid()
    self.saveStepButton = ttk.Button(parent, text="save step", command=self.saveStep).grid()
    self.whiteBoard.grid(column=0, row=0, sticky=(N, W, E, S))
    self.cycleDraw()
    self.lastx = 0
    self.lasty = 0

def bindRectangle(self):
    self.whiteBoard.bind("<Button-1>", self.xy)
    self.whiteBoard.bind("<B1-Motion>", self.addRectangle)

def bindOval(self):
    self.whiteBoard.bind("<Button-1>", self.xy)
    self.whiteBoard.bind("<B1-Motion>", self.addOval)

def bindPencil(self):
    self.whiteBoard.bind("<Button-1>", self.xy)
    self.whiteBoard.bind("<B1-Motion>", self.pencil)

def xy(self, event):
    self.lastx, self.lasty = event.x, event.y
    print("xy bound successfully")

def pencil(self, event):
    self.whiteBoard.create_line((self.lastx, self.lasty, event.x, event.y), fill="red", width=3)
    self.lastx, self.lasty = event.x, event.y
    print("pencil success")

def addOval(self, event):
    self.whiteBoard.create_oval((self.lastx, self.lasty, event.x, event.y))
    self.lastx, self.lasty = event.x, event.y

def addRectangle(self, event):
    self.whiteBoard.create_rectangle((lastx, lasty, event.x, event.y))
    self.lastx, self.lasty = event.x, event.y

def saveStep(self):
    stepList = []
    for obj in self.whiteBoard.find_all():
        if self.whiteBoard.gettags(obj) == ():
            self.whiteBoard.addtag("saved step")
    self.savedSteps.append(stepList)

def saveMember(self):
    memberList = []
    for obj in self.whiteBoard.find_all():
        if self.whiteBoard.gettags(obj) == ():
            self.whiteBoard.addtag(self.currentMember.getName())
            memberList.append(obj)
    self.savedMembers.append(memberList)
    self.cycleDraw().__next__

def cycleDraw(self):
    for mem in self.group.getMembers():
        self.currentMember = mem
        self.myParent.message1.configure(text="Create a visual representation of member %s" % (mem.getName()))

        yield

我很难理解为什么我的'xy'方法无法绑定。正如你所看到的,我有一个print语句来测试xy,这不是调用。

我希望程序告诉用户他正在绘制哪个成员,然后在(铅笔,椭圆,矩形)按钮选择类型时记录对象。现在我正在选择按钮,我按下whiteBoard画布,没有任何内容出现。

感谢您的帮助。

0 个答案:

没有答案