forked from ernestas-poskus/interactive-programming-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_tips3.py
More file actions
45 lines (34 loc) · 1.04 KB
/
code_tips3.py
File metadata and controls
45 lines (34 loc) · 1.04 KB
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
#####################
# Example of event-driven code, buggy version
import simplegui
size = 10
radius = 10
# Define event handlers.
def incr_button_handler():
"""Increment the size."""
global size
size += 1
label.set_text("Value: " + str(size))
def decr_button_handler():
"""Decrement the size."""
global size
if size > 0:
size -= 1
label.set_text("Value: " + str(size))
def change_circle_handler():
"""Change the circle radius."""
global radius
radius = size
radiuslabel.set_text('Radius: ' + str(radius))
def draw_handler(canvas):
"""Draw the circle."""
canvas.draw_circle((100, 100), radius, 5, "Red")
frame = simplegui.create_frame("Home", 200, 200)
label = frame.add_label("Value: " + str(size))
frame.add_button("Increase", incr_button_handler)
frame.add_button("Decrease", decr_button_handler)
radiuslabel = frame.add_label("Radius: " + str(radius))
frame.add_button("Change circle", change_circle_handler)
frame.set_draw_handler(draw_handler)
# Start the frame animation
frame.start()