基于python的计算器制作

本文介绍了如何使用Python语言创建一个简单的计算器。通过实现基本的加、减、乘、除功能,构建一个具备图形用户界面的计算器应用。
摘要由CSDN通过智能技术生成

简单计算器制作

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值