写出代码.利用Tkinter简单的计算机开发和设计Python的基本数字运算(加法、减法、乘法和除法、平方、除法、sin、cos等)、Python字符串相关运算、Python选择语句、循环语句、函数等相关设计。
时间: 2023-07-03 18:06:31 浏览: 104
以下是一个基本的 Tkinter 计算器应用程序,可以进行加、减、乘、除、平方、sin、cos 等运算:
```python
import tkinter as tk
from math import sin, cos
# 创建Tkinter窗口
window = tk.Tk()
window.title("计算器")
# 创建输入框
input_field = tk.Entry(window, width=50, borderwidth=5)
input_field.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# 定义按钮的点击事件
def button_click(number):
current = input_field.get()
input_field.delete(0, tk.END)
input_field.insert(0, str(current) + str(number))
def button_clear():
input_field.delete(0, tk.END)
def button_add():
first_number = input_field.get()
global f_num
global math_operation
math_operation = "addition"
f_num = float(first_number)
input_field.delete(0, tk.END)
def button_subtract():
first_number = input_field.get()
global f_num
global math_operation
math_operation = "subtraction"
f_num = float(first_number)
input_field.delete(0, tk.END)
def button_multiply():
first_number = input_field.get()
global f_num
global math_operation
math_operation = "multiplication"
f_num = float(first_number)
input_field.delete(0, tk.END)
def button_divide():
first_number = input_field.get()
global f_num
global math_operation
math_operation = "division"
f_num = float(first_number)
input_field.delete(0, tk.END)
def button_squared():
first_number = input_field.get()
global f_num
f_num = float(first_number)
result = f_num ** 2
input_field.delete(0, tk.END)
input_field.insert(0, result)
def button_sin():
first_number = input_field.get()
global f_num
f_num = float(first_number)
result = sin(f_num)
input_field.delete(0, tk.END)
input_field.insert(0, result)
def button_cos():
first_number = input_field.get()
global f_num
f_num = float(first_number)
result = cos(f_num)
input_field.delete(0, tk.END)
input_field.insert(0, result)
def button_equal():
second_number = input_field.get()
input_field.delete(0, tk.END)
if math_operation == "addition":
input_field.insert(0, f_num + float(second_number))
elif math_operation == "subtraction":
input_field.insert(0, f_num - float(second_number))
elif math_operation == "multiplication":
input_field.insert(0, f_num * float(second_number))
elif math_operation == "division":
input_field.insert(0, f_num / float(second_number))
# 创建按钮
button_1 = tk.Button(window, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = tk.Button(window, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = tk.Button(window, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = tk.Button(window, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = tk.Button(window, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = tk.Button(window, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7 = tk.Button(window, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = tk.Button(window, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = tk.Button(window, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = tk.Button(window, text="0", padx=40, pady=20, command=lambda: button_click(0))
button_add = tk.Button(window, text="+", padx=39, pady=20, command=button_add)
button_subtract = tk.Button(window, text="-", padx=41, pady=20, command=button_subtract)
button_multiply = tk.Button(window, text="*", padx=40, pady=20, command=button_multiply)
button_divide = tk.Button(window, text="/", padx=41, pady=20, command=button_divide)
button_squared = tk.Button(window, text="x²", padx=37, pady=20, command=button_squared)
button_sin = tk.Button(window, text="sin", padx=35, pady=20, command=button_sin)
button_cos = tk.Button(window, text="cos", padx=35, pady=20, command=button_cos)
button_clear = tk.Button(window, text="清空", padx=79, pady=20, command=button_clear)
button_equal = tk.Button(window, text="=", padx=91, pady=20, command=button_equal)
# 将按钮放置在窗口上
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, 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=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_0.grid(row=4, column=0)
button_add.grid(row=5, column=0)
button_subtract.grid(row=6, column=0)
button_multiply.grid(row=6, column=1)
button_divide.grid(row=6, column=2)
button_squared.grid(row=4, column=1)
button_sin.grid(row=4, column=2)
button_cos.grid(row=5, column=1)
button_clear.grid(row=7, column=0, columnspan=2)
button_equal.grid(row=7, column=2, columnspan=2)
# 运行窗口
window.mainloop()
```
以上代码可以实现一个简单的计算器应用程序,包括数字运算、字符串相关运算、选择语句、循环语句和函数等常见功能。
阅读全文