import sys
import pygame
from pygame.locals import *
one = pygame.image.load('jisuanxia/py1.png')
two = pygame.image.load('jisuanxia/py2.png')
three = pygame.image.load('jisuanxia/py3.png')
four = pygame.image.load('jisuanxia/py4.png')
five = pygame.image.load('jisuanxia/py5.png')
six = pygame.image.load('jisuanxia/py6.png')
seven = pygame.image.load('jisuanxia/py7.png')
eight = pygame.image.load('jisuanxia/py8.png')
nine = pygame.image.load('jisuanxia/py9.png')
zero = pygame.image.load('jisuanxia/py0.png')
jia = pygame.image.load('jisuanxia/jia.png')
jian = pygame.image.load('jisuanxia/jian.png')
cheng = pygame.image.load('jisuanxia/cheng.png')
chu = pygame.image.load('jisuanxia/pychu.png')
deng = pygame.image.load('jisuanxia/deng.png')
guiling = pygame.image.load('jisuanxia/guiling.png')
jieguo = pygame.image.load('jisuanxia/jieguo.png')
num = ''
sum = ''
pygame.init()
screen_size = width,height = 430, 500
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption("计算器")
def event_handle():
global num
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
screen.blit(jieguo,(0,0))
x,y = pygame.mouse.get_pos()
if x>=110 and x<=210 and y>=400 and y<=500:
num += str(0)
filltext(num)
pygame.display.update()
elif x>=0 and x<=100 and y>=290 and y<=390:
num += str(1)
filltext(num)
pygame.display.update()
elif x>=110 and x<=210 and y>=290 and y<=390:
num += str(2)
filltext(num)
pygame.display.update()
elif x>=220 and x<=320 and y>=290 and y<=390:
num += str(3)
filltext(num)
pygame.display.update()
elif x>=0 and x<=100 and y>=180 and y<=280:
num += str(4)
filltext(num)
pygame.display.update()
elif x>=110 and x<=210 and y>=180 and y<=280:
num += str(5)
filltext(num)
pygame.display.update()
elif x>=220 and x<=320 and y>=180 and y<=280:
num += str(6)
filltext(num)
pygame.display.update()
elif x>=0 and x<=100 and y>=70 and y<=170:
num += str(7)
filltext(num)
pygame.display.update()
elif x>=110 and x<=210 and y>=70 and y<=170:
num += str(8)
filltext(num)
pygame.display.update()
elif x>=220 and x<=320 and y>=70 and y<=170:
num += str(9)
filltext(num)
pygame.display.update()
elif x>=330 and x<=430 and y>=70 and y<=170:
num += '+'
filltext(num)
pygame.display.update()
elif x>=330 and x<=430 and y>=180 and y<=280:
num += '-'
filltext(num)
pygame.display.update()
elif x>=330 and x<=430 and y>=290 and y<=390:
num += '*'
filltext(num)
pygame.display.update()
elif x>=330 and x<=430 and y>=400 and y<=500:
num += '/'
filltext(num)
pygame.display.update()
elif x>=0 and x<=100 and y>=400 and y<=500:
num = ''
screen.blit(jieguo,(0,0))
pygame.display.update()
elif x>=220 and x<=320 and y>=400 and y<=500:#等号
total = eval(num)
num = str(total)
screen.blit(jieguo,(0,0))
filltext(str(total))
pygame.display.update()
elif event.type == pygame.KEYDOWN:
screen.blit(jieguo,(0,0))
print(event.key)
if event.key == 48 or event.key == 256:
num += str(0)
filltext(num)
pygame.display.update()
elif event.key == 49 or event.key == 257:
num += str(1)
filltext(num)
pygame.display.update()
elif event.key == 50 or event.key == 258:
num += str(2)
filltext(num)
pygame.display.update()
elif event.key == 51 or event.key == 259:
num += str(3)
filltext(num)
pygame.display.update()
elif event.key == 52 or event.key == 260:
num += str(4)
filltext(num)
pygame.display.update()
elif event.key == 53 or event.key == 261:
num += str(5)
filltext(num)
pygame.display.update()
elif event.key == 54 or event.key == 262:
num += str(6)
filltext(num)
pygame.display.update()
elif event.key == 55 or event.key == 263:
num += str(7)
filltext(num)
pygame.display.update()
elif event.key == 56 or event.key == 264:
num += str(8)
filltext(num)
pygame.display.update()
elif event.key == 57 or event.key == 265:
num += str(9)
filltext(num)
pygame.display.update()
elif event.key == 61 or event.key == 270:
num += '+'
filltext(num)
pygame.display.update()
elif event.key == 45 or event.key == 269:
num += '-'
filltext(num)
pygame.display.update()
elif event.key == 268:
num += '*'
filltext(num)
pygame.display.update()
elif event.key == 47 or event.key == 267:
num += '/'
filltext(num)
pygame.display.update()
elif event.key ==13 or event.key == 271:#等号
total = eval(num)
num = str(total)
screen.blit(jieguo,(0,0))
filltext(str(total))
pygame.display.update()
def filltext(text):
f = pygame.font.SysFont('微软雅黑',50)
text = f.render(text,True,(0,0,0))
screen.blit(text,(15,20))
def calculator():
screen.fill((255,255,255))
screen.blit(seven,(0,70))
screen.blit(eight,(110,70))
screen.blit(nine,(220,70))
screen.blit(four,(0,180))
screen.blit(five,(110,180))
screen.blit(six,(220,180))
screen.blit(one,(0,290))
screen.blit(two,(110,290))
screen.blit(three,(220,290))
screen.blit(zero,(110,400))
screen.blit(guiling,(0,400))
screen.blit(jia,(330,70))
screen.blit(jian,(330,180))
screen.blit(cheng,(330,290))
screen.blit(chu,(330,400))
screen.blit(deng,(220,400))
screen.blit(jieguo,(0,0))
while True:
event_handle()
pygame.display.update()
calculator()