Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Python-Scientific-Calculator/main.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
241 lines (186 sloc)
7.79 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from subprocess import list2cmdline | |
import pygame | |
pygame.init() | |
win = pygame.display.set_mode((500, 500)) | |
win.fill((235, 235, 235)) | |
pygame.display.set_caption("Calculator") | |
list_of_ops = [] | |
current_num = '' | |
temp_num1 = '' | |
temp_num2 = '' | |
temp_res = 0 | |
class button(): | |
def __init__(self, color, x, y, width, height, text=''): | |
self.color = color | |
self.x = x | |
self.y = y | |
self.width = width | |
self.height = height | |
self.text = text | |
def draw(self, win, outline=None): | |
# Call this method to draw the button on the screen | |
if outline: | |
pygame.draw.circle(win, outline, (self.x + self.width/2, self.y + self.height/2), 30.0, self.width, True, True, True, True) | |
#pygame.draw.rect(win, outline, (self.x - 2, self.y - 2, self.width + 4, self.height + 4), 0) | |
pygame.draw.circle(win, self.color, (self.x + self.width/2, self.y + self.height/2), 30.0, self.width, True, True, True, True) | |
#pygame.draw.rect(win, self.color, (self.x, self.y, self.width, self.height), 0) | |
if self.text != '': | |
font = pygame.font.SysFont('Segoe UI', 35) | |
text = font.render(self.text, 1, (0, 0, 0)) | |
win.blit(text, ( | |
self.x + (self.width / 2 - text.get_width() / 2), self.y + (self.height / 2 - text.get_height() / 2))) | |
def isOver(self, pos): | |
# Pos is the mouse position or a tuple of (x,y) coordinates | |
if pos[0] > self.x and pos[0] < self.x + self.width: | |
if pos[1] > self.y and pos[1] < self.y + self.height: | |
return True | |
return False | |
def eval_L(L): | |
pass | |
run = True | |
seven_button = button((173, 216, 230), 25, 100, 75, 75, "7") | |
eight_button = button((173, 216, 230), 125, 100, 75, 75, "8") | |
nine_button = button((173, 216, 230), 225, 100, 75, 75, "9") | |
four_button = button((173, 216, 230), 25, 200, 75, 75, "4") | |
five_button = button((173, 216, 230), 125, 200, 75, 75, "5") | |
six_button = button((173, 216, 230), 225, 200, 75, 75, "6") | |
one_button = button((173, 216, 230), 25, 300, 75, 75, "1") | |
two_button = button((173, 216, 230), 125, 300, 75, 75, "2") | |
three_button = button((173, 216, 230), 225, 300, 75, 75, "3") | |
zero_button = button((173, 216, 230), 125, 400, 75, 75, "0") | |
plus_button = button((255, 229, 180), 325, 300, 75, 75, "+") | |
minus_button = button((255, 229, 180), 325, 200, 75, 75, "-") | |
multiply_button = button((255, 229, 180), 425, 300, 75, 75, "*") | |
divide_button = button((255, 229, 180), 425, 200, 75, 75, "/") | |
enter_button = button((180, 217, 202), 325, 400, 75, 75, "=") | |
while run: | |
clicked_enter = False | |
one_button.draw(win) | |
two_button.draw(win) | |
three_button.draw(win) | |
four_button.draw(win) | |
five_button.draw(win) | |
six_button.draw(win) | |
seven_button.draw(win) | |
eight_button.draw(win) | |
nine_button.draw(win) | |
zero_button.draw(win) | |
plus_button.draw(win) | |
minus_button.draw(win) | |
multiply_button.draw(win) | |
divide_button.draw(win) | |
enter_button.draw(win) | |
pygame.display.update() | |
# event, anything user do check for event | |
for event in pygame.event.get(): | |
pos = pygame.mouse.get_pos() | |
# pygame.event.get() like a queue of commands | |
if event.type == pygame.QUIT: | |
# pygame.QUIT is the x button, if event type is that | |
run = False | |
if event.type == pygame.MOUSEBUTTONDOWN: | |
if one_button.isOver(pos): | |
current_num += '1' | |
elif two_button.isOver(pos): | |
current_num += '2' | |
elif three_button.isOver(pos): | |
current_num += '3' | |
elif four_button.isOver(pos): | |
current_num += '4' | |
elif five_button.isOver(pos): | |
current_num += '5' | |
elif six_button.isOver(pos): | |
current_num += '6' | |
elif seven_button.isOver(pos): | |
current_num += '7' | |
elif eight_button.isOver(pos): | |
current_num += '8' | |
elif nine_button.isOver(pos): | |
current_num += '9' | |
elif zero_button.isOver(pos): | |
current_num += '0' | |
elif plus_button.isOver(pos): | |
list_of_ops.append(current_num) | |
current_num = '' | |
list_of_ops.append('+') | |
elif minus_button.isOver(pos): | |
list_of_ops.append(current_num) | |
current_num = '' | |
list_of_ops.append('-') | |
elif multiply_button.isOver(pos): | |
list_of_ops.append(current_num) | |
current_num = '' | |
list_of_ops.append('*') | |
elif divide_button.isOver(pos): | |
list_of_ops.append(current_num) | |
current_num = '' | |
list_of_ops.append('/') | |
elif enter_button.isOver(pos): | |
print(list_of_ops) | |
if event.type == pygame.MOUSEMOTION: | |
if one_button.isOver(pos): | |
one_button.color = (193, 226, 250) | |
else: | |
one_button.color = (173, 216, 230) | |
if two_button.isOver(pos): | |
two_button.color = (193, 226, 250) | |
else: | |
two_button.color = (173, 216, 230) | |
if three_button.isOver(pos): | |
three_button.color = (193, 226, 250) | |
else: | |
three_button.color = (173, 216, 230) | |
if four_button.isOver(pos): | |
four_button.color = (193, 226, 250) | |
else: | |
four_button.color = (173, 216, 230) | |
if five_button.isOver(pos): | |
five_button.color = (193, 226, 250) | |
else: | |
five_button.color = (173, 216, 230) | |
if six_button.isOver(pos): | |
six_button.color = (193, 226, 250) | |
else: | |
six_button.color = (173, 216, 230) | |
if seven_button.isOver(pos): | |
seven_button.color = (193, 226, 250) | |
else: | |
seven_button.color = (173, 216, 230) | |
if eight_button.isOver(pos): | |
eight_button.color = (193, 226, 250) | |
else: | |
eight_button.color = (173, 216, 230) | |
if nine_button.isOver(pos): | |
nine_button.color = (193, 226, 250) | |
else: | |
nine_button.color = (173, 216, 230) | |
if zero_button.isOver(pos): | |
zero_button.color = (193, 226, 250) | |
else: | |
zero_button.color = (173, 216, 230) | |
if plus_button.isOver(pos): | |
plus_button.color = (255, 249, 200) | |
else: | |
plus_button.color = (255, 229, 180) | |
if minus_button.isOver(pos): | |
minus_button.color = (255, 249, 200) | |
else: | |
minus_button.color = (255, 229, 180) | |
if multiply_button.isOver(pos): | |
multiply_button.color = (255, 249, 200) | |
else: | |
multiply_button.color = (255, 229, 180) | |
if divide_button.isOver(pos): | |
divide_button.color = (255, 249, 200) | |
else: | |
divide_button.color = (255, 229, 180) | |
if enter_button.isOver(pos): | |
enter_button.color = (200, 237, 222) | |
else: | |
enter_button.color = (180, 217, 202) | |
if clicked_enter: | |
result = eval_L(list_of_ops) | |
print(result) | |
# keys is something that gets pressed | |
keys = pygame.key.get_pressed() | |
pygame.quit() |