#!/usr/bin/env python3.6
import tkinter
from tkinter import Tk
import parser #分析器模块
from tkinter import END
import math
import datetime
from time import localtime,strftime
root = tkinter.Tk()
root.title(‘function Calculator‘)
i = 0
display = tkinter.Entry(root,font = ("Calibri",13),width =50)
display.grid(row = 1,columnspan = 9)
display1 = tkinter.Entry(root,state = ‘normal‘,font = ("Calibri",13),width =50)
display1.grid(row = 6,columnspan = 9)
# -*- coding:utf-8 -*- 此处复制粘贴功能并未实现
# 为计算机添加显示模块和菜单与快捷菜单,以实现对计算结果的复制与粘贴及退出程序。
menu = tkinter.Menu(root) # 生成菜单
submenu = tkinter.Menu(menu,tearoff=0) # 生成下拉菜单
submenu.add_command(label="Open") # 向下拉菜单中添加Open命令
submenu.add_command(label="Save") # 向下拉菜单中添加Save命令
def Exit():
root.quit()
submenu.add_command(label="Exit",command=Exit) # 向下拉菜单中添加Exit命令
menu.add_cascade(label="File",menu=submenu) # 将下拉菜单添加到菜单中
submenu = tkinter.Menu(menu,tearoff=0) # 生成下拉菜单
def Copy():
display.get()
submenu.add_command(label="Copy",command=Copy)
def Cut():
Copy()
#删除所选内容
clear_all()
submenu.add_command(label=‘Cut‘,command=Cut)
submenu.add_separator()
def Paste():
#如果没有选中内容,直接粘贴到鼠标位置
#如果有所选内容,则先删除再粘贴
Copy()
submenu.add_command(label=‘Paste‘,command=Paste)
menu.add_cascade(label="Edit",menu=submenu) # 将下拉菜单添加到菜单中
submenu = tkinter.Menu(menu,tearoff=1) # 生成下拉菜单
submenu.add_command(label="About") # 向下拉菜单中添加About命令
menu.add_cascade(label="Help",menu=submenu) # 向下拉菜单中添加到菜单中
root.bind("<Control-c>",Copy)
root.bind("<Control-x>",Cut)
root.bind("<Control-v>",Paste) # 向下拉菜单中添加到菜单中
root.bind("<KeyPress-E>",Exit)
root.config(menu=menu)
def timeshow(): #时间模块
whole_string = datetime.datetime.now()
display1.insert(0,whole_string)
def timeclear():
"""clears all the content in the Entry widget"""
display1.delete(0,END)
def time(): #时间模块
whole_string = display1.get()
operator = time()
length = len(operator)
display1.insert(0,operator)
def factorial():
"""Calculates the factorial of the number entered."""
whole_string = display.get()
number = int(whole_string)
fact = 1
counter = number
try:
while counter > 0:
fact = fact*counter
counter -= 1
clear_all()
display.insert(0,fact)
except Exception:
clear_all()
display.insert(0,"Error")
def clear_all():
"""clears all the content in the Entry widget"""
display.delete(0,END)
def get_variables(num):
"""Gets the user input for operands and puts it inside the entry widget"""
global i
display.insert(i,num)
i += 1
def get_operation(operator):
"""Gets the operand the user wants to apply on the functions"""
global i
length = len(operator)
display.insert(i,operator)
i += length
def undo():
"""removes the last entered operator/variable from entry widget"""
whole_string = display.get()
if len(whole_string): ## repeats until
## now just decrement(减量) the string by one index
new_string = whole_string[:-1]
print(new_string)
clear_all()
display.insert(0,new_string)
else:
clear_all()
display.insert(0,"Error, press AC")
def exp():
whole_string = display.get()
new_string = float(whole_string)
expx = math.sin(new_string)
clear_all()
display.insert(0,expx)
def sin():
whole_string = display.get()
new_string = float(whole_string)
sinx = math.sin(new_string)
clear_all()
display.insert(0,sinx)
def cos():
whole_string = display.get()
new_string = float(whole_string)
cosx = math.cos(new_string)
clear_all()
display.insert(0,cosx)
def tan():
whole_string = display.get()
new_string = float(whole_string)
tanx = math.tan(new_string)
clear_all()
display.insert(0,tanx)
def asin():
whole_string = display.get()
new_string = float(whole_string)
asinx = math.asin(new_string)
clear_all()
display.insert(0,asinx)
def acos():
whole_string = display.get()
new_string = float(whole_string)
acosx = math.acos(new_string)
clear_all()
display.insert(0,acosx)
def log():
whole_string = display.get()
new_string = float(whole_string)
logx = math.log(new_string)
clear_all()
display.insert(0,logx)
def sqrt():
whole_string = display.get()
new_string = float(whole_string)
sqrtx = math.sqrt(new_string)
clear_all()
display.insert(0,sqrtx)
def powx():
whole_string = display.get()
new_strlist = whole_string.split(‘,‘)
x = int(new_strlist[0])
y = int(new_strlist[1])
powx = math.pow(x,y)
clear_all()
display.insert(0,powx)
def calculate():
"""
Evaluates the expression
ref : http://stackoverflow.com/questions/594266/equation-parsing-in-python
"""
whole_string = display.get()
try:
formulae = parser.expr(whole_string).compile() #formulae:配方公式 paser分析器 expr如果表达式评估为假
result = eval(formulae) #eval(evaluate) 评价
clear_all()
display.insert(0,result)
except Exception:
clear_all()
display.insert(0,"Error!")
one = tkinter.Button(root,text = "1",width = 5,command = lambda : get_variables(1),font=("Calibri",12))
one.grid(row = 2,column = 0)
two = tkinter.Button(root,text = "2",width = 5,command = lambda : get_variables(2),font=("Calibri",12))
two.grid(row = 2,column = 1)
three = tkinter.Button(root,text = "3",width = 5,command = lambda : get_variables(3),font=("Calibri",12))
three.grid(row = 2,column = 2)
four = tkinter.Button(root,text = "4",width = 5,command = lambda : get_variables(4),font=("Calibri",12))
four.grid(row = 3,column = 0)
five = tkinter.Button(root,text = "5",width = 5,command = lambda : get_variables(5),font=("Calibri",12))
five.grid(row = 3,column = 1)
six = tkinter.Button(root,text = "6",width = 5,command = lambda : get_variables(6),font=("Calibri",12))
six.grid(row = 3,column = 2)
seven = tkinter.Button(root,text = "7",width = 5,command = lambda : get_variables(7),font=("Calibri",12))
seven.grid(row = 4,column = 0)
eight = tkinter.Button(root,text = "8",width = 5,command = lambda : get_variables(8),font=("Calibri",12))
eight.grid(row = 4,column = 1)
nine = tkinter.Button(root,text = "9",width = 5,command = lambda : get_variables(9),font=("Calibri",12))
nine.grid(row = 4,column = 2)
cls = tkinter.Button(root,text = "AC",width = 5,command = clear_all,font=("Calibri", 12),foreground = "red")
cls.grid(row = 5,column = 0)
zero = tkinter.Button(root,text = "0",width = 5,command = lambda : get_variables(0),font=("Calibri",12))
zero.grid(row = 5,column = 1)
result = tkinter.Button(root,text = "=",width = 5,command = calculate,font=("Calibri", 12),foreground = "red")
result.grid(row = 5,column = 2)
plus = tkinter.Button(root,text = "+",width = 5,command = lambda : get_operation("+"),font=("Calibri",12))
plus.grid(row = 2,column = 3)
minus = tkinter.Button(root,text = "-",width = 5,command = lambda : get_operation("-"),font=("Calibri",12))
minus.grid(row = 3,column = 3)
multiply = tkinter.Button(root,text = "*",width = 5,command = lambda : get_operation("*"),font=("Calibri",12))
multiply.grid(row = 4,column = 3)
divide = tkinter.Button(root,text = "/",width = 5,command = lambda : get_operation("/"),font=("Calibri",12))
divide.grid(row = 5,column = 3)
# adding new operations
pi = tkinter.Button(root,text = "pi",width = 5,command = lambda: get_operation("*3.14"),font =("Calibri",12))
pi.grid(row = 2,column = 4)
modulo = tkinter.Button(root,text = "%",width = 5,command = lambda : get_operation("%"),font=("Calibri",12))
modulo.grid(row = 3,column = 4)
left_bracket = tkinter.Button(root,text = "(",width = 5,command = lambda: get_operation("("),font =("Calibri",12))
left_bracket.grid(row = 4,column = 4)
exp = tkinter.Button(root,text = "exp",width = 5,command = exp,font = ("Calibri",12))
exp.grid(row = 5,column = 4)
## To be added :
# sin, cos, log, ln
undo_button = tkinter.Button(root,text = "<-",width = 5,command = undo,font =("Calibri",12),foreground = "red")
undo_button.grid(row = 2,column = 5)
fact = tkinter.Button(root,text = "x!",width = 5,command = factorial,font=("Calibri",12))
fact.grid(row = 3,column = 5)
right_bracket = tkinter.Button(root,text = ")",width = 5,command = lambda: get_operation(")"),font =("Calibri",12))
right_bracket.grid(row = 4,column = 5)
square = tkinter.Button(root,text = "^2",width = 5,command = lambda: get_operation("**2"),font = ("Calibri",12))
square.grid(row = 5,column = 5)
#adding
point = tkinter.Button(root,text = ".",width = 5,command = lambda: get_operation("."),font = ("Calibri",12))
point.grid(row = 2,column = 6)
sin = tkinter.Button(root,text = "sin",width = 5,command = sin,font = ("Calibri",12))
sin.grid(row = 3,column = 6)
cos = tkinter.Button(root,text = "cos",width = 5,command = cos,font = ("Calibri",12))
cos.grid(row = 4,column = 6)
tan = tkinter.Button(root,text = "tan",width = 5,command = tan,font = ("Calibri",12))
tan.grid(row = 5,column = 6)
#adding
acos = tkinter.Button(root,text = "acos",width = 5,command = acos,font = ("Calibri",12))
acos.grid(row = 2,column = 7)
asin = tkinter.Button(root,text = "asin",width = 5,command = asin,font = ("Calibri",12))
asin.grid(row = 3,column = 7)
log = tkinter.Button(root,text = "log",width = 5,command = log,font = ("Calibri",12))
log.grid(row = 4,column = 7)
sqrt = tkinter.Button(root,text = "sqrt",width = 5,command = sqrt,font = ("Calibri",12))
sqrt.grid(row = 5,column = 7)
powx = tkinter.Button(root,text = "pow",width = 5,command = powx,font = ("Calibri",12))
powx.grid(row = 2,column = 8)
comma = tkinter.Button(root,text = ",",width = 5,command = lambda: get_operation(","),font = ("Calibri",12))
comma.grid(row = 3,column = 8)
time = tkinter.Button(root,text = "time",width = 5,command = timeshow,font = ("Calibri",12))
time.grid(row = 4,column = 8)
timeAC = tkinter.Button(root,text = "timeAC",width = 5,command = timeclear,font = ("Calibri",12))
timeAC.grid(row = 5,column = 8)
root.mainloop()
原文:https://www.cnblogs.com/fan-fanfan/p/8989915.html