Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

132928 views
License: OTHER
1
#!/usr/bin/env python
2
#
3
# Copyright 2019 the original author or authors.
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
#
17
import pygame
18
from vqe_playground.utils.colors import BLUE, BLACK, WHITE, LIGHT_GREY
19
from vqe_playground.utils.fonts import *
20
21
22
class Button(pygame.sprite.Sprite):
23
"""Button that may be clicked"""
24
def __init__(self, label, width, height, enabled=True):
25
pygame.sprite.Sprite.__init__(self)
26
self.label = None
27
self.width = width
28
self.height = height
29
self._enabled = enabled
30
31
self.image = pygame.Surface([self.width, self.height])
32
self.image.convert()
33
self.rect = self.image.get_rect()
34
self.rectangle = pygame.Rect(0, 0, self.width, self.height)
35
36
self.font_color = WHITE
37
self.font = ARIAL_36
38
39
self.set_label(label)
40
self.draw_button()
41
42
def update(self):
43
self.draw_button()
44
45
def set_label(self, label):
46
self.label = label
47
48
def get_enabled(self):
49
return self._enabled
50
51
def set_enabled(self, enabled):
52
self._enabled = enabled
53
self.draw_button()
54
55
def draw_button(self):
56
self.image.fill(BLUE if self._enabled else LIGHT_GREY)
57
pygame.draw.rect(self.image, BLACK, self.rectangle, 1)
58
59
text_surface = self.font.render(self.label, False, self.font_color)
60
text_xpos = (self.rect.width - text_surface.get_rect().width) / 2
61
text_ypos = (self.rect.height - text_surface.get_rect().height) / 2
62
self.image.blit(text_surface, (text_xpos, text_ypos))
63
64