欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

基于python的计算器制作

程序员文章站 2024-02-27 13:02:21
...

简单计算器制作

import tkinter as tk
from decimal import Decimal
import math
top=tk.Tk()
top.title("我是一个计算器")
top.geometry("350x400+150+150")
n=0
#实现单击数字对应出现在文本框中
def nclick():
    global n
    n=n+1
    text=bt['text']
    text1.insert(n-1,text)
def n1click():
    global n
    n=n+1
    text=bt1['text']
    text1.insert(n-1,text)
def n2click():
    global n
    n=n+1
    text=bt2['text']
    text1.insert(n-1,text)
def n3click():
    global n
    n=n+1
    text=bt3['text']
    text1.insert(n-1,text)
def n4click():
    global n
    n=n+1
    text=bt4['text']
    text1.insert(n-1,text)
def n5click():
    global n
    n=n+1
    text=bt5['text']
    text1.insert(n-1,text)
def n6click():
    global n
    n=n+1
    text=bt6['text']
    text1.insert(n-1,text)
def n7click():
    global n
    n=n+1
    text=bt7['text']
    text1.insert(n-1,text)
def n8click():
    global n
    n=n+1
    text=bt8['text']
    text1.insert(n-1,text)
def n14click():
    global n
    n=n+1
    text=bt14['text']
    text1.insert(n-1,text)
#实现单击符号对应出现在文本框中
def n9click():
    global n
    n=n+1
    text=bt9['text']
    text1.insert(n-1,text)
def n10click():
    global n
    n=n+1
    text=bt10['text']
    text1.insert(n-1,text)
def n11click():
    global n
    n=n+1
    text=bt11['text']
    text1.insert(n-1,text)
def n12click():
    global n
    n=n+1
    text=bt12['text']
    text1.insert(n-1,text)
def n13click():
    global n
    n=n+1
    text=bt13['text']
    text1.insert(n-1,text)
#完成计算的函数
def n15click():
    text=text1.get()
    for i in range(len(text)):
        if(text[i]=='.'):
            for i in range(len(text)):
                if(text[i]=='+'):
                    p=Decimal(text[0:i:])
                    try:
                        q=Decimal(text[i+1::])
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p+q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                if(text[i]=='-'):
                    p=Decimal(text[0:i:])
                    try:
                        q=Decimal(text[i+1::])
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p-q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                if(text[i]=='*'):
                    p=Decimal(text[0:i:])
                    try:
                        q=Decimal(text[i+1::])
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p*q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                if(text[i]=='/'):
                    p=Decimal(text[0:i:])
                    try:
                        q=Decimal(text[i+1::])
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p/q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                if(text[i]=='^'):
                    p=Decimal(text[0:i:])
                    try:
                        q=Decimal(text[i+1::])
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p**q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                if(text[i]=='S'):
                    if(i>0):
                        p=Decimal(text[0:i:])
                        result=math.sqrt(p)
                    else:
                        try:
                            p=Decimal(text[i+1::])
                        except:
                            text1.delete(0,tk.END)
                            text1.insert(0,"负数没有平方根")
                        result=math.sqrt(p)
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                if(text[i]=='P'):
                    if(i>0):
                        p=Decimal(text[0:i:])
                        q=Decimal(math.pi)
                        result=p*q
                    else:
                        try:
                            p=Decimal(text[i+1::])
                            q=Decimal(math.pi)
                        except:
                            text1.delete(0,tk.END)
                            text1.insert(0,"error")
                        result=p*q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
            break  
        elif(text[i]=='+'):
            for j in range(len(text)):
                if(text[j]=='.'):
                    p=Decimal(text[0:i])
                    try:
                        q=Decimal(text[i+1])
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p+q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                else:
                    p=eval(text[0:i])
                    try:
                        q=eval(text[i+1])
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p+q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                break
        elif(text[i]=='-'):
            for j in range(len(text)):
                if(text[j]=='.'):
                    if(i==0):
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    p=Decimal(text[0:i])
                    try:
                        q=Decimal(text[i+1])
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p-q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                else:
                    if(i==0):
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    p=eval(text[0:i])
                    try:
                        q=eval(text[i+1])
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p-q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)         
                break
        elif(text[i]=='*'):
            for j in range(len(text)):
                if(text[j]=='.'):
                    p=Decimal(text[0:i])
                    try:
                        q=Decimal(text[i+1])
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p*q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                else:
                    p=eval(text[0:i])
                    try:
                        q=eval(text[i+1])
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p*q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                break
        elif(text[i]=='/'):
            for j in range(len(text)):
                if(text[j]=='.'):
                    p=decimal(text[0:i:])
                    try:                        
                        q=decimal(text[i+1::])
                        if(q==0):
                            text1.delete(0,tk.END)
                            text1.insert(0,"除数不能为0")
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p/q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                else:
                    p=eval(text[0:i:])
                    try:
                        q=eval(text[i+1::])
                        if(q==0):
                            text1.delete(0,tk.END)
                            text1.insert(0,"除数不能为0")
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p/q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                break
        elif(text[i]=='^'):
            for j in range(len(text)):
                if(text[j]=='.'):
                    p=Decimal(text[0:i])
                    try:
                        q=Decimal(text[i+1])
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p**q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                else:
                    p=eval(text[0:i])
                    try:
                        q=eval(text[i+1])
                    except:
                        text1.delete(0,tk.END)
                        text1.insert(0,"error")
                    result=p**q
                    text1.delete(0,tk.END)
                    text1.insert(0,result)
                break
        elif(text[i]=='S'):
            if(i>0):
                p=eval(text[0:i])
                result=math.sqrt(p)
            else:
                try:
                    p=eval(text[i+1])
                except:
                    text1.delete(0,tk.END)
                    text1.insert(0,"负数没有平方根")
                result=math.sqrt(p)
            text1.delete(0,tk.END)
            text1.insert(0,result)
            break
        elif(text[i]=='P'):
            if(i>0):
                p=eval(text[0:i])
                result=p*math.pi
            else:
                try:
                    p=eval(text[i+1])
                except:
                    text1.delete(0,tk.END)
                    text1.insert(0,"error")
                result=p*math.pi
            text1.delete(0,tk.END)
            text1.insert(0,result)
            break
                       
def n16click():
    text1.delete(0,tk.END)
def n17click():
    global n
    n=n+1
    text=bt17['text']
    text1.insert(n-1,text)
def n18click():
    global n
    n=n+1
    text=bt18['text']
    text1.insert(n-1,text)
def n19click():
    global n
    n=n+1
    text=bt19['text']
    text1.insert(n-1,text)
#bd用来设置文本框的边框大小
#输入框中选中文本,默认会复制到粘贴板exportselection=0则忽略这个功能
text1=tk.Entry(top,bd=5,exportselection=0,fg='green',width=28)
bt=tk.Button(top,text="7",width=6,command=nclick,relief="groove")
bt1=tk.Button(top,text="8",width=6,command=n1click,relief="groove")
bt2=tk.Button(top,text="9",width=6,command=n2click,relief="groove")
bt3=tk.Button(top,text="4",width=6,command=n3click,relief="groove")
bt4=tk.Button(top,text="5",width=6,command=n4click,relief="groove")
bt5=tk.Button(top,text="6",width=6,command=n5click,relief="groove")
bt6=tk.Button(top,text="1",width=6,command=n6click,relief="groove")
bt7=tk.Button(top,text="2",width=6,command=n7click,relief="groove")
bt8=tk.Button(top,text="3",width=6,command=n8click,relief="groove")
bt9=tk.Button(top,text="+",width=6,command=n9click,activebackground="#a2d7ff",fg="#f37d56")
bt10=tk.Button(top,text="-",width=6,command=n10click,activebackground="#a2d7ff",fg="#f37d56")
bt11=tk.Button(top,text="*",width=6,command=n11click,activebackground="#a2d7ff",fg="#f37d56")
bt12=tk.Button(top,text="/",width=6,command=n12click,activebackground="#a2d7ff",fg="#f37d56")
bt13=tk.Button(top,text=".",width=6,command=n13click,relief="groove",fg="blue")
bt14=tk.Button(top,text="0",width=6,command=n14click,relief="groove")
bt15=tk.Button(top,text="=",width=6,command=n15click,fg="blue",relief="groove")
bt16=tk.Button(top,text="clear",width=6,command=n16click,fg="red")
#P代表圆周率
bt17=tk.Button(top,text="P",width=6,command=n17click,activebackground="#a2d7ff",fg="#f37d56")
#S代表开方
bt18=tk.Button(top,text="S",width=6,command=n18click,activebackground="#a2d7ff",fg="#f37d56")
bt19=tk.Button(top,text="^",width=6,command=n19click,activebackground="#a2d7ff",fg="#f37d56")
#注示信息的存放
l1=tk.Label(top,text="P:圆周率",fg="gray")
l2=tk.Label(top,text="S:表开方",fg="gray")
#文本框的放置
text1.grid(rowspan=1,columnspan=4)
#第一排数字的放置
bt.grid(row=1,column=0)
bt1.grid(row=1,column=1)
bt2.grid(row=1,column=2)
bt9.grid(row=1,column=3)
#第二排数字的放置
bt3.grid(row=2,column=0)
bt4.grid(row=2,column=1)
bt5.grid(row=2,column=2)
bt10.grid(row=2,column=3)
#第三排数字的放置
bt6.grid(row=3,column=0)
bt7.grid(row=3,column=1)
bt8.grid(row=3,column=2)
bt11.grid(row=3,column=3)
#第四排数字的放置
bt13.grid(row=4,column=0)
bt14.grid(row=4,column=1)
bt15.grid(row=4,column=2)
bt12.grid(row=4,column=3)
#删除键的放置
bt16.grid(row=5,column=0)
bt17.grid(row=5,column=1)
bt18.grid(row=5,column=2)
bt19.grid(row=5,column=3)
#注示信息的放置
l1.grid(row=6)
l2.grid(row=7)

top.mainloop()

#界面如下图所示:
基于python的计算器制作