|
1 | 1 | """turtledemo.two_canvases |
2 | 2 |
|
3 | 3 | Use TurtleScreen and RawTurtle to draw on two |
4 | | -distinct canvases. |
| 4 | +distinct canvases in a separate windows. The |
| 5 | +new window must be separately closed in |
| 6 | +addition to pressing the STOP button. |
5 | 7 | """ |
6 | | -#The final mainloop only serves to keep the window open. |
7 | | - |
8 | | -#TODO: This runs in its own two-canvas window when selected in the |
9 | | -#demoviewer examples menu but the text is not loaded and the previous |
10 | | -#example is left visible. If the ending mainloop is removed, the text |
11 | | -#Eis loaded, this run again in a third window, and if start is pressed, |
12 | | -#demoviewer raises an error because main is not found, and then freezes. |
13 | 8 |
|
14 | 9 | from turtle import TurtleScreen, RawTurtle, TK |
15 | 10 |
|
16 | | -root = TK.Tk() |
17 | | -cv1 = TK.Canvas(root, width=300, height=200, bg="#ddffff") |
18 | | -cv2 = TK.Canvas(root, width=300, height=200, bg="#ffeeee") |
19 | | -cv1.pack() |
20 | | -cv2.pack() |
| 11 | +def main(): |
| 12 | + root = TK.Tk() |
| 13 | + cv1 = TK.Canvas(root, width=300, height=200, bg="#ddffff") |
| 14 | + cv2 = TK.Canvas(root, width=300, height=200, bg="#ffeeee") |
| 15 | + cv1.pack() |
| 16 | + cv2.pack() |
21 | 17 |
|
22 | | -s1 = TurtleScreen(cv1) |
23 | | -s1.bgcolor(0.85, 0.85, 1) |
24 | | -s2 = TurtleScreen(cv2) |
25 | | -s2.bgcolor(1, 0.85, 0.85) |
| 18 | + s1 = TurtleScreen(cv1) |
| 19 | + s1.bgcolor(0.85, 0.85, 1) |
| 20 | + s2 = TurtleScreen(cv2) |
| 21 | + s2.bgcolor(1, 0.85, 0.85) |
26 | 22 |
|
27 | | -p = RawTurtle(s1) |
28 | | -q = RawTurtle(s2) |
| 23 | + p = RawTurtle(s1) |
| 24 | + q = RawTurtle(s2) |
29 | 25 |
|
30 | | -p.color("red", (1, 0.85, 0.85)) |
31 | | -p.width(3) |
32 | | -q.color("blue", (0.85, 0.85, 1)) |
33 | | -q.width(3) |
| 26 | + p.color("red", (1, 0.85, 0.85)) |
| 27 | + p.width(3) |
| 28 | + q.color("blue", (0.85, 0.85, 1)) |
| 29 | + q.width(3) |
34 | 30 |
|
35 | | -for t in p,q: |
36 | | - t.shape("turtle") |
37 | | - t.lt(36) |
| 31 | + for t in p,q: |
| 32 | + t.shape("turtle") |
| 33 | + t.lt(36) |
38 | 34 |
|
39 | | -q.lt(180) |
| 35 | + q.lt(180) |
40 | 36 |
|
41 | | -for t in p, q: |
42 | | - t.begin_fill() |
43 | | -for i in range(5): |
44 | 37 | for t in p, q: |
45 | | - t.fd(50) |
46 | | - t.lt(72) |
47 | | -for t in p,q: |
48 | | - t.end_fill() |
49 | | - t.lt(54) |
50 | | - t.pu() |
51 | | - t.bk(50) |
52 | | - |
53 | | -## Want to get some info? |
54 | | - |
55 | | -#print(s1, s2) |
56 | | -#print(p, q) |
57 | | -#print(s1.turtles()) |
58 | | -#print(s2.turtles()) |
59 | | - |
60 | | -TK.mainloop() |
| 38 | + t.begin_fill() |
| 39 | + for i in range(5): |
| 40 | + for t in p, q: |
| 41 | + t.fd(50) |
| 42 | + t.lt(72) |
| 43 | + for t in p,q: |
| 44 | + t.end_fill() |
| 45 | + t.lt(54) |
| 46 | + t.pu() |
| 47 | + t.bk(50) |
| 48 | + |
| 49 | + return "EVENTLOOP" |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + main() |
| 54 | + TK.mainloop() # keep window open until user closes it |
0 commit comments