Reputation: 3657
I am learning tkinter as well as trying to get to grips with classes & objects.
I have created and object that draws a line from a given coordinate to where ever the mouse is clicked. Well that's what I thought I had done but what I get is just a blank canvas.
My code,
from Tkinter import *
class App:
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.can = Canvas(master, width=200, height=100)
self.can.configure(cursor="crosshair")
self.can.pack()
self.start_point = [50, 50]
self.end_point = None
self.can.bind("<Button-1>", lambda ev: self.line_end)
def line_end(self, event):
self.can.create_line(self.start_point[0], self.start_point[1], event.x, event.y)
root = Tk()
app = App(root)
root.mainloop()
Is there some kind of push function like openGL? If so I cant find it.
Upvotes: 0
Views: 1073
Reputation: 385900
The problem is that your binding is not calling your function. You can put a print statement inside your function to prove that.
The reason is that you're using a lambda like this:
self.can.bind("<Button-1>", lambda ev: self.line_end)
Within the body of the lambda you must actually call the function, not just reference it. To do that, you call it like you do anywhere else: with parenthesis on the end:
self.can.bind("<Button-1>", lambda ev: self.line_end(ev))
However, you don't need lambda in this instance. Normally you should just give the command attribute a reference to a function, and not use lambda at all. Lambda is only necessary if you want to pass some additional parameters to your function, which is often not necessary in event-driven programming.
Change your binding to this:
self.can.bind("<Button-1>", self.line_end)
Upvotes: 2