-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjbws_button.py
More file actions
executable file
·62 lines (47 loc) · 2.06 KB
/
Copy pathjbws_button.py
File metadata and controls
executable file
·62 lines (47 loc) · 2.06 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
57
58
59
60
61
62
# button.py by Jack Beal
from graphics import *
class Button:
""" A button is a labelled Zelle rectangle in a window. Its active state is a bool toggle.
Using activate() or deactivate() this can be switched.
isClicked() will return true is the button is active and the clickpoint is inside of the rectangle defining the button.
"""
def __init__(self, win, lbl, cpt, width, height, active):
""" Makes a rectangular button wherein <win> is a Zelle window into which to draw, <label> is a string label for the button, <cpt> is a Zelle point object for the central anchor of the button, <width> and <height> are integers specifying the dimensions.
"""
self.label = Text(cpt, lbl)
self.active = active
self.win = win
self.cpt = cpt
x,y = self.cpt.getX(), self.cpt.getY()
self.xMin = x - width / 2
self.xMax = x + width / 2
self.yMin = y - height / 2
self.yMax = y + height / 2
self.rect = Rectangle(Point(self.xMin, self.yMin), Point(self.xMax, self.yMax))
self.rect.draw(self.win)
self.label.draw(win)
if self.active == True:
self.activate()
elif self.active == False:
self.deactivate()
def activate(self):
""" Sets a Button object to state Active by making the text black and makes the outline bolder, then sets flag Active to True
"""
self.label.setFill("black")
self.rect.setWidth(2)
self.rect.setFill("grey")
self.active = True
def deactivate(self):
"""Sets this button to 'inactive' . """
self.label.setFill('darkgray')
self.rect.setWidth(.5)
self.active = False
def isClicked(self, p):
if self.active == True:
"""Returns true if button active and Point p is inside"""
if (p.getX() < self.xMax) and (p.getX() > self.xMin) and (p.getY() < self.yMax) and (p.getY() > self.yMin):
return True
else:
return False
else:
return False