用python实现计算器,他应该有以下功能:1.首先它是有gui界面的.2他可以进行加减乘除最基本的四则运算,3他可以进行进制转换
时间: 2024-05-05 09:18:13 浏览: 105
感谢您对ChitGPT的使用。
针对您的问题,以下是用Python实现计算器的代码,同时满足您提出的要求:
``` python
from tkinter import *
# 创建GUI窗口
root = Tk()
root.title("计算器")
# 创建计算器显示区域
display = Entry(root, width=30, justify=RIGHT, bd=5)
display.grid(row=0, column=0, columnspan=4)
# 定义数字按键的操作
def click_num(number):
current_num = display.get()
display.delete(0, END)
display.insert(0, str(current_num) + str(number))
# 定义加减乘除按键的操作
def click_op(operator):
current_op = display.get()
global math_op
global first_num
math_op = operator
first_num = float(current_op)
display.delete(0, END)
# 定义等于按键的操作
def click_equal():
second_num = float(display.get())
display.delete(0, END)
if math_op == '+':
result = first_num + second_num
elif math_op == '-':
result = first_num - second_num
elif math_op == '*':
result = first_num * second_num
elif math_op == '/':
result = first_num / second_num
else:
result = second_num
display.insert(0, result)
# 定义清除按键的操作
def click_clear():
display.delete(0, END)
# 定义二进制、十进制、十六进制转换的操作
def click_bin():
current_num = display.get()
current_num = int(current_num)
bin_num = bin(current_num)[2:]
display.delete(0, END)
display.insert(0, bin_num)
def click_dec():
current_num = display.get()
current_num = int(current_num, 2)
display.delete(0, END)
display.insert(0, current_num)
def click_hex():
current_num = display.get()
current_num = int(current_num, 2)
hex_num = hex(current_num)[2:]
display.delete(0, END)
display.insert(0, hex_num)
# 创建按钮
button_1 = Button(root, text="1", padx=20, pady=10, command=lambda: click_num(1))
button_2 = Button(root, text="2", padx=20, pady=10, command=lambda: click_num(2))
button_3 = Button(root, text="3", padx=20, pady=10, command=lambda: click_num(3))
button_4 = Button(root, text="4", padx=20, pady=10, command=lambda: click_num(4))
button_5 = Button(root, text="5", padx=20, pady=10, command=lambda: click_num(5))
button_6 = Button(root, text="6", padx=20, pady=10, command=lambda: click_num(6))
button_7 = Button(root, text="7", padx=20, pady=10, command=lambda: click_num(7))
button_8 = Button(root, text="8", padx=20, pady=10, command=lambda: click_num(8))
button_9 = Button(root, text="9", padx=20, pady=10, command=lambda: click_num(9))
button_0 = Button(root, text="0", padx=20, pady=10, command=lambda: click_num(0))
button_add = Button(root, text="+", padx=19, pady=10, command=lambda: click_op('+'))
button_subtract = Button(root, text="-", padx=19, pady=10, command=lambda: click_op('-'))
button_multiply = Button(root, text="*", padx=20, pady=10, command=lambda: click_op('*'))
button_divide = Button(root, text="/", padx=20, pady=10, command=lambda: click_op('/'))
button_equal = Button(root, text="=", padx=47, pady=10, command=click_equal)
button_clear = Button(root, text="清除", padx=36, pady=10, command=click_clear)
button_bin = Button(root, text="二进制", padx=20, pady=10, command=click_bin)
button_dec = Button(root, text="十进制", padx=20, pady=10, command=click_dec)
button_hex = Button(root, text="十六进制", padx=13, pady=10, command=click_hex)
# 把按钮放到Grid中
button_1.grid(row=1, column=0)
button_2.grid(row=1, column=1)
button_3.grid(row=1, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=3, column=0)
button_8.grid(row=3, column=1)
button_9.grid(row=3, column=2)
button_0.grid(row=4, column=0)
button_add.grid(row=4, column=1)
button_subtract.grid(row=4, column=2)
button_multiply.grid(row=5, column=0)
button_divide.grid(row=5, column=1)
button_equal.grid(row=5, column=2, columnspan=2)
button_clear.grid(row=6, column=0, columnspan=2)
button_bin.grid(row=2, column=3)
button_dec.grid(row=3, column=3)
button_hex.grid(row=4, column=3)
root.mainloop()
```
以上代码实现了一个具有图形用户界面(GUI)的简单计算器,它可以进行加减乘除四则运算以及十进制、二进制、十六进制之间的相互转换。
阅读全文