-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttons.py
48 lines (37 loc) · 1.46 KB
/
buttons.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
from graphics import *
class Button:
"""activated() and deactivated() and clicked(p) is a method that returns if the user pressed within the required area"""
def __init__(self, win, center, width, height, label):
"""Create rectangular button e.g qb=Button(myWin,centerPoint,width,height,'Quit')"""
w, h = width / 2.0, height / 2.0
x, y = center.getX(), center.getY()
self.xmax, self.xmin = x + w, x - w
self.ymax, self.ymin = y + h, y - h
p1 = Point(self.xmin, self.ymin)
p2 = Point(self.xmax, self.ymax)
self.rect = Rectangle(p1, p2)
self.rect.setFill("lightgrey")
self.rect.draw(win)
self.label = Text(center, label)
self.label.draw(win)
self.deactivate()
def clicked(self, p):
"""return true if active and inside p"""
return (
self.active
and self.xmin <= p.getX() <= self.xmax
and self.ymin <= p.getY() <= self.ymax
) # use return when you change an already set variable/variables
def getLabel(self):
"""label of the string"""
return self.label.getText() # here as well
def activate(self):
"""sets button to active"""
self.label.setFill("black")
self.rect.setWidth(2)
self.active = True
def deactivate(self):
"""sets button to unactive"""
self.label.setFill("darkgray")
self.rect.setWidth(1)
self.active = False