blob: 1e49ba667c84712f58016443c6dcf1d2cc74aee7 [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
14 # gets tagged as "current" for free by tk.
15
16 if not event.widget.find_withtag("current"):
17 # we clicked outside of all dots on the canvas. unselect all.
18
19 # re-color everything back to an unselected color
20 self.draw.itemconfig("selected", {"fill" : UNSELECTED_COLOR})
21 # unselect everything
22 self.draw.dtag("selected")
23 else:
24 # mark as "selected" the thing the cursor is under
25 self.draw.addtag("selected", "withtag", "current")
26 # color it as selected
27 self.draw.itemconfig("selected", {"fill": SELECTED_COLOR})
28
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,
41 {"fill" : SELECTED_COLOR, "tag" : "current"})
42 # and make it selected
43 self.draw.addtag("selected", "withtag", "current")
44
45 def createWidgets(self):
46 self.QUIT = Button(self, {'text': 'QUIT',
47 'fg': 'red',
48 'command': self.quit})
49
50 ################
51 # make the canvas and bind some behavior to it
52 ################
53 self.draw = Canvas(self, {"width" : "5i", "height" : "5i"})
54 Widget.bind(self.draw, "<1>", self.mouseDown)
55 Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)
56
57
58 # and other things.....
59 self.button = Button(self, {"text" : "make a new dot",
60 "command" : self.makeNewDot,
61 "fg" : "blue"})
62
63 self.label = Message(self,
64 {"width" : "5i",
65 "text" : SELECTED_COLOR + " dots are selected and can be dragged.\n" +
66 UNSELECTED_COLOR + " are not selected.\n" +
67 "Click in a dot to select it.\n" +
68 "Click on empty space to deselect all dots." })
69
70 self.QUIT.pack({'side': 'bottom', 'fill': 'both'})
71 self.label.pack({"side" : "bottom", "fill" : "x", "expand" : 1})
72 self.button.pack({"side" : "bottom", "fill" : "x"})
73 self.draw.pack({'side': 'left'})
74
75
76 def __init__(self, master=None):
77 Frame.__init__(self, master)
78 Pack.config(self)
79 self.createWidgets()
80
81test = Test()
82test.mainloop()
83
84
85