-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEX9.22.py
71 lines (57 loc) · 2.21 KB
/
EX9.22.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 9.22 (Geometry: pendulum) Write a program that animates a pendulum swinging, as
# shown in Figure 9.33. Press the Up arrow key to increase the speed and the Down
# arrow key to decrease it. Press the S key to stop the animation and the R key to
# resume it.
from tkinter import * # Import tkinter
import math
width = 200
height = 200
pendulumRadius = 150
ballRadius = 10
leftAngle = 120
rightAngle = 60
class MainGUI:
def __init__(self):
window = Tk() # Create a window
window.title("Pendulum") # Set a title
self.canvas = Canvas(window, bg="white", width=width, height=height)
self.canvas.pack()
self.angle = leftAngle # Start from leftAngle
self.angleDelta = 1 # Swing interval
self.delay = 100
self.canvas.focus_set()
self.canvas.bind("<Key>", self.speed)
self.on = True
while True:
if self.on:
self.canvas.delete("pendulum")
self.displayPendulum()
self.canvas.after(self.delay)
self.canvas.update()
window.mainloop() # Create an event loop
def displayPendulum(self):
x1 = width / 2
y1 = 20
if self.angle < rightAngle:
self.angleDelta = 1 # Swing to the left
elif self.angle > leftAngle:
self.angleDelta = -1 # Swing to the right
self.angle += self.angleDelta
x = x1 + pendulumRadius * math.cos(math.radians(self.angle))
y = y1 + pendulumRadius * math.sin(math.radians(self.angle))
self.canvas.create_line(x1, y1, x, y, tags="pendulum")
self.canvas.create_oval(x1 - 2, y1 - 2, x1 + 2, y1 + 2, fill="black", tags="pendulum")
self.canvas.create_oval(x - ballRadius, y - ballRadius, x + ballRadius, y + ballRadius,
fill="black", tags="pendulum")
def speed(self, event):
if event.keysym == "Down":
if self.delay < 200:
self.delay += 10
elif event.keysym == "Up":
if self.delay > 10:
self.delay -= 10
elif event.keysym == "s": #causes error
self.on = False
elif event.keysym == "r":
self.on = True
MainGUI()