在python图形中计算文本中的sqrt

2024-09-20 03:59:11 发布

您现在位置:Python中文网/ 问答频道 /正文

因为代码很长,我把代码放在这个链接中:http://textuploader.com/5xofp

以下是小代码:

from graphics import *
from math import *
def main():
win = GraphWin('Simple Calculator',400,600)
win.setBackground('slategray')
p1 = Point(10,70)
p2 = Point(390,10)
display_box = Rectangle(p1,p2)
display_box.draw(win)
text = Text(Point(190,30),"")
text.setStyle('italic')
text.setSize(15)
text.draw(win)

sqrt = Rectangle(Point(70,190),Point(120,140))
sqrt.draw(win)
sqrt1 = Text(Point(95,165),"√ ")
sqrt1.setStyle('italic')
sqrt1.setSize(36)
sqrt1.draw(win)

eq_sign = Rectangle(Point(170,510),Point(240,440))
eq_sign.draw(win)
eq = Text(Point(205,475),"=")
eq.setStyle('italic')
eq.setSize(36)
eq.draw(win)

num1 = Rectangle(Point(10,270),Point(80,200))
num1.draw(win)
num1_ = Text(Point(45,235),"1")
num1_.setStyle('italic')
num1_.setSize(36)
num1_.draw(win)

if 10<=mouse.x<=80 and 200<=mouse.y<=270:
        text.setText(text.getText()+"1")
if 70<=mouse.x<=120 and 140<=mouse.y<=190:
        text.setText(text.getText()+"sqrt(")
if 170<=mouse.x<=240 and 440<=mouse.y<=510:#=
        try:
            result = eval(text.getText())
        except:
            result = "ERROR"
        text.setText(result)
main()

为什么当我输入sqrt(36)时,它会在文本中给出错误?我已经把它做成一根绳子了。 我的密码有错吗?你知道吗


Tags: 代码textsqrtwinpointeqdrawitalic
1条回答
网友
1楼 · 发布于 2024-09-20 03:59:11
sqrt = Rectangle(Point(70,190),Point(120,140))
sqrt.draw(win)

这将用矩形对象覆盖数学模块的sqrt函数。尝试将此变量的名称更改为其他名称。你知道吗

sqrt_button = Rectangle(Point(70,190),Point(120,140))
sqrt_button.draw(win)

相关问题 更多 >