计算器demo.exe
最近两天在琢磨复利相关的东西,今天突然想用python写一个桌面小软件,花了1个小时写了个简单计算器,并打包成exe软件。
import tkinter as tk
# 计算功能函数
# 追加数字
def append_num(i):
list.append(i)
result.set(''.join(list))
# 选择运算符
def operator(i):
if len(list) > 0:
if list[-1] in ['+', '-', 'x', '/']:
list[-1] = i
else:
list.append(i)
result.set(''.join(list))
# 清零
def clear():
list.clear()
result.set(0)
# 退格
def back():
# list.pop()
if len(list) > 0:
del list[-1]
result.set(list)
# 计算
def equal():
a = ''.join(list)
end_num = equal(a)
result.set(end_num)
list.clear()
list.append(str(end_num))
# 界面
# 实例化一个窗口对象
root = tk.Tk()
# 标题
root.title('计算器')
# 设置大小位置
root.geometry("300x280+110+150")
# 透明度
root.attributes("-alpha", 0.88)
# 背景
root['background'] = "#ffffff"
#
list = []
result = tk.StringVar()
result.set(0)
# 标签
label1 = tk.Label(root, textvariable=result, width=20, height=2, font=("宋体", 20), justify='left', background="#ffffff",
anchor='se')
# 布局
label1.grid(row=0, column=0, columnspan=4)
# 按钮
button_clear = tk.Button(root, text='C', width=5, font=("宋体", 16), relief='flat', background="#C0C0C0",
command=lambda: clear())
button_clear.grid(padx=4, row=1, column=0)
button_back = tk.Button(root, text='←', width=5, font=("宋体", 16), relief='flat', background="#C0C0C0",
command=lambda: back())
button_back.grid(padx=4, row=1, column=1)
button_division = tk.Button(root, text='/', width=5, font=("宋体", 16), relief='flat',background="#C0C0C0",
command=lambda: operator('/'))
button_division.grid(padx=4, row=1, column=2)
button_multiplication = tk.Button(root, text='x', width=5, font=("宋体", 16), relief='flat',background="#C0C0C0",
command=lambda: operator('*'))
button_multiplication.grid(padx=4, row=1, column=3)
button_1 = tk.Button(root, text='1', width=5, font=("宋体", 16), relief='flat',background="#C0C0C0", command=lambda: append_num('1'))
button_1.grid(padx=4, row=2, column=0)
button_2 = tk.Button(root, text='2', width=5, font=("宋体", 16), relief='flat',background="#C0C0C0", command=lambda: append_num('2'))
button_2.grid(padx=4, row=2, column=1)
button_3 = tk.Button(root, text='3', width=5, font=("宋体", 16), relief='flat',background="#C0C0C0", command=lambda: append_num('3'))
button_3.grid(padx=4, row=2, column=2)
button_plus = tk.Button(root, text='+', width=5, font=("宋体", 16), relief='flat',background="#C0C0C0", command=lambda: append_num('+'))
button_plus.grid(padx=4, row=2, column=3)
button_4 = tk.Button(root, text='4', width=5, font=("宋体", 16), relief='flat',background="#C0C0C0", command=lambda: append_num('4'))
button_4.grid(padx=4, row=3, column=0)
button_5 = tk.Button(root, text='5', width=5, font=("宋体", 16), relief='flat',background="#C0C0C0", command=lambda: append_num('5'))
button_5.grid(padx=4, row=3, column=1)
button_6 = tk.Button(root, text='6', width=5, font=("宋体", 16), relief='flat',background="#C0C0C0", command=lambda: append_num('6'))
button_6.grid(padx=4, row=3, column=2)
button_dec = tk.Button(root, text='-', width=5, font=("宋体", 16), relief='flat',background="#C0C0C0", command=lambda: append_num('-'))
button_dec.grid(padx=4, row=3, column=3)
button_7 = tk.Button(root, text='7', width=5, font=("宋体", 16), relief='flat',background="#C0C0C0", command=lambda: append_num('7'))
button_7.grid(padx=4, row=4, column=0)
button_8 = tk.Button(root, text='8', width=5, font=("宋体", 16), relief='flat',background="#C0C0C0", command=lambda: append_num('8'))
button_8.grid(padx=4, row=4, column=1)
button_9 = tk.Button(root, text='9', width=5, font=("宋体", 16), relief='flat',background="#CfCfC0", command=lambda: append_num('9'))
button_9.grid(padx=4, row=4, column=2)
button_0 = tk.Button(root, text='0', width=5, font=("宋体", 16), relief='flat',background="#C0C0C0", command=lambda: append_num('0'))
button_0.grid(padx=4, row=4, column=3)
button_equal = tk.Button(root, text='=', width=5, font=("宋体", 16), relief='flat', background="#C0C0C0",
command=lambda: append_num('='))
button_equal.grid(padx=4, row=5, column=3, columnspan=4)
# 消息循环
root.mainloop()
然后用pyinstaller打包成exe软件
开发环境:anaconda+pycharm
- 1.在Anaconda prompt中activate 虚拟环境;
- 2.在anaconda虚拟环境中conda install pyinstaller;
- 3.cd 文件.py;
- 4.pyinstaller -F 文件名.py;
- 5.返回目标文件目录,打包好的exe文件,在同目录的dist文件中。
只做了个简单的小计算器,好像计算还没显示,配色啥的也没做,不过思路大体一样,做其他计算软件,包括图形显示,对视分析可视化啥的,差不多!