python 实现一个简单的计算器
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""
@Author: zh
@Time 2023/12/2 下午13:01 .
@Email:
@Describe:
"""
import tkinter as tk
# 创建计算器窗口
root = tk.Tk()
root.title("计算器")
# root.geometry("800x800+100+100")
input = tk.Entry(root, width=50, borderwidth=4)
input.grid(row=0, column=0, columnspan=3, padx=20, pady=20)
def btnClick(number):
current = input.get()
input.delete(0, tk.END)
input.insert(0, str(current) + str(number))
def clear():
input.delete(0, tk.END)
def add():
current = input.get()
global first_num
global math
math = "add"
first_num = float(current)
clear()
def subtract():
current = input.get()
global first_num
global math
math = "subtract"
first_num = float(current)
clear()
def multiply():
current = input.get()
global first_num
global math
math = "multiply"
first_num = float(current)
clear()
def divide():
current = input.get()
global first_num
global math
math = "divide"
first_num = float(current)
clear()
def equals():
second_number = input.get()
clear()
if math == "add":
input.insert(0, first_num + float(second_number))
elif math == "subtract":
input.insert(0, first_num - float(second_number))
elif math == "multiply":
input.insert(0, first_num * float(second_number))
elif math == "divide":
input.insert(0, first_num / float(second_number))
btn1 = tk.Button(root, padx=40, pady=20, text="1", command=lambda: btnClick(1))
btn2 = tk.Button(root, padx=40, pady=20, text="2", command=lambda: btnClick(2))
btn3 = tk.Button(root, padx=40, pady=20, text="3", command=lambda: btnClick(3))
btn4 = tk.Button(root, padx=40, pady=20, text="4", command=lambda: btnClick(4))
btn5 = tk.Button(root, padx=40, pady=20, text="5", command=lambda: btnClick(5))
btn6 = tk.Button(root, padx=40, pady=20, text="6", command=lambda: btnClick(6))
btn7 = tk.Button(root, padx=40, pady=20, text="7", command=lambda: btnClick(7))
btn8 = tk.Button(root, padx=40, pady=20, text="8", command=lambda: btnClick(8))
btn9 = tk.Button(root, padx=40, pady=20, text="9", command=lambda: btnClick(9))
btn0 = tk.Button(root, padx=40, pady=20, text="0", command=lambda: btnClick(0))
btnAdd = tk.Button(root, padx=40, pady=20, text="+", command=lambda: add())
btnSubtract = tk.Button(root, padx=42, pady=20, text="-", command=lambda: subtract())
btnMultiply = tk.Button(root, padx=40, pady=20, text="x", command=lambda: multiply())
btnDivide = tk.Button(root, padx=40, pady=20, text="➗", command=lambda: divide())
btnEquals = tk.Button(root, padx=40, pady=20, text="=", command=lambda: equals())
btnClear = tk.Button(root, padx=120, pady=20, text="clear", command=lambda: clear())
# 布局
btn1.grid(row=1, column=0)
btn2.grid(row=1, column=1)
btn3.grid(row=1, column=2)
btn4.grid(row=2, column=0)
btn5.grid(row=2, column=1)
btn6.grid(row=2, column=2)
btn7.grid(row=3, column=0)
btn8.grid(row=3, column=1)
btn9.grid(row=3, column=2)
btn0.grid(row=4, column=0)
btnAdd.grid(row=4, column=1)
btnSubtract.grid(row=4, column=2)
btnMultiply.grid(row=5, column=0)
btnDivide.grid(row=5, column=1)
btnEquals.grid(row=5, column=2)
btnClear.grid(row=6, column=0, columnspan=3)
root.mainloop()
原文地址:https://blog.csdn.net/qq_23025319/article/details/134755035
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_28810.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。