blob: c74c1c3e7bcbc01f38975fd92e68bc41ea05e950 [file] [log] [blame]
Guido van Rossum35820f71994-10-07 09:55:26 +00001from Tkinter import *
2
3# this file demonstrates the creation of widgets as part of a canvas object
4
5class Test(Frame):
6 ###################################################################
7 ###### Event callbacks for THE CANVAS (not the stuff drawn on it)
8 ###################################################################
9 def mouseDown(self, event):
Tim Peters182b5ac2004-07-18 06:16:08 +000010 # see if we're inside a dot. If we are, it
11 # gets tagged as CURRENT for free by tk.
Guido van Rossum35820f71994-10-07 09:55:26 +000012
Tim Peters182b5ac2004-07-18 06:16:08 +000013 if not event.widget.find_withtag(CURRENT):
14 # there is no dot here, so we can make one,
15 # and bind some interesting behavior to it.
16 # ------
17 # create a dot, and mark it as current
18 fred = self.draw.create_oval(
19 event.x - 10, event.y -10, event.x +10, event.y + 10,
20 fill="green")
21 self.draw.tag_bind(fred, "<Enter>", self.mouseEnter)
22 self.draw.tag_bind(fred, "<Leave>", self.mouseLeave)
23 self.lastx = event.x
24 self.lasty = event.y
Guido van Rossum89cb67b1996-07-30 18:57:18 +000025
Guido van Rossum35820f71994-10-07 09:55:26 +000026 def mouseMove(self, event):
Tim Peters182b5ac2004-07-18 06:16:08 +000027 self.draw.move(CURRENT, event.x - self.lastx, event.y - self.lasty)
28 self.lastx = event.x
29 self.lasty = event.y
Guido van Rossum35820f71994-10-07 09:55:26 +000030
31 ###################################################################
32 ###### Event callbacks for canvas ITEMS (stuff drawn on the canvas)
33 ###################################################################
34 def mouseEnter(self, event):
35 # the "current" tag is applied to the object the cursor is over.
Tim Peters182b5ac2004-07-18 06:16:08 +000036 # this happens automatically.
37 self.draw.itemconfig(CURRENT, fill="red")
Collin Winter6f2df4d2007-07-17 20:59:35 +000038 print(self.draw.coords(CURRENT))
Tim Peters182b5ac2004-07-18 06:16:08 +000039
Guido van Rossum35820f71994-10-07 09:55:26 +000040 def mouseLeave(self, event):
Tim Peters182b5ac2004-07-18 06:16:08 +000041 # the "current" tag is applied to the object the cursor is over.
42 # this happens automatically.
43 self.draw.itemconfig(CURRENT, fill="blue")
Guido van Rossum35820f71994-10-07 09:55:26 +000044
45 def createWidgets(self):
Tim Peters182b5ac2004-07-18 06:16:08 +000046 self.QUIT = Button(self, text='QUIT', foreground='red',
47 command=self.quit)
48 self.QUIT.pack(side=LEFT, fill=BOTH)
49 self.draw = Canvas(self, width="5i", height="5i")
50 self.draw.pack(side=LEFT)
Guido van Rossum89cb67b1996-07-30 18:57:18 +000051
Tim Peters182b5ac2004-07-18 06:16:08 +000052 Widget.bind(self.draw, "<1>", self.mouseDown)
53 Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)
Guido van Rossum35820f71994-10-07 09:55:26 +000054
55 def __init__(self, master=None):
Tim Peters182b5ac2004-07-18 06:16:08 +000056 Frame.__init__(self, master)
57 Pack.config(self)
58 self.createWidgets()
Guido van Rossum35820f71994-10-07 09:55:26 +000059
60test = Test()
61test.mainloop()