snakeware

Python-based Linux distro for x86-64 and RPi
git clone git://squid-tech.com/snakeware.git
Log | Files | Refs | README

commit 53119c82febee669f7225d1d0dfabd56a748e31c
parent b2225218ccedfc16df5f70d30af9bcbd29294537
Author: Josh Moore <jxm5210@rit.edu>
Date:   Fri, 29 May 2020 14:28:41 -0400

Add calculator app

Diffstat:
Asnakewm/apps/tools/snakecalc/__init__.py | 13+++++++++++++
Asnakewm/apps/tools/snakecalc/calc.py | 96+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 109 insertions(+), 0 deletions(-)

diff --git a/snakewm/apps/tools/snakecalc/__init__.py b/snakewm/apps/tools/snakecalc/__init__.py @@ -0,0 +1,13 @@ +from .calc import SnakeCalc + +def load(manager, params): + """ + Create and launch a new instance of SnakeCalc. + """ + # default position + pos = (100, 100) + + if params is not None and len(params) > 0: + pos = params[0] + + SnakeCalc(pos, manager) diff --git a/snakewm/apps/tools/snakecalc/calc.py b/snakewm/apps/tools/snakecalc/calc.py @@ -0,0 +1,96 @@ +import pygame +import pygame_gui + +class SnakeCalc(pygame_gui.elements.UIWindow): + # operations to be converted to buttons + OPS = '+-/*1234567890pxC=' + + # button dimensions + BSIZE = (67, 75) + + # user input + USERCALC = '' + + def __init__(self, pos, manager): + super().__init__( + pygame.Rect(pos, (300, 475)), + manager=manager, + window_display_title='snakecalc', + object_id='#snaketerm', + resizable=True + ) + + self.textbox = pygame_gui.elements.UITextBox( + '', + relative_rect=pygame.Rect(0, 1, 268, 40), + manager=manager, + container=self, + anchors={ + 'left': 'left', + 'right': 'right', + 'top': 'top', + 'bottom': 'bottom' + } + ) + + # generate calculator buttons + for i in range(len(self.OPS)): + if self.OPS[i] == 'x': + # skip placeholder ops + continue + + pygame_gui.elements.UIButton( + relative_rect=pygame.Rect( + (i % 4 * self.BSIZE[0], + 40 + int(i / 4) * self.BSIZE[1]), + self.BSIZE + ), + text='.' if self.OPS[i] == 'p' else self.OPS[i], + manager=manager, + container=self, + object_id='#op-' + self.OPS[i] + ) + + def process_event(self, event): + super().process_event(event) + + if event.type == pygame.USEREVENT: + if event.user_type == pygame_gui.UI_BUTTON_PRESSED: + self.input_op(event.ui_object_id[-1]) + return True + + def set_text(self, text): + self.textbox.html_text = text + self.textbox.rebuild() + + def append_text(self, text): + self.textbox.html_text = self.textbox.html_text + text + self.textbox.rebuild() + + def calculate(self, expression): + """ + Perform the actual calculation based on user input. + """ + result = '' + + try: + result = str(eval(expression)) + except Exception: + result = 'Error' + + self.set_text(result) + + def input_op(self, op): + """ + Called to append user input ops to the existing input. + """ + if op in self.OPS: + if op == '=': + # perform the calculation + self.calculate(self.textbox.html_text) + elif op == 'C': + self.set_text('') + elif op == 'p': + self.append_text('.') + else: + self.append_text(op)