blob: 2368733d0419d482df2907449c0401d3269b0255 [file] [log] [blame]
Guido van Rossum35820f71994-10-07 09:55:26 +00001from Tkinter import *
2
3# allows moving dots with multiple selection.
4
5SELECTED_COLOR = "red"
6UNSELECTED_COLOR = "blue"
7
8class Test(Frame):
9 ###################################################################
10 ###### Event callbacks for THE CANVAS (not the stuff drawn on it)
11 ###################################################################
12 def mouseDown(self, event):
13 # see if we're inside a dot. If we are, it
Guido van Rossum89cb67b1996-07-30 18:57:18 +000014 # gets tagged as CURRENT for free by tk.
Guido van Rossum35820f71994-10-07 09:55:26 +000015
Guido van Rossum89cb67b1996-07-30 18:57:18 +000016 if not event.widget.find_withtag(CURRENT):
Guido van Rossum35820f71994-10-07 09:55:26 +000017 # we clicked outside of all dots on the canvas. unselect all.
18
19 # re-color everything back to an unselected color
Guido van Rossum89cb67b1996-07-30 18:57:18 +000020 self.draw.itemconfig("selected", fill=UNSELECTED_COLOR)
Guido van Rossum35820f71994-10-07 09:55:26 +000021 # unselect everything
22 self.draw.dtag("selected")
23 else:
24 # mark as "selected" the thing the cursor is under
Guido van Rossum89cb67b1996-07-30 18:57:18 +000025 self.draw.addtag("selected", "withtag", CURRENT)
Guido van Rossum35820f71994-10-07 09:55:26 +000026 # color it as selected
Guido van Rossum89cb67b1996-07-30 18:57:18 +000027 self.draw.itemconfig("selected", fill=SELECTED_COLOR)
Guido van Rossum35820f71994-10-07 09:55:26 +000028
29 self.lastx = event.x
30 self.lasty = event.y
31
32
33 def mouseMove(self, event):
34 self.draw.move("selected", event.x - self.lastx, event.y - self.lasty)
35 self.lastx = event.x
36 self.lasty = event.y
37
38 def makeNewDot(self):
39 # create a dot, and mark it as current
40 fred = self.draw.create_oval(0, 0, 20, 20,
Guido van Rossum89cb67b1996-07-30 18:57:18 +000041 fill=SELECTED_COLOR, tags=CURRENT)
Guido van Rossum35820f71994-10-07 09:55:26 +000042 # and make it selected
Guido van Rossum89cb67b1996-07-30 18:57:18 +000043 self.draw.addtag("selected", "withtag", CURRENT)
Guido van Rossum35820f71994-10-07 09:55:26 +000044
45 def createWidgets(self):
Guido van Rossum89cb67b1996-07-30 18:57:18 +000046 self.QUIT = Button(self, text='QUIT', foreground='red',
47 command=self.quit)
Guido van Rossum35820f71994-10-07 09:55:26 +000048
49 ################
50 # make the canvas and bind some behavior to it
51 ################
Guido van Rossum89cb67b1996-07-30 18:57:18 +000052 self.draw = Canvas(self, width="5i", height="5i")
Guido van Rossum35820f71994-10-07 09:55:26 +000053 Widget.bind(self.draw, "<1>", self.mouseDown)
54 Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)
55
Guido van Rossum35820f71994-10-07 09:55:26 +000056 # and other things.....
Guido van Rossum89cb67b1996-07-30 18:57:18 +000057 self.button = Button(self, text="make a new dot", foreground="blue",
58 command=self.makeNewDot)
Guido van Rossum35820f71994-10-07 09:55:26 +000059
Guido van Rossum89cb67b1996-07-30 18:57:18 +000060 message = ("%s dots are selected and can be dragged.\n"
61 "%s are not selected.\n"
62 "Click in a dot to select it.\n"
63 "Click on empty space to deselect all dots."
64 ) % (SELECTED_COLOR, UNSELECTED_COLOR)
65 self.label = Message(self, width="5i", text=message)
Guido van Rossum35820f71994-10-07 09:55:26 +000066
Guido van Rossum89cb67b1996-07-30 18:57:18 +000067 self.QUIT.pack(side=BOTTOM, fill=BOTH)
68 self.label.pack(side=BOTTOM, fill=X, expand=1)
69 self.button.pack(side=BOTTOM, fill=X)
70 self.draw.pack(side=LEFT)
Guido van Rossum35820f71994-10-07 09:55:26 +000071
72 def __init__(self, master=None):
73 Frame.__init__(self, master)
74 Pack.config(self)
75 self.createWidgets()
76
77test = Test()
78test.mainloop()
79
80
81