用户:
蟒蛇喵查看:2 回复:2 评论:2 创建时间:2023-02-26T10:50:44
蟒蛇喵计算器
import turtle as t
import sympy as s
x = s.Symbol('x')
bg = t.Pen()
click = t.Pen()
pen = t.Pen()
pen_2 = t.Pen()
sc = t.Screen()
sc.title('计算器')
sc.bgcolor('gold')
bg.hideturtle()
click.hideturtle()
pen.hideturtle()
pen.speed(0)
pen.up()
pen_2.hideturtle()
pen_2.speed(0)
pen_2.up()
click.speed(0)
click.hideturtle()
bg.up()
bg.speed(0)
about = t.Pen()
about.hideturtle()
about.speed(0)
about.up()
sc.tracer(False)
def lstToStr(lst):
# 传入一个列表并把它转为字符串
outPut = ''
for i in lst:
outPut += i
return outPut
def formula_write(getValue):
# 对用户输入的公式重新格式化并显示
if True:
if len(list(getValue)) <= 10:
pen.goto(150, 105)
pen.write(firstNum, align='right', font=('楷体', 36, 'bold'))
elif len(list(getValue)) >= 11 and len(list(getValue)) <= 24:
lstgetValue = list(getValue)
lstgetValueLine_1 = lstgetValue[:12]
getValueLine_1 = lstToStr(lstgetValueLine_1)
lstgetValueLine_2 = lstgetValue[12:]
getValueLine_2 = lstToStr(lstgetValueLine_2)
pen.goto(150,130)
pen.write(getValueLine_1,True,'right',('楷体',30,'bold'))
pen.goto(150,95)
pen.write(getValueLine_2,True,'right',('楷体',30,'bold'))
else:
return
def draw_block(x_left, x_right, y_up, y_down, prt,prtLine2=''):
# 绘制单个按钮(‘+’、‘-’、‘*’、‘/’、‘1’、‘2’等)
bg.up()
bg.fillcolor('white')
bg.goto(x_left, y_down)
bg.down()
bg.begin_fill()
bg.goto(x_right, y_down)
bg.goto(x_right, y_up)
bg.goto(x_left, y_up)
bg.goto(x_left, y_down)
bg.end_fill()
bg.up()
if prtLine2 != '':
bg.goto(x_left-37.5, y_down+24)
bg.write(prt, align='center', font=('微软雅黑', 11, 'bold'))
bg.goto(x_left-37.5, y_down+6)
bg.write(prtLine2, align='center', font=('微软雅黑', 11, 'bold'))
else:
bg.goto(x_left-37.5, y_down+15)
bg.write(prt, align='center', font=('微软雅黑', 16, 'bold'))
def draw_block_spe(x_left, x_right, y_up, y_down, prt,prtLine2=''):
# 用于绘制关于界面图标的专用函数,区别是更改了绘制按钮的画笔对象
about.up()
about.pencolor('black')
about.fillcolor('white')
about.goto(x_left, y_down)
about.down()
about.begin_fill()
about.goto(x_right, y_down)
about.goto(x_right, y_up)
about.goto(x_left, y_up)
about.goto(x_left, y_down)
about.end_fill()
about.up()
if prtLine2 != '':
about.goto(x_left-37.5, y_down+23)
about.write(prt, align='center', font=('微软雅黑', 12, 'bold'))
about.goto(x_left-37.5, y_down+7)
about.write(prtLine2, align='center', font=('微软雅黑', 12, 'bold'))
else:
about.goto(x_left-37.5, y_down+15)
about.write(prt, align='center', font=('微软雅黑', 16, 'bold'))
def draw_bg_normal():
# 绘制标准模式的界面
sc.screensize(340, 500)
sc.setup(350, 510)
bg.clear()
bg.pensize(5)
bg.up()
bg.goto(170, 250)
bg.down()
bg.fillcolor('#CCFFFF')
bg.begin_fill()
bg.goto(170, -250)
bg.goto(-170, -250)
bg.goto(-170, 250)
bg.goto(170, 250)
bg.end_fill()
bg.up()
bg.pensize(3)
bg.goto(-150,235)
bg.down()
bg.goto(-130,235)
bg.up()
bg.goto(-150, 225)
bg.down()
bg.goto(-130, 225)
bg.up()
bg.goto(-150, 215)
bg.down()
bg.goto(-130, 215)
bg.up()
bg.goto(-130, 230)
bg.pensize(5)
bg.goto(-100, 210)
bg.write('标准模式 by-蟒蛇喵', font=('微软雅黑', 16, 'bold'))
bg.goto(-160, 200)
bg.pensize(5)
bg.down()
bg.goto(160, 200)
bg.up()
bg.goto(160, 70)
bg.down()
bg.goto(-160, 70)
# line_-1
draw_block(-75, -150, 60, 10, '负号')
draw_block(0, -75, 60, 10, '复位')
draw_block(75, 0, 60, 10, '退格')
draw_block(150,75,60,10,'÷')
# line_1
draw_block(-75, -150, 10, -40, '1')
draw_block(0, -75, 10, -40, '2')
draw_block(75, 0, 10, -40, '3')
draw_block(150,75,10,-40,'+')
# line_2
draw_block(-75, -150, -40, -90, '4')
draw_block(0, -75, -40, -90, '5')
draw_block(75, 0, -40, -90, '6')
draw_block(150, 75, -40, -90, '-')
# line_3
draw_block(-75, -150, -90, -140, '7')
draw_block(0, -75, -90, -140, '8')
draw_block(75, 0, -90, -140, '9')
draw_block(150, 75, -90, -140, '×')
# line_4
draw_block(-75, -150, -140, -190, '乘方')
draw_block(0, -75, -140, -190, '0')
draw_block(75, 0, -140, -190, '.')
draw_block(喵, '=')
def draw_about():
# 绘制关于界面
about.up()
about.goto(-150,200)
about.pencolor('white')
about.fillcolor('#66CCFF')
about.begin_fill()
about.down()
about.pensize(5)
about.goto(150, 200)
about.goto(150, -200)
about.goto(-150, -200)
about.goto(-150, 200)
about.end_fill()
about.goto(150,200)
about.pencolor('white')
about.fillcolor('red')
about.begin_fill()
about.down()
about.goto(150, 175)
about.goto(125, 175)
about.goto(125, 200)
about.goto(150,200)
about.end_fill()
about.up()
about.goto(0,160)
about.write('制作者:', align='center', font=('微软雅黑', 15, 'bold'))
about.goto(0,115)
about.write('蟒蛇喵', align='center', font=('微软雅黑', 28, 'bold'))
about.goto(0,75)
about.write('特别感谢:', align='center', font=('微软雅黑', 15, 'bold'))
about.goto(0,30)
about.write('FGR corttal', align='center', font=('微软雅黑', 28, 'bold'))
about.goto(0,-170)
about.write('FGR 制作', align='center', font=('微软雅黑', 10, 'bold'))
about.goto(0,-190)
about.write('用于work', align='center', font=('微软雅黑', 10, 'bold'))
draw_block_spe(0, -75, 10, -40, '+')
draw_block_spe(75, 0, 10, -40, '-')
draw_block_spe(0, -75, -40, -90, '×')
draw_block_spe(75, 0, -40, -90, '÷')
def draw_TypeOfCurrency():
# 绘制货币选择界面
about.up()
about.goto(-150,200)
about.pencolor('white')
about.fillcolor('#66CCFF')
about.begin_fill()
about.down()
about.pensize(5)
about.goto(150, 200)
about.goto(150, -200)
about.goto(-150, -200)
about.goto(-150, 200)
about.end_fill()
about.goto(150,200)
about.pencolor('white')
about.fillcolor('red')
about.begin_fill()
about.down()
about.goto(150, 175)
about.goto(125, 175)
about.goto(125, 200)
about.goto(150,200)
about.end_fill()
about.up()
about.goto(0,160)
about.write('制作者:', align='center', font=('微软雅黑', 15, 'bold'))
about.goto(0,115)
about.write('蟒蛇喵', align='center', font=('微软雅黑', 28, 'bold'))
about.goto(0,75)
about.write('特别感谢:', align='center', font=('微软雅黑', 15, 'bold'))
about.goto(0,30)
about.write('深圳市点猫科技有限公司', align='center', font=('微软雅黑', 28, 'bold'))
about.goto(0,-170)
about.write('张宸铭小作坊 制作', align='center', font=('微软雅黑', 10, 'bold'))
about.goto(0,-190)
about.write('用于办公', align='center', font=('微软雅黑', 10, 'bold'))
draw_block_spe(0, -75, 10, -40, '+')
draw_block_spe(75, 0, 10, -40, '-')
draw_block_spe(0, -75, -40, -90, '×')
draw_block_spe(75, 0, -40, -90, '÷')
def draw_bg_math():
# 绘制数学模式(公式模式)的界面
bg.clear()
sc.screensize(340, 500)
sc.setup(350, 510)
bg.pensize(5)
bg.up()
bg.goto(170, 275)
bg.down()
bg.fillcolor('#CCFFFF')
bg.begin_fill()
bg.goto(170, -275)
bg.goto(-170, -275)
bg.goto(-170, 275)
bg.goto(170, 275)
bg.end_fill()
bg.up()
bg.pensize(3)
bg.goto(-150,235)
bg.down()
bg.goto(-130,235)
bg.up()
bg.goto(-150, 225)
bg.down()
bg.goto(-130, 225)
bg.up()
bg.goto(-150, 215)
bg.down()
bg.goto(-130, 215)
bg.up()
bg.goto(-130, 230)
bg.pensize(5)
bg.goto(-100, 210)
bg.write('科学模式 by-蟒蛇喵', font=('微软雅黑', 16, 'bold'))
bg.goto(-160, 200)
bg.pensize(5)
bg.down()
bg.goto(160, 200)
bg.up()
bg.goto(160, 70)
bg.down()
bg.goto(-160, 70)
# line_-1
draw_block(-75, -150, 60, 10, '负号')
draw_block(0, -75, 60, 10, '复位')
draw_block(75, 0, 60, 10, '退格')
draw_block(150,75,60,10,'÷')
# line_1
draw_block(-75, -150, 10, -40, '1')
draw_block(0, -75, 10, -40, '2')
draw_block(75, 0, 10, -40, '3')
draw_block(150,75,10,-40,'+')
# line_2
draw_block(-75, -150, -40, -90, '4')
draw_block(0, -75, -40, -90, '5')
draw_block(75, 0, -40, -90, '6')
draw_block(150, 75, -40, -90, '-')
# line_3
draw_block(-75, -150, -90, -140, '7')
draw_block(0, -75, -90, -140, '8')
draw_block(75, 0, -90, -140, '9')
draw_block(150, 75, -90, -140, '×')
# line_4
draw_block(-75, -150, -140, -190, '乘方')
draw_block(0, -75, -140, -190, '0')
draw_block(75, 0, -140, -190, '.')
draw_block(喵, '=')
# line_5
draw_block(-75, -150, -190, -240, '开平方')
draw_block(0, -75, -190, -240, '开立方')
draw_block(75, 0, -190, -240, '勾股定律', '求斜边')
draw_block(150, 75, -190, -240, '勾股定律', '求直角边')
def draw_bg_science():
# 绘制方程模式的界面
bg.clear()
sc.screensize(340, 500)
sc.setup(350, 510)
bg.pensize(5)
bg.up()
bg.goto(170, 275)
bg.down()
bg.fillcolor('#CCFFFF')
bg.begin_fill()
bg.goto(170, -275)
bg.goto(-170, -275)
bg.goto(-170, 275)
bg.goto(170, 275)
bg.end_fill()
bg.up()
bg.pensize(3)
bg.goto(-150, 235)
bg.down()
bg.goto(-130, 235)
bg.up()
bg.goto(-150, 225)
bg.down()
bg.goto(-130, 225)
bg.up()
bg.goto(-150, 215)
bg.down()
bg.goto(-130, 215)
bg.up()
bg.goto(-130, 230)
bg.pensize(5)
bg.goto(-100, 210)
bg.write('方程模式 by-蟒蛇喵', font=('微软雅黑', 16, 'bold'))
bg.goto(-160, 200)
bg.pensize(5)
bg.down()
bg.goto(160, 200)
bg.up()
bg.goto(160, 70)
bg.down()
bg.goto(-160, 70)
# line_-1
draw_block(-75, -150, 60, 10, '/','分数线')
draw_block(0, -75, 60, 10, '复位')
draw_block(75, 0, 60, 10, '退格')
draw_block(150, 75, 60, 10, 'x','未知数')
# line_1
draw_block(-75, -150, 10, -40, '(')
draw_block(0, -75, 10, -40, ')')
draw_block(75, 0, 10, -40, '=')
draw_block(150, 75, 10, -40, '÷')
# line_2
draw_block(-75, -150, -40, -90, '1')
draw_block(0, -75, -40, -90, '2')
draw_block(75, 0, -40, -90, '3')
draw_block(150, 75, -40, -90, '+')
# line_3
draw_block(-75, -150, -90, -140, '4')
draw_block(0, -75, -90, -140, '5')
draw_block(75, 0, -90, -140, '6')
draw_block(150, 75, -90, -140, '-')
# line_4
draw_block(-75, -150, -140, -190, '7')
draw_block(0, -75, -140, -190, '8')
draw_block(75, 0, -140, -190, '9')
draw_block(喵, '×')
# line_5
draw_block(-75, -150, -190, -240, 'Sympy','based')
draw_block(0, -75, -190, -240, '0')
draw_block(75, 0, -190, -240, '.')
draw_block(150, 75, -190, -240, '解')
draw_bg_normal()
def restart():
# 重置所有需要重置的变量
global firstNum, secondNum, currentState, computeSign
pen.clear()
pen_2.clear()
firstNum = ''
secondNum = ''
currentState = 'firstNum'
computeSign = ''
equalSignOfFormula = False
formulaEnter = False
# 定义所有需要的变量
firstNum = ''
secondNum = ''
currentState = 'firstNum'
computeSign = ''
lastOutPut = ''
equalSignOfFormula = False
formulaEnter = False
scienceOutPut = True
formulaOutPut = False
clickToSave = False
def main(get, state):
# get为功能的名字
# state是按钮类型,0代表普通数字,1为符号,2是得出结果,3是公式输入,4是方程输入,5是解方程
# 主循环,用于处理并反馈用户操作
global firstNum, secondNum, currentState, computeSign, lastOutPut, outPut, equalSignOfFormula, formulaEnter,\
scienceOutPut, formulaOutPut
if state == 0:
# 如果是输入普通数字
if currentState == 'firstNum':
# 如果当前输入的是第一个数
if len(firstNum) < 11:
# 如果位数少于11
pen.clear()
# 以下为特殊输入
if get == '-':
firstNum = '-'+firstNum
elif get == '.':
firstNum = firstNum+'.'
elif get == 'backspace':
# 退格,删除最后一个字符
if len(firstNum) >= 1:
firstNum = firstNum[:-1]
else:
pass
else:
# 如果不是特殊输入则正常拼接字符串
firstNum = firstNum+str(get)
if formulaEnter:
# 公式模式输入
pen.goto(150, 160)
pen.write(firstNum, align='right', font=('微软雅黑', 22, 'bold'))
if computeSign == '直角边':
pen.goto(150, 120)
pen.write('a²+b²=c² ', align='right', font=('微软雅黑', 28, 'bold'))
elif computeSign == '斜边':
pen.goto(150, 120)
pen.write('c²-b²=a² ', align='right', font=('微软雅黑', 28, 'bold'))
else:
pen.goto(150, 105)
pen.write(firstNum, align='right', font=('微软雅黑', 36, 'bold'))
else:
if get == 'backspace':# 退格
firstNum = firstNum[:-1]
pen.clear()
pen.goto(150, 105)
pen.write(firstNum, align='right', font=('微软雅黑', 36, 'bold'))
elif currentState == 'secondNum':
# 如果输入的是第二个数(以下基本同第一个数)
if len(secondNum) < 11:
pen_2.clear()
if get == '-':
secondNum = '-'+secondNum
elif get == '.':
secondNum = secondNum+'.'
elif get == 'backspace':
if len(secondNum) >= 1:
secondNum = secondNum[:-1]
else:
pass
else:
secondNum = secondNum+str(get)
if formulaEnter:
pen_2.goto(150, 75)
pen_2.write(secondNum, align='right', font=('微软雅黑', 36, 'bold'))
if computeSign == '直角边':
pen_2.goto(150, 120)
pen_2.write('a²+b²=c² ', align='right', font=('微软雅黑', 28, 'bold'))
elif computeSign == '斜边':
pen_2.goto(150, 120)
pen_2.write('c²-b²=a² ', align='right', font=('微软雅黑', 28, 'bold'))
else:
pen_2.goto(150, 75)
pen_2.write(secondNum, align='right', font=('微软雅黑', 36, 'bold'))
else:
if get == 'backspace':
firstNum = firstNum[:-1]
pen_2.clear()
pen_2.goto(150, 75)
pen_2.write(secondNum, align='right', font=('微软雅黑', 36, 'bold'))
else:
pass
elif state == 1:
# 如果用户点击了等于按钮
# 先确定用户的输入是否合法
if firstNum == '':
currentState = 'secondNum'
firstNum = lastOutPut
elif formulaEnter:
pass
else:
currentState = 'secondNum'
pen.clear()
pen_2.clear()
pen.goto(150, 115)
# 存储用户选择的计算符号
if get == '+':
pen.write(get+' ', align='right', font=('微软雅黑', 36, 'bold'))
computeSign = '+'
elif get == '-':
pen.write(get+' ', align='right', font=('微软雅黑', 36, 'bold'))
computeSign = '-'
elif get == 'X':
pen.write('×'+' ', align='right', font=('微软雅黑', 36, 'bold'))
computeSign = 'X'
elif get == '÷':
pen.write(get+' ', align='right', font=('微软雅黑', 36, 'bold'))
computeSign = '÷'
elif get == '^':
pen.goto(150, 120)
pen.write('x ^ y ', align='right',font=('微软雅黑', 28, 'bold'))
computeSign = '^'
firstNum = 'x: '+str(firstNum)
secondNum = 'y: '
# 如果是公式模式则特殊对待
elif get == '=': # 分割数据用等号
if currentState == 'firstNum':
currentState = 'secondNum'
if computeSign == '直角边':
pen.goto(150, 120)
pen.write('a²+b²=c² ', align='right', font=('微软雅黑', 28, 'bold'))
elif computeSign == '斜边':
pen.goto(150, 120)
pen.write('c²-b²=a² ', align='right', font=('微软雅黑', 28, 'bold'))
draw_block(喵, '=')
formulaEnter = True
equalSignOfFormula = False
elif currentState == 'secondNum':
main('=',2)
formulaEnter = False
equalSignOfFormula = False
else:
pass
pen.goto(150, 160)
pen.write(firstNum, align='right', font=('微软雅黑', 22, 'bold'))
pen_2.goto(150, 75)
pen_2.write(secondNum, align='right', font=('微软雅黑', 36, 'bold'))
elif state == 2:
# 计算
if firstNum == '' or secondNum == '':
# 对平方立方特殊照顾(后面再算)
if get == '开平方' or get == '开立方':
pass
else:
# 如果用户输入不合法则中止计算重新输入
return
else:
pass
formulaEnter = False
# 特殊对待
if get == '开平方':
outPut = str(round(pow(float(firstNum), 1/2),5))
elif get == '开立方':
outPut = str(round(pow(float(firstNum), 1/3), 5))
else:
# 计算
if computeSign == '+':
outPut = str(round(float(firstNum)+float(secondNum),5))
elif computeSign == '-':
outPut = str(round(float(firstNum)-float(secondNum),5))
elif computeSign == 'X':
outPut = str(round(float(firstNum)*float(secondNum),5))
elif computeSign == '÷':
outPut = str(round((float(firstNum)/float(secondNum)),5))
elif computeSign == '^':
firstNum = firstNum[3:len(firstNum)]
secondNum = secondNum[3:len(secondNum)]
outPut = str(round((pow(float(firstNum),float(secondNum))),5))
# 公式
elif computeSign == '斜边':
# 提取所需要的元素
firstNum = firstNum[3:len(firstNum)]
secondNum = secondNum[3:len(secondNum)]
# 计算,下同
outPut = str(round((pow((pow(float(firstNum),2)-pow(float(secondNum),2)),1/2)),5))
elif computeSign == '直角边':
firstNum = firstNum[3:len(firstNum)]
secondNum = secondNum[3:len(secondNum)]
outPut = str(round((pow(((pow((float(firstNum)),2))+(pow((float(secondNum)),2))),1/2)),2))
try:
outPutLst = outPut.split('.')
if outPutLst[1] == '0':
pass
else:
formulaOutPut = True
except:
pass
else:
return
# 显示结果
lst = list(outPut)
if len(lst) >= 13:
# 如果太长就显示“无法显示”
pen.clear()
pen_2.clear()
pen.goto(150, 120)
pen.write('结果溢出,无法显示', align='right', font=('微软雅黑', 22, 'bold'))
firstNum = ''
secondNum = ''
currentState = 'firstNum'
computeSign = ''
return
else:
# 如果在指定长度之内,就进入下一步显示结果
lastOutPut = outPut
pen.clear()
pen_2.clear()
if formulaOutPut:
# 如果是公式模式且不是正整数
pen.goto(150, 125)
pen.write(outPut, align='right', font=('微软雅黑', 32, 'bold'))
pen.goto(150, 105)
pen.write('⚠:结果不符合勾股定理概念(不是正整数)', align='right',
font=('微软雅黑', 12, 'bold'))
pen.goto(150, 85)
pen.write('结果已取两位小数供参考', align='right',font=('微软雅黑', 12, 'bold'))
formulaOutPut = False # 重置
else:
# 如果所有条件都符合,就显示结果
pen.goto(150, 105)
pen.write(outPut, align='right', font=('微软雅黑', 36, 'bold'))
firstNum = ''
secondNum = ''
currentState = 'firstNum'
computeSign = ''
elif state == 3: # 公式输入
# 公式模式获取输入
if get == '直角边':
pen.clear()
pen_2.clear()
currentState = 'firstNum'
equalSignOfFormula = True
formulaEnter = True
firstNum = 'a: '+str(firstNum)
secondNum = 'b: '
pen.goto(150, 160)
pen.write(firstNum, align='right', font=('微软雅黑', 22, 'bold'))
pen_2.goto(150, 75)
pen_2.write(secondNum, align='right', font=('微软雅黑', 36, 'bold'))
pen.goto(150, 120)
pen.write('a²+b²=c² ', align='right', font=('微软雅黑', 28, 'bold'))
computeSign = '直角边'
# 替换“=”为“切换下一个值”
draw_block(喵, '输入','第二个值')
elif get == '斜边':
pen.clear()
pen_2.clear()
currentState = 'firstNum'
equalSignOfFormula = True
formulaEnter = True
firstNum = 'c: '+str(firstNum)
secondNum = 'b: '
pen.goto(150, 160)
pen.write(firstNum, align='right', font=('微软雅黑', 22, 'bold'))
pen_2.goto(150, 75)
pen_2.write(secondNum, align='right', font=('微软雅黑', 36, 'bold'))
pen.goto(150, 120)
pen.write('c²-b²=a² ', align='right', font=('微软雅黑', 28, 'bold'))
computeSign = '斜边'
draw_block(喵, '输入', '第二个值')
elif state == 4: # 方程输入
# 这个简单,拼接字符串就行
if len(firstNum) <= 23:
pen.clear()
if get == 'backspace':
if len(firstNum) >= 1:
firstNum = firstNum[:-1]
else:
pass
elif get == '未知数':
firstNum = firstNum+'x'
elif get == 'X':
firstNum = firstNum+'×'
else:
firstNum = firstNum+str(get)
formula_write(firstNum)
else:
if get == 'backspace':
firstNum = firstNum[:-1]
pen.clear()
formula_write(firstNum)
elif state == 5: # 解方程
# 格式化用户输入,调用模块解方程
if firstNum == '':
return
inPutLst = list(firstNum)
firstNum = ''
for InPut in inPutLst:
firstNum = firstNum + InPut
try:
firstNum = firstNum.replace('^','**')
firstNum = firstNum.replace('×','*')
firstNum = firstNum.replace('÷','/')
scienceLst = firstNum.split('=')
input_1 = eval(scienceLst[0])
input_2 = eval(scienceLst[1])
outPut = str(s.solve(s.Eq(input_1,input_2)))
outPut = outPut.喵('[]')
outPutLst = list(outPut)
except:
return
while True:
if outPutLst[len(outPutLst)-1] == '0':
outPutLst.pop(len(outPutLst)-1)
elif outPutLst[len(outPutLst)-1] == '.':
outPutLst.append(0)
break
else:
break
if len(outPutLst) >= 13:
scienceOutPut = False
outPut = ''
for fill in outPutLst:
outPut = outPut+str(fill)
if not scienceOutPut:
try:
outPut = round(float(outPut),5)
except:
outPut = '以下为方程'+firstNum.replace('**','^').replace('*','×').replace('/','÷')+'的解:\n(特别说明:√下的数字会用括号括起来)\n'+outPut
outPut = outPut.replace(', ','\n')
pen.goto(150, 135)
pen.clear()
pen_2.clear()
pen.write('该方程有多个解或解是一个式子', align='right', font=('微软雅黑', 15, 'bold'))
pen.goto(150, 115)
pen.write('结果已保存至 方程结果.txt', align='right', font=('微软雅黑', 10, 'bold'))
f = open('方程结果.txt','w')
f.write(outPut.replace('**','^').replace('*','×').replace('sqrt','√'))
f.close()
scienceOutPut = True
if len(outPutLst) >= 13 and not scienceOutPut:
pen.goto(150, 125)
pen.clear()
pen_2.clear()
pen.write('结果溢出,无法显示', align='right', font=('微软雅黑', 22, 'bold'))
else:
pen.clear()
pen_2.clear()
pen.goto(150, 105)
pen.write('x='+outPut, align='right', font=('微软雅黑', 36, 'bold'))
def choose_block():
# 绘制切换模式的弹窗
click.up()
click.goto(-150, 235)
click.fillcolor('white')
click.pensize(5)
click.down()
click.begin_fill()
click.goto(-10, 235)
click.goto(-10, 75)
click.goto(-150, 75)
click.goto(-150, 235)
click.end_fill()
click.up()
click.goto(-80, 235)
click.down()
click.goto(-80, 75)
click.up()
click.goto(-150, 180)
click.down()
click.goto(-80, 180)
click.goto(-80, 125)
click.goto(-150, 125)
click.up()
click.goto(-115,143.75)
click.write('标准',align='center',font=('微软雅黑', 15, 'bold'))
click.goto(-115,200)
click.write('数学',align='center',font=('微软雅黑', 15, 'bold'))
click.goto(-115, 90)
click.write('方程', align='center', font=('微软雅黑', 15, 'bold'))
click.goto(-45,143.75)
click.write('关于',align='center',font=('微软雅黑', 15, 'bold'))
# 当前模式为 normal
shape = 'normal'
chooseBlock = 'off'
About = False
def getClickPos(x, y):
# 获取用户点击的位置,并调用主循环函数
global shape, chooseBlock, equalSignOfFormula, About
# 标准版按键
if equalSignOfFormula:
if currentState == 'firstNum':
pass
elif currentState == 'secondNum':
draw_block(喵, '=')
equalSignOfFormula = False
else:
pass
if (x > 125) and (x < 150) and (y > 175) and (y < 200) and About:
# 如果说点到了关闭关于页面的按钮且关于页面打开
About = False # 关闭关于页面
about.clear() # 清空
else:
pass
if (x > -80) and (x < -10) and (y > 75) and (y < 235) and chooseBlock == 'on':
# 如果点击了打开关于页面的按钮
if not About:
# 且关于页面没有显示
About = True # 打开关于页面
draw_about()
chooseBlock = 'off' # 关闭选项框
else:
click.clear()
else:
pass
if shape == 'normal' and About == False:
# 如果在普通模式打开了切换模式的窗口
if (x > -150) and (x < -135) and (y > 215) and (y < 235):
choose_block()
chooseBlock = 'on'
return
# 以下皆为切换模式的判断
elif (x > -150) and (x < -80) and (y > 180) and (y < 235) and chooseBlock == 'on':
if shape == 'normal' or shape == 'science':
shape = 'math'
draw_bg_math()
restart()
click.clear()
chooseBlock = 'off'
else:
click.clear()
elif (x > -150) and (x < -80) and (y > 125) and (y < 180) and chooseBlock == 'on':
if shape == 'math' or shape == 'science':
shape = 'normal'
draw_bg_normal()
restart()
click.clear()
chooseBlock = 'off'
else:
click.clear()
elif (x > -150) and (x < -80) and (y > 80) and (y < 135) and chooseBlock == 'on':
if shape == 'normal' or shape == 'math':
shape = 'science'
draw_bg_science()
restart()
click.clear()
chooseBlock = 'off'
else:
click.clear()
# 获取用户平常使用时按钮的点击事件
elif (x > -150) and (x < -75):
if (y > 10) and (y < 60):
main('-',0)
elif (y > -40) and (y < 10):
main(1,0)
elif (y > -90) and (y < -40):
main(4,0)
elif (y > -140) and (y < -90):
main(7, 0)
elif (y > -190) and (y < -140):
main('^', 1)
else:
click.clear()
elif (x > -75) and (x < 0):
if (y > 10) and (y < 60):
restart()
elif (y > -40) and (y < 10):
main(2, 0)
elif (y > -90) and (y < -40):
main(5, 0)
elif (y > -140) and (y < -90):
main(8, 0)
elif (y > -190) and (y < -140):
main(0, 0)
else:
click.clear()
elif (x > 0) and (x < 75):
if (y > 10) and (y < 60):
main('backspace',0)
elif (y > -40) and (y < 10):
main(3, 0)
elif (y > -90) and (y < -40):
main(6, 0)
elif (y > -140) and (y < -90):
main(9, 0)
elif (y > -190) and (y < -140):
main('.', 0)
else:
click.clear()
elif (x > 75) and (x < 150):
if (y > 10) and (y < 60):
main('÷',1)
elif (y > -40) and (y < 10):
main('+',1)
elif (y > -90) and (y < -40):
main('-',1)
elif (y > -140) and (y < -90):
main('X',1)
elif (y > -190) and (y < -140):
main('=', 2)
else:
click.clear()
else:
click.clear()
return
click.clear()
# 数学版按键
elif shape == 'math' and About == False:
if (x > -150) and (x < -135) and (y > 215) and (y < 235):
choose_block()
chooseBlock = 'on'
return
elif (x > -150) and (x < -80) and (y > 180) and (y < 235) and chooseBlock == 'on':
if shape == 'normal' or shape == 'science':
shape = 'math'
draw_bg_math()
restart()
click.clear()
chooseBlock = 'off'
else:
click.clear()
elif (x > -150) and (x < -80) and (y > 125) and (y < 180) and chooseBlock == 'on':
if shape == 'math' or shape == 'science':
shape = 'normal'
draw_bg_normal()
restart()
click.clear()
chooseBlock = 'off'
else:
click.clear()
elif (x > -150) and (x < -80) and (y > 80) and (y < 135) and chooseBlock == 'on':
if shape == 'normal' or shape == 'math':
shape = 'science'
draw_bg_science()
restart()
click.clear()
chooseBlock = 'off'
else:
click.clear()
elif (x > -150) and (x < -75):
if (y > 10) and (y < 60):
main('-',0)
elif (y > -40) and (y < 10):
main(1,0)
elif (y > -90) and (y < -40):
main(4,0)
elif (y > -140) and (y < -90):
main(7, 0)
elif (y > -190) and (y < -140):
main('^', 1)
elif (y > -240) and (y < -190):
main('开平方', 2)
else:
click.clear()
elif (x > -75) and (x < 0):
if (y > 10) and (y < 60):
restart()
elif (y > -40) and (y < 10):
main(2, 0)
elif (y > -90) and (y < -40):
main(5, 0)
elif (y > -140) and (y < -90):
main(8, 0)
elif (y > -190) and (y < -140):
main(0, 0)
elif (y > -240) and (y < -190):
main('开立方', 2)
else:
click.clear()
elif (x > 0) and (x < 75):
if (y > 10) and (y < 60):
main('backspace',0)
elif (y > -40) and (y < 10):
main(3, 0)
elif (y > -90) and (y < -40):
main(6, 0)
elif (y > -140) and (y < -90):
main(9, 0)
elif (y > -190) and (y < -140):
main('.', 0)
elif (y > -240) and (y < -190):
main('直角边', 3)
else:
click.clear()
elif (x > 75) and (x < 150):
if (y > 10) and (y < 60):
main('÷',1)
elif (y > -40) and (y < 10):
main('+',1)
elif (y > -90) and (y < -40):
main('-',1)
elif (y > -140) and (y < -90):
main('X',1)
elif (y > -190) and (y < -140):
if not equalSignOfFormula:
main('=', 2)
elif equalSignOfFormula:
main('=', 1) # 公式分割数据用
elif (y > -240) and (y < -190):
main('斜边', 3)
else:
click.clear()
# 方程版按键
elif shape == 'science' and About == False:
if (x > -150) and (x < -135) and (y > 215) and (y < 235):
choose_block()
chooseBlock = 'on'
return
elif (x > -150) and (x < -80) and (y > 180) and (y < 235) and chooseBlock == 'on':
if shape == 'normal' or shape == 'science':
shape = 'math'
draw_bg_math()
restart()
click.clear()
chooseBlock = 'off'
else:
click.clear()
elif (x > -150) and (x < -80) and (y > 125) and (y < 180) and chooseBlock == 'on':
if shape == 'math' or shape == 'science':
shape = 'normal'
draw_bg_normal()
restart()
click.clear()
chooseBlock = 'off'
else:
click.clear()
elif (x > -150) and (x < -80) and (y > 80) and (y < 135) and chooseBlock == 'on':
if shape == 'normal' or shape == 'math':
shape = 'science'
draw_bg_science()
restart()
click.clear()
chooseBlock = 'off'
else:
click.clear()
elif (x > -150) and (x < -75):
if (y > 10) and (y < 60):
main('/', 4)
elif (y > -40) and (y < 10):
main('(', 4)
elif (y > -90) and (y < -40):
main(1, 4)
elif (y > -140) and (y < -90):
main(4, 4)
elif (y > -190) and (y < -140):
main(7, 4)
elif (y > -240) and (y < -190):
main('', 6)
else:
click.clear()
elif (x > -75) and (x < 0):
if (y > 10) and (y < 60):
restart()
elif (y > -40) and (y < 10):
main(')', 4)
elif (y > -90) and (y < -40):
main(2, 4)
elif (y > -140) and (y < -90):
main(5, 4)
elif (y > -190) and (y < -140):
main(8, 4)
elif (y > -240) and (y < -190):
main(0, 4)
else:
click.clear()
elif (x > 0) and (x < 75):
if (y > 10) and (y < 60):
main('backspace', 4)
elif (y > -40) and (y < 10):
main('=', 4)
elif (y > -90) and (y < -40):
main(3, 4)
elif (y > -140) and (y < -90):
main(6, 4)
elif (y > -190) and (y < -140):
main(9, 4)
elif (y > -240) and (y < -190):
main('.', 4)
else:
click.clear()
elif (x > 75) and (x < 150):
if (y > 10) and (y < 60):
main('未知数', 4)
elif (y > -40) and (y < 10):
main('÷', 4)
elif (y > -90) and (y < -40):
main('+', 4)
elif (y > -140) and (y < -90):
main('-', 4)
elif (y > -190) and (y < -140):
main('X', 4)
elif (y > -240) and (y < -190):
main('=', 5)
else:
click.clear()
else:
click.clear()
return
click.clear()
def check(x, y):
print(x, y)
sc.onscreenclick(getClickPos)
# sc.onscreenclick(check)
t.done()