编写计算器小软件

知识共享许可协议 版权声明:署名,允许他人基于本文进行创作,且必须基于与原先许可协议相同的许可协议分发本文 (Creative Commons

项目目的

主要是为了训练tkinter库的使用,熟悉常用tkinter的控件,能够编写发布EXE可执行文件。作为开发小软件的练手项目,大致走通整条路线,以后需要工具软件也可以自己开发。

预期功能

实现简单计算器加减乘除的功能,打包成可执行文件,并附带软件图标。

开发步骤

由于是第一次做,因此将开发分成两步。第一步是代码实现,通过编译器实现预期功能;第二部是封装成EXE,可以在其他电脑运行。

代码实现

import tkinter
root=tkinter.Tk()
root.title("QJX's Calculator")   #窗口标题
root.geometry('500x400')  #窗口大小
root.iconbitmap("cal.ico")	#左上角图标更换

# 用户输入
def text_input(v):
    return lambda:text.insert('insert',v)

# 计算结果
def Res():
	qus = text.get('0.0', 'end')
	text.insert('insert','=')
	text.insert('insert',eval(qus))

# 绘制显示框
var=tkinter.StringVar()
text=tkinter.Text(root,font=('Times New Roman',30),height=1,width=22,bg='grey')
text.place(x=7,y=1,anchor='nw')

# 计算器GUI
btn_on=tkinter.Button(root,text='ON',font=('Times New Roman',16),height=1,width=10,bg='blue',command=lambda:text.delete('1.0','end')).place(x=10,y=53,anchor='nw')
btn_off=tkinter.Button(root,text='OFF',font=('Times New Roman',16),height=1,width=10,bg='blue',command=lambda:text.delete('1.0','end')).place(x=163,y=53,anchor='nw')
btn_clear=tkinter.Button(root,text='Clear',font=('Times New Roman',16),height=1,width=10,bg='blue',command=lambda:text.delete('1.0','end')).place(x=316,y=53,anchor='nw')

# 按键和计算符号
btn1=tkinter.Button(root,text='1',font=('Times New Roman',30),height=1,width=3,fg='white',bg='green',command=text_input(1)).place(x=10,y=100,anchor='nw')
btn2=tkinter.Button(root,text='2',font=('Times New Roman',30),height=1,width=3,fg='white',bg='green',command=text_input(2)).place(x=100,y=100,anchor='nw')
btn3=tkinter.Button(root,text='3',font=('Times New Roman',30),height=1,width=3,fg='white',bg='green',command=text_input(3)).place(x=190,y=100,anchor='nw')
btn4=tkinter.Button(root,text='4',font=('Times New Roman',30),height=1,width=3,fg='white',bg='green',command=text_input(4)).place(x=10,y=200,anchor='nw')
btn5=tkinter.Button(root,text='5',font=('Times New Roman',30),height=1,width=3,fg='white',bg='green',command=text_input(5)).place(x=100,y=200,anchor='nw')
btn6=tkinter.Button(root,text='6',font=('Times New Roman',30),height=1,width=3,fg='white',bg='green',command=text_input(6)).place(x=190,y=200,anchor='nw')
btn7=tkinter.Button(root,text='7',font=('Times New Roman',30),height=1,width=3,fg='white',bg='green',command=text_input(7)).place(x=10,y=300,anchor='nw')
btn8=tkinter.Button(root,text='8',font=('Times New Roman',30),height=1,width=3,fg='white',bg='green',command=text_input(8)).place(x=100,y=300,anchor='nw')
btn9=tkinter.Button(root,text='9',font=('Times New Roman',30),height=1,width=3,fg='white',bg='green',command=text_input(9)).place(x=190,y=300,anchor='nw')
btn0=tkinter.Button(root,text='0',font=('Times New Roman',30),height=1,width=3,fg='white',bg='green',command=text_input(0)).place(x=280,y=300,anchor='nw')
btn_plus=tkinter.Button(root,text='+',font=('Times New Roman',30),height=1,width=3,bg='yellow',command=text_input('+')).place(x=280,y=100,anchor='nw')
btn_de=tkinter.Button(root,text='-',font=('Times New Roman',30),height=1,width=3,bg='yellow',command=text_input('-')).place(x=370,y=100,anchor='nw')
btn_mul=tkinter.Button(root,text='*',font=('Times New Roman',30),height=1,width=3,bg='yellow',command=text_input('*')).place(x=280,y=200,anchor='nw')
btn_did=tkinter.Button(root,text='/',font=('Times New Roman',30),height=1,width=3,bg='yellow',command=text_input('/')).place(x=370,y=200,anchor='nw')
btn_equ=tkinter.Button(root,text='=',font=('Times New Roman',30),height=1,width=3,bg='yellow',command=Res).place(x=370,y=300,anchor='nw')

root.mainloop()

代码封装

代码封装主要是将代码打包成可执行文件并附加软件图标。需要执行以下步骤:

  1. pyinstaller安装
    利用pip命令安装pyinstaller:
pip install pyinstaller
  1. 代码封装
    在cmd环境下将目录调整至代码py文件所在目录,图标ico文件也放在该目录下,执行
pyinstaller -F -i cal.ico Calulator1.py -w

-w :关闭DOC控制台
-i : 替换exe图标
按照以上步骤,就可以生成对应的小软件。开发类似的软件都可以遵循类似的思路。

猜你喜欢

转载自blog.csdn.net/zhanshen112/article/details/92435785