简体   繁体   English

在计算器上工作,但我不知道如何添加 pi

[英]Working on a calculator but I can't figure out how to add pi

I cannot figure out how to add a pi button on my python calculator that actually works.我不知道如何在我的 python 计算器上添加一个实际工作的 pi 按钮。 I got the button ready so it is the pi symbol and can display the symbol when i click it but it doesn't work.我准备好了按钮,所以它是 pi 符号,当我单击它时可以显示该符号,但它不起作用。

this is the code:这是代码:

from tkinter import *
import math 
math.pi  

class calc: 

    def getandreplace(self): 

        """replace x with * and ÷ with /"""
        self.expression = self.e.get() 
        self.newtext=self.expression.replace('/','/') 
        self.newtext=self.newtext.replace('x','*') 


    def equals(self): 
        """when the equal button is pressed"""
        self.getandreplace() 
        try: 

            self.value= eval(self.newtext)  
        except SyntaxError or NameError: 
            self.e.delete(0,END) 
            self.e.insert(0,'Invalid Input!') 
        else: 
            self.e.delete(0,END) 
            self.e.insert(0,self.value) 

    def squareroot(self): 
        """squareroot method"""
        self.getandreplace() 
        try: 

            self.value= eval(self.newtext)  
        except SyntaxError or NameError: 
            self.e.delete(0,END) 
            self.e.insert(0,'Invalid Input!') 
        else: 
            self.sqrtval=math.sqrt(self.value) 
            self.e.delete(0,END) 
            self.e.insert(0,self.sqrtval) 

    def square(self): 
        """square method"""
        self.getandreplace() 
        try: 

            self.value= eval(self.newtext)  
        except SyntaxError or NameError: 
            self.e.delete(0,END) 
            self.e.insert(0,'Invalid Input!') 
        else: 
            self.sqval=math.pow(self.value,2) 
            self.e.delete(0,END) 
            self.e.insert(0,self.sqval) 

    def clearall(self): 
            """when clear button is pressed,clears the text input area"""
            self.e.delete(0,END) 

    def clear1(self): 
            self.txt=self.e.get()[:-1] 
            self.e.delete(0,END) 
            self.e.insert(0,self.txt) 

    def action(self,argi): 
            """pressed button's value is inserted into the end of the text area"""
            self.e.insert(END,argi) 

    def __init__(self,master): 
            """Constructor method"""
            master.title('Calulator') 
            master.geometry() 
            self.e = Entry(master) 
            self.e.grid(row=0,column=0,columnspan=6,pady=3) 
            self.e.focus_set() 


            Button(master,text="=",width=11,height=3,fg="red", 
                   bg="light green",command=lambda:self.equals()).grid( 
                                     row=4, column=4,columnspan=2) 

            Button(master,text='AC',width=5,height=3, 
                          fg="red", bg="light green", 
             command=lambda:self.clearall()).grid(row=1, column=4) 

            Button(master,text='C',width=5,height=3, 
                   fg="red",bg="light green", 
                   command=lambda:self.clear1()).grid(row=1, column=5) 

            Button(master,text="+",width=5,height=3, 
                   fg="blue",bg="orange", 
                   command=lambda:self.action('+')).grid(row=4, column=3) 

            Button(master,text="x",width=5,height=3, 
                    fg="blue",bg="orange", 
                    command=lambda:self.action('x')).grid(row=2, column=3) 

            Button(master,text="-",width=5,height=3, 
                    fg="blue",bg="orange", 
                    command=lambda:self.action('-')).grid(row=3, column=3) 

            Button(master,text="÷",width=5,height=3, 
                   fg="blue",bg="orange", 
                   command=lambda:self.action('/')).grid(row=1, column=3) 

            Button(master,text="π",width=5,height=3, 
                   fg="blue",bg="orange", 
                   command=lambda:self.action('π')).grid(row=4, column=2) 

            Button(master,text="7",width=5,height=3, 
                   fg="white",bg="blue", 
                   command=lambda:self.action(7)).grid(row=1, column=0) 

            Button(master,text="8",width=5,height=3, 
                   fg="white",bg="blue",  
                   command=lambda:self.action(8)).grid(row=1, column=1) 

            Button(master,text="9",width=5,height=3, 
                   fg="white",bg="blue",   
                   command=lambda:self.action(9)).grid(row=1, column=2) 

            Button(master,text="4",width=5,height=3, 
                   fg="white",bg="blue", 
                   command=lambda:self.action(4)).grid(row=2, column=0) 

            Button(master,text="5",width=5,height=3, 
                   fg="white",bg="blue", 
                   command=lambda:self.action(5)).grid(row=2, column=1) 

            Button(master,text="6",width=5,height=3, 
                   fg="white",bg="blue",  
                   command=lambda:self.action(6)).grid(row=2, column=2) 

            Button(master,text="1",width=5,height=3, 
                   fg="white",bg="blue", 
                   command=lambda:self.action(1)).grid(row=3, column=0) 

            Button(master,text="2",width=5,height=3, 
                   fg="white",bg="blue", 
                   command=lambda:self.action(2)).grid(row=3, column=1) 

            Button(master,text="3",width=5,height=3, 
                   fg="white",bg="blue", 
                   command=lambda:self.action(3)).grid(row=3, column=2) 

            Button(master,text="0",width=5,height=3, 
                   fg="white",bg="blue", 
                   command=lambda:self.action(0)).grid(row=4, column=0) 

            Button(master,text=".",width=5,height=3, 
                   fg="blue",bg="orange",  
                   command=lambda:self.action('.')).grid(row=4, column=1) 

            Button(master,text="(",width=5,height=3, 
                   fg="red",bg="light green", 
                   command=lambda:self.action('(')).grid(row=2, column=4) 

            Button(master,text=")",width=5,height=3, 
                   fg="red",bg="light green", 
                   command=lambda:self.action(')')).grid(row=2, column=5) 

            Button(master,text="SQRT",width=5,height=3, 
                   fg="red",bg="light green", 
                   command=lambda:self.squareroot()).grid(row=3, column=4) 

            Button(master,text="x²",width=5,height=3, 
                   fg="red",bg="light green",
                   command=lambda:self.square()).grid(row=3, column=5) 


root = Tk() 

obj=calc(root)

root.mainloop() 

You are just inserting the symbol pi ('π'), you need to change the button part to insert the value (math.pi or numpy.pi) like this (You will need to import numpy):您只是插入符号 pi ('π'),您需要更改按钮部分以插入值(math.pi 或 numpy.pi),如下所示(您需要导入 numpy):

Button(master,text="π",width=5,height=3,
    fg="blue",bg="orange",
    command=lambda:self.action(numpy.pi)).grid(row=4, column=2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我不知道这个房间计算器 - I can't figure out this room calculator 这个自定义计算器的Python代码现在可以正常工作了,我把它弄坏了,无法弄清楚哪里出了问题 - This Python code for a Custom Calculator was working now I broke it and I can't figure out where I went wrong 单行计算器,无法确定最后一个功能 - Single line calculator, can't figure out last function Pygame 游戏尝试不起作用,我不知道为什么 - Pygame game attempt not working and i can't figure out why Discord Py 不知道如何添加角色 - Discord Py can't figure out how to add roles 我刚刚开始在 Python 中制作一个基本的交互式计算器,但无法弄清楚为什么它不接受给定的输入 - I have just started making a basic interactive calculator in Python, but can't figure out why it won't accept the given input 为包括加班费的工资计算器编写学校项目。 无法弄清楚我在这方面做错了什么 - Writing a school project for a pay calculator that includes overtime. Can't figure out what I am doing wrong on this 无法弄清楚为什么 PyDub 不起作用 - Can't figure out why PyDub is not working 我不知道如何在 PyCharm 中安装 pandas - I can't figure out how to install pandas in PyCharm 我不知道如何在 Python 中获得正确的 output - I can't figure out how to get the right output in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM