基于python的计算器制作

简单计算器制作

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
  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Python 是一种广泛使用的编程语言,非常适合用于计算器制作。为了制作一个 Python 计算器,你需要学习 Python 的语法和基本知识。 首先,你需要理解 Python 的基本数据类型和运算符。这包括数字、字符串和布尔类型,以及加、减、乘、除等基本的算术运算符。你还需要掌握 Python 的条件语句和循环语句,以便编写一个可以接受用户输入并执行不同操作的程序。当你掌握了这些基本知识后,你就可以开始着手编写你自己的 Python 计算器了。 在编写计算器时,你需要使用 Python 的输入和输出函数来接收用户输入并将结果输出到屏幕上。你还需要创建不同的函数来执行各种操作,例如加、减、乘、除等。你可以使用流程控制语句和条件语句来指定你的程序应该执行哪个函数。 最后,你需要将你的计算器发布为一个完整的应用程序,可以在用户的终端上运行。要做到这一点,你需要打包你的程序并将其发布到合适的平台。你还需要测试你的程序,确保它可以正确地处理各种数据类型和操作符,并且输出正确的结果。 总之, Python 计算器制作并不难,只要掌握了 Python 的基本知识和技能,就可以编写一个完全由自己制作计算器。它可以用来执行各种操作,也可以扩展到其他功能,例如图形界面和计算历史记录。 ### 回答2: Python是一种流行的编程语言,因其简单易学,且可以应用于许多领域,Python也成为了许多初学者选择的编程语言。这里介绍一种Python计算器制作方法。 首先,我们需要了解GUI编程,Python有许多GUI库可以使用,比如Tkinter,PyQt等。这里我们以Tkinter为例。 在Tkinter中,我们可以创建各种控件,例如文本框、按钮、标签等。我们也可以指定这些控件的位置,并为其设置某些属性。例如,对于计算器制作来说,我们需要创建数值输入框和运算符按钮,并在点击按钮后实现计算的逻辑。 具体实现步骤如下: 1. 导入Tkinter库。 ``` import tkinter as tk ``` 2. 创建主窗口。 ``` root = tk.Tk() ``` 3. 添加菜单栏及子项。 ``` menubar = tk.Menu(root) filemenu = tk.Menu(menubar, tearoff=0) filemenu.add_command(label="Exit", command=root.quit) menubar.add_cascade(label="File", menu=filemenu) root.config(menu=menubar) ``` 4. 创建输入框。 ``` e = tk.Entry(root, font=('Arial', 20), width=25, bd=5) e.grid(row=0, column=0, columnspan=4, pady=10, padx=5) ``` 5. 创建按钮。 ``` b1 = tk.Button(root, text='1', font=('Arial', 15), width=5, command=lambda: append('1')) b1.grid(row=1, column=0, pady=5, padx=5) b2 = tk.Button(root, text='2', font=('Arial', 15), width=5, command=lambda: append('2')) b2.grid(row=1, column=1, pady=5, padx=5) ... ``` 6. 添加操作方法。例如,加减乘除四个运算操作。 ``` def add(): global operator operator = '+' first_num = float(e.get()) e.delete(0, tk.END) return first_num ... def operate(): global operator second_num = float(e.get()) if operator == '+': result = first_num + second_num elif operator == '-': result = first_num - second_num elif operator == '*': result = first_num * second_num elif operator == '/': result = first_num / second_num e.delete(0, tk.END) e.insert(0, result) ... ``` 7. 根据布局,将按钮绑定到相应的方法。 ``` add_button = tk.Button(root, text="+", font=('Arial', 15), width=5, command=add) add_button.grid(row=4, column=3, pady=5, padx=5) equal_button = tk.Button(root, text="=", font=('Arial', 15), width=5, command=operate) equal_button.grid(row=5, column=3, pady=5, padx=5) ``` 8. 执行主程序。 ``` root.mainloop() ``` 通过以上操作,我们便可以实现一个简单的Python计算器啦!总之,Python开发计算器需要掌握GUI编程、函数调用、参数传递等知识,希望以上操作可以帮助您快速入门编写Python计算器。 ### 回答3: Python编程语言的易读性、易学性以及其强大的数学计算能力,使其成为了制作计算器的一种理想语言。以下是一个使用Python实现的简单计算器: 首先,需要使用GUI库来制作窗口界面。Python中常用的GUI库有Tkinter、wxPython、PyQt等等。在本例中使用的是Tkinter。使用Tkinter可以快速制作出一个简易的计算器界面,代码如下: ```python from tkinter import * class Calculator: def __init__(self, master): self.master = master master.title('Calculator') self.display = Entry(master, width=30, borderwidth=5) self.display.grid(row=0, column=0, columnspan=4, padx=10, pady=10) # 按钮创建 self.button_1 = Button(master, text='1', padx=40, pady=20, command=lambda: self.button_click(1)) self.button_2 = Button(master, text='2', padx=40, pady=20, command=lambda: self.button_click(2)) self.button_3 = Button(master, text='3', padx=40, pady=20, command=lambda: self.button_click(3)) self.button_4 = Button(master, text='4', padx=40, pady=20, command=lambda: self.button_click(4)) self.button_5 = Button(master, text='5', padx=40, pady=20, command=lambda: self.button_click(5)) self.button_6 = Button(master, text='6', padx=40, pady=20, command=lambda: self.button_click(6)) self.button_7 = Button(master, text='7', padx=40, pady=20, command=lambda: self.button_click(7)) self.button_8 = Button(master, text='8', padx=40, pady=20, command=lambda: self.button_click(8)) self.button_9 = Button(master, text='9', padx=40, pady=20, command=lambda: self.button_click(9)) self.button_0 = Button(master, text='0', padx=40, pady=20, command=lambda: self.button_click(0)) self.button_add = Button(master, text='+', padx=39, pady=20, command=lambda: self.button_operation('+')) self.button_subtract = Button(master, text='-', padx=41, pady=20, command=lambda: self.button_operation('-')) self.button_multiply = Button(master, text='*', padx=40, pady=20, command=lambda: self.button_operation('*')) self.button_divide = Button(master, text='/', padx=41, pady=20, command=lambda: self.button_operation('/')) self.button_clear = Button(master, text='Clear', padx=79, pady=20, command=self.button_clear) self.button_equal = Button(master, text='=', padx=91, pady=20, command=self.button_equal) # 按钮布局 self.button_1.grid(row=3, column=0) self.button_2.grid(row=3, column=1) self.button_3.grid(row=3, column=2) self.button_4.grid(row=2, column=0) self.button_5.grid(row=2, column=1) self.button_6.grid(row=2, column=2) self.button_7.grid(row=1, column=0) self.button_8.grid(row=1, column=1) self.button_9.grid(row=1, column=2) self.button_0.grid(row=4, column=0) self.button_clear.grid(row=4, column=1, columnspan=2) self.button_add.grid(row=5, column=0) self.button_subtract.grid(row=6, column=0) self.button_multiply.grid(row=6, column=1) self.button_divide.grid(row=6, column=2) self.button_equal.grid(row=5, column=1, columnspan=2) self.operation = '' self.first_number = 0 def button_click(self, number): current = self.display.get() self.display.delete(0, END) self.display.insert(0, str(current) + str(number)) def button_clear(self): self.display.delete(0, END) self.operation = '' self.first_number = 0 def button_operation(self, operation): self.first_number = float(self.display.get()) self.operation = operation self.display.delete(0, END) def button_equal(self): second_number = float(self.display.get()) self.display.delete(0, END) if self.operation == '+': self.display.insert(0, self.first_number + second_number) elif self.operation == '-': self.display.insert(0, self.first_number - second_number) elif self.operation == '*': self.display.insert(0, self.first_number * second_number) elif self.operation == '/': self.display.insert(0, self.first_number / second_number) root = Tk() app = Calculator(root) root.mainloop() ``` 这段代码使用了Tkinter的布局方法实现了一个基本的计算器界面,并对按键注册了相应的事件处理函数。当按下数字键时,则调用```button_click```方法,并将数字附加在当前显示值的后面;当按下运算符键时,则调用```button_operation```方法,并将当前显示值转化为浮点型数值。当按下等于号时,则调用```button_equal```方法,对第一次输入和第二次输入的数字进行相应的运算,并将结果显示在屏幕上。同时,也在```button_clear```方法中编写重置所有显示参数的功能。最后使用```root.mainloop```方法启动了计算器。 总之,Python是一种非常适合制作计算器的编程语言,通过编写合适的计算逻辑,再使用Python的GUI库创建计算器界面,即可快速实现一个简单的计算器

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值