-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_loop3.py
More file actions
56 lines (41 loc) · 1.22 KB
/
event_loop3.py
File metadata and controls
56 lines (41 loc) · 1.22 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
46
47
48
49
50
51
52
53
54
55
56
# event_loop3.py
# Color changing window with clicks to enter text
from graphics import *
def handleKey(k, win):
if k == "r":
win.setBackground("pink")
elif k == "w":
win.setBackground("white")
elif k == "g":
win.setBackground("lightgray")
elif k == "b":
win.setBackground("lightblue")
def handleClick(pt, win):
# create an Entry for user to type in
entry = Entry(pt, 10)
entry.draw(win)
# Go modal: wait until user types Return or Escape Key
while True:
key = win.getKey()
if key == "Return":
break
# undraw the entry and draw Text
entry.undraw()
Text(pt, entry.getText()).draw(win)
# clear (ignore) any mouse click that occurred during text entry
win.checkMouse()
def main():
win = GraphWin("Click and Type", 500, 500)
# Event Loop: handle key presses and mouse clicks until the user
# presses the "q" key.
while True:
key = win.checkKey()
if key == "q": # loop exit
break
if key:
handleKey(key, win)
pt = win.checkMouse()
if pt:
handleClick(pt, win)
win.close()
main()