使用python编写一个计算器 并打包成可执行文件
时间: 2023-07-16 12:17:03 浏览: 105
好的,我可以为你提供一个Python计算器的示例代码,并指导你如何将其打包成可执行文件。首先,我们使用Python的Tkinter模块来创建GUI界面,然后使用eval()函数计算用户输入的表达式。以下是示例代码:
```python
import tkinter as tk
class Calculator:
def __init__(self, master):
self.master = master
master.title("Calculator")
# 创建输入框
self.entry = tk.Entry(master, width=30, borderwidth=5)
self.entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# 创建数字按钮
self.button_1 = tk.Button(master, text="1", padx=40, pady=20, command=lambda: self.button_click(1))
self.button_2 = tk.Button(master, text="2", padx=40, pady=20, command=lambda: self.button_click(2))
self.button_3 = tk.Button(master, text="3", padx=40, pady=20, command=lambda: self.button_click(3))
self.button_4 = tk.Button(master, text="4", padx=40, pady=20, command=lambda: self.button_click(4))
self.button_5 = tk.Button(master, text="5", padx=40, pady=20, command=lambda: self.button_click(5))
self.button_6 = tk.Button(master, text="6", padx=40, pady=20, command=lambda: self.button_click(6))
self.button_7 = tk.Button(master, text="7", padx=40, pady=20, command=lambda: self.button_click(7))
self.button_8 = tk.Button(master, text="8", padx=40, pady=20, command=lambda: self.button_click(8))
self.button_9 = tk.Button(master, text="9", padx=40, pady=20, command=lambda: self.button_click(9))
self.button_0 = tk.Button(master, text="0", padx=40, pady=20, command=lambda: self.button_click(0))
# 创建运算符按钮
self.button_add = tk.Button(master, text="+", padx=39, pady=20, command=lambda: self.button_click("+"))
self.button_subtract = tk.Button(master, text="-", padx=41, pady=20, command=lambda: self.button_click("-"))
self.button_multiply = tk.Button(master, text="*", padx=40, pady=20, command=lambda: self.button_click("*"))
self.button_divide = tk.Button(master, text="/", padx=41, pady=20, command=lambda: self.button_click("/"))
self.button_equal = tk.Button(master, text="=", padx=91, pady=20, command=self.button_equal)
self.button_clear = tk.Button(master, text="Clear", padx=79, pady=20, command=self.button_clear)
# 将按钮放入界面
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)
def button_click(self, number):
current = self.entry.get()
self.entry.delete(0, tk.END)
self.entry.insert(0, str(current) + str(number))
def button_clear(self):
self.entry.delete(0, tk.END)
def button_equal(self):
expression = self.entry.get()
result = eval(expression)
self.entry.delete(0, tk.END)
self.entry.insert(0, result)
root = tk.Tk()
calculator = Calculator(root)
root.mainloop()
```
在代码中,我们创建了一个Calculator类,该类包含了计算器的GUI界面和计算逻辑。我们使用grid函数将数字按钮和运算符按钮放置在GUI界面中。
然后我们通过创建一个Tk对象,并将其传递给Calculator类来显示计算器的GUI界面。
现在我们需要将这个Python脚本打包成可执行文件。我们可以使用pyinstaller库来完成这个任务。以下是打包步骤:
1. 安装pyinstaller库
在命令行中输入以下命令:
```
pip install pyinstaller
```
2. 打包Python脚本
在命令行中切换到Python脚本所在目录,并输入以下命令:
```
pyinstaller --onefile calculator.py
```
这将会在dist目录下生成一个可执行文件,名为calculator.exe。
现在你可以双击该可执行文件来运行计算器了。
阅读全文