用tkinter实现简单计算器得设计

最近了解了用Python中tkinter实现GUI界面的设计,尝试着做了简易得科学计算器,主要实现加、减、乘、除(包括整除和点除)以及取余 运算等功能,但目前只能实现输入类型为整数的运算。废话不多说,直接上代码:

import tkinter as tk
from tkinter.filedialog import *
from tkinter.messagebox import *

def author():
    showinfo(title="作者",message="小脚色")

def power():
    showinfo(title="版权信息",message="2019-05-20-17:00 周一 兰州理工大学")

def Sum_function():
	First_Number = First_Num.get()
	Second_Number = Second_Num.get()
	Sum_Num = int(First_Number) + int(Second_Number)
	##print(First_Number)
	Result_Number.set(Sum_Num)
	#return Sum_Num

def Sub_function():
	First_Number = First_Num.get()
	Second_Number = Second_Num.get()
	Sub_Num = int(First_Number) - int(Second_Number)
	Result_Number.set(Sub_Num)
	#return Sub_Num

def Multi_function():
	First_Number = First_Num.get()
	Second_Number = Second_Num.get()
	Multi_Num = int(First_Number) * int(Second_Number)
	Result_Number.set(Multi_Num)
	#return Multi_Num

def FloatDiv_function():  ###浮点数除法
	First_Number = First_Num.get()
	Second_Number = Second_Num.get()
	Div_Num = int(First_Number) / int(Second_Number)
	Result_Number.set(Div_Num)
	#return Div_Num

def IntDiv_function():  ###整数除法
	First_Number = First_Num.get()
	Second_Number = Second_Num.get()
	Div_Num = int(First_Number) // int(Second_Number)
	Result_Number.set(Div_Num)
	#return Div_Num

def Mod_function():   ###求余数
	First_Number = First_Num.get()
	Second_Number = Second_Num.get()
	Mod_Num = int(First_Number) % int(Second_Number)
	Result_Number.set(Mod_Num)
	#return Mod_Num

root = tk.Tk()
root.title('Calculator')
root.geometry('400x200')

Result_Number = tk.StringVar()

Num1_Label = tk.Label(root,text = 'First Number')
Num1_Label.grid(row = 0,column = 0,sticky = W)

First_Num = tk.Entry(root)
First_Num.grid(row = 0,column = 1,sticky = W)

Num2_Label = tk.Label(root,text = 'Second Number')
Num2_Label.grid(row = 1,column = 0,sticky = W)

Second_Num = tk.Entry(root)
Second_Num.grid(row = 1,column = 1,sticky = W)

Result_Label = tk.Label(root,text = 'Result')
Result_Label.grid(row = 2,column = 0,sticky = W)

Result_Num = tk.Entry(root,textvariable = Result_Number)
Result_Num.grid(row = 2,column = 1,sticky = W)

Button_Add = tk.Button(root,text = "加",command = Sum_function)
Button_Add.grid(row = 3,column = 3,sticky = E)

Button_Sub = tk.Button(root,text = "减",command = Sub_function)
Button_Sub.grid(row = 3,column = 4,sticky = E)

Button_Multi = tk.Button(root,text = "乘",command = Multi_function)
Button_Multi.grid(row = 3,column = 5,sticky = E)

Button_FloatDiv = tk.Button(root,text = "点除",command = FloatDiv_function)
Button_FloatDiv.grid(row = 4,column = 3,sticky = E)

Button_IntDiv = tk.Button(root,text = "整除",command = IntDiv_function)
Button_IntDiv.grid(row = 4,column = 4,sticky = E)

Button_ModDiv = tk.Button(root,text = "取余",command = Mod_function)
Button_ModDiv.grid(row = 4,column = 5,sticky = E)

MenuBar=Menu(root)
AboutMenu=Menu(root)
AboutMenu.add_command(label="作者",command=author)
AboutMenu.add_command(label="版权",command=power)
MenuBar.add_cascade(label="关于",menu=AboutMenu)

root['menu']=MenuBar

root.mainloop()

 

界面如下:

用tkinter实现简单计算器得设计_第1张图片 

最后在cmd中使用pyinstaller将其打包成.exe可执行文件,如下:

 用tkinter实现简单计算器得设计_第2张图片

打包命令为 pyinstaller -F -w xxx.py ,打包前需在cmd中进入到当前文件夹下才可正确执行。

你可能感兴趣的:(python)