123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- import pygame
- import sys
- import GUI
- from random import random
- from math import sqrt
- pygame.init()
- screen = pygame.display.set_mode((256, 384))
- pygame.display.set_caption('Calculator')
- clock = pygame.time.Clock()
- i = []
- i.append(GUI.Button(screen, text = '0', width = 64, height = 64, x = 64, y = 320))
- i.append(GUI.Button(screen, text = '1', width = 64, height = 64, x = 0, y = 256))
- i.append(GUI.Button(screen, text = '2', width = 64, height = 64, x = 64, y = 256))
- i.append(GUI.Button(screen, text = '3', width = 64, height = 64, x = 128, y = 256))
- i.append(GUI.Button(screen, text = '4', width = 64, height = 64, x = 0, y = 192))
- i.append(GUI.Button(screen, text = '5', width = 64, height = 64, x = 64, y = 192))
- i.append(GUI.Button(screen, text = '6', width = 64, height = 64, x = 128, y = 192))
- i.append(GUI.Button(screen, text = '7', width = 64, height = 64, x = 0, y = 128))
- i.append(GUI.Button(screen, text = '8', width = 64, height = 64, x = 64, y = 128))
- i.append(GUI.Button(screen, text = '9', width = 64, height = 64, x = 128, y = 128))
- i.append(GUI.Button(screen, text = '*', width = 64, height = 48, x = 192, y = 128))
- i.append(GUI.Button(screen, text = '/', width = 64, height = 48, x = 192, y = 176))
- i.append(GUI.Button(screen, text = '+', width = 64, height = 48, x = 192, y = 224))
- i.append(GUI.Button(screen, text = '-', width = 64, height = 48, x = 192, y = 272))
- i.append(GUI.Button(screen, text = '=', width = 64, height = 64, x = 192, y = 320))
- i.append(GUI.Button(screen, text = 'C', width = 64, height = 64, x = 0, y = 320, hover_color = (255, 150, 150)))
- i.append(GUI.Button(screen, text = ',', width = 64, height = 64, x = 128, y = 320))
- i.append(GUI.Label(screen, text = '', x = 16, y = 16, font_size = 32))
- i.append(GUI.Button(screen, text = '\u03C0', x = 0, y = 96, width = 64, height = 32))
- i.append(GUI.Button(screen, text = '^2', x = 64, y = 96, width = 64, height = 32))
- i.append(GUI.Button(screen, text = '\u221A', x = 128, y = 96, width = 64, height = 32))
- i.append(GUI.Button(screen, text = 'rnd', x = 192, y = 96, width = 64, height = 32))
- while True:
- clock.tick()
- screen.fill((40, 40, 40))
- for value in range(len(i)):
- i[value].update()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- sys.exit()
- elif event.type == pygame.MOUSEBUTTONDOWN:
- for value in range(10):
- if i[value].active(key = event.button):
- i[17].add_text(str(value))
- if i[10].active(key = event.button):
- i[17].add_text('*')
- elif i[11].active(key = event.button):
- i[17].add_text('/')
- elif i[12].active(key = event.button):
- i[17].add_text('+')
- elif i[13].active(key = event.button):
- i[17].add_text('-')
- elif i[14].active(key = event.button):
- try:
- i[17].set_text(str(eval(i[17].get_text())))
- except:
- i[17].set_text('Error')
- elif i[15].active(key = event.button):
- i[17].set_text('')
- elif i[16].active(key = event.button):
- i[17].add_text('.')
- elif i[18].active(key = event.button):
- i[17].add_text('3.14')
- elif i[19].active(key = event.button):
- try:
- i[17].set_text(str(eval(i[17].get_text())))
- i[17].set_text(str(float(i[17].get_text())*float(i[17].get_text())))
- except:
- i[17].set_text('Error')
- elif i[20].active(key = event.button):
- try:
- i[17].set_text(str(eval(i[17].get_text())))
- i[17].set_text(str(sqrt(float(i[17].get_text()))))
- except:
- i[17].set_text('Error')
- elif i[21].active(key = event.button):
- i[17].add_text(str(random()))
- pygame.display.update()
|