calculator.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import pygame
  2. import sys
  3. import GUI
  4. from random import random
  5. from math import sqrt
  6. pygame.init()
  7. screen = pygame.display.set_mode((256, 384))
  8. pygame.display.set_caption('Calculator')
  9. clock = pygame.time.Clock()
  10. i = []
  11. i.append(GUI.Button(screen, text = '0', width = 64, height = 64, x = 64, y = 320))
  12. i.append(GUI.Button(screen, text = '1', width = 64, height = 64, x = 0, y = 256))
  13. i.append(GUI.Button(screen, text = '2', width = 64, height = 64, x = 64, y = 256))
  14. i.append(GUI.Button(screen, text = '3', width = 64, height = 64, x = 128, y = 256))
  15. i.append(GUI.Button(screen, text = '4', width = 64, height = 64, x = 0, y = 192))
  16. i.append(GUI.Button(screen, text = '5', width = 64, height = 64, x = 64, y = 192))
  17. i.append(GUI.Button(screen, text = '6', width = 64, height = 64, x = 128, y = 192))
  18. i.append(GUI.Button(screen, text = '7', width = 64, height = 64, x = 0, y = 128))
  19. i.append(GUI.Button(screen, text = '8', width = 64, height = 64, x = 64, y = 128))
  20. i.append(GUI.Button(screen, text = '9', width = 64, height = 64, x = 128, y = 128))
  21. i.append(GUI.Button(screen, text = '*', width = 64, height = 48, x = 192, y = 128))
  22. i.append(GUI.Button(screen, text = '/', width = 64, height = 48, x = 192, y = 176))
  23. i.append(GUI.Button(screen, text = '+', width = 64, height = 48, x = 192, y = 224))
  24. i.append(GUI.Button(screen, text = '-', width = 64, height = 48, x = 192, y = 272))
  25. i.append(GUI.Button(screen, text = '=', width = 64, height = 64, x = 192, y = 320))
  26. i.append(GUI.Button(screen, text = 'C', width = 64, height = 64, x = 0, y = 320, hover_color = (255, 150, 150)))
  27. i.append(GUI.Button(screen, text = ',', width = 64, height = 64, x = 128, y = 320))
  28. i.append(GUI.Label(screen, text = '', x = 16, y = 16, font_size = 32))
  29. i.append(GUI.Button(screen, text = '\u03C0', x = 0, y = 96, width = 64, height = 32))
  30. i.append(GUI.Button(screen, text = '^2', x = 64, y = 96, width = 64, height = 32))
  31. i.append(GUI.Button(screen, text = '\u221A', x = 128, y = 96, width = 64, height = 32))
  32. i.append(GUI.Button(screen, text = 'rnd', x = 192, y = 96, width = 64, height = 32))
  33. while True:
  34. clock.tick()
  35. screen.fill((40, 40, 40))
  36. for value in range(len(i)):
  37. i[value].update()
  38. for event in pygame.event.get():
  39. if event.type == pygame.QUIT:
  40. sys.exit()
  41. elif event.type == pygame.MOUSEBUTTONDOWN:
  42. for value in range(10):
  43. if i[value].active(key = event.button):
  44. i[17].add_text(str(value))
  45. if i[10].active(key = event.button):
  46. i[17].add_text('*')
  47. elif i[11].active(key = event.button):
  48. i[17].add_text('/')
  49. elif i[12].active(key = event.button):
  50. i[17].add_text('+')
  51. elif i[13].active(key = event.button):
  52. i[17].add_text('-')
  53. elif i[14].active(key = event.button):
  54. try:
  55. i[17].set_text(str(eval(i[17].get_text())))
  56. except:
  57. i[17].set_text('Error')
  58. elif i[15].active(key = event.button):
  59. i[17].set_text('')
  60. elif i[16].active(key = event.button):
  61. i[17].add_text('.')
  62. elif i[18].active(key = event.button):
  63. i[17].add_text('3.14')
  64. elif i[19].active(key = event.button):
  65. try:
  66. i[17].set_text(str(eval(i[17].get_text())))
  67. i[17].set_text(str(float(i[17].get_text())*float(i[17].get_text())))
  68. except:
  69. i[17].set_text('Error')
  70. elif i[20].active(key = event.button):
  71. try:
  72. i[17].set_text(str(eval(i[17].get_text())))
  73. i[17].set_text(str(sqrt(float(i[17].get_text()))))
  74. except:
  75. i[17].set_text('Error')
  76. elif i[21].active(key = event.button):
  77. i[17].add_text(str(random()))
  78. pygame.display.update()