# 开始

### 献上代码:

# calculator.py
# magic remark
# -*- encoding:utf-8 -*-
try:
import math
except:
print('use pip install math to do it')
print('and use python -c "import math" to double-check it')
try:
import tkinter as tk
import tkinter.messagebox as msg
except:
import Tkinter as tk
import tkMessageBox as msg
base = tk.Tk()
n = 0
keys = ['+/-','x+y','x-y','x*y','x/y','x!','x√y','log x(y)','x^y']
tk.Label(base,text='x:').grid(column = 0,row = 0)
tk.Label(base,text='y:').grid(column = 2,row = 0)
x = tk.StringVar()
y = tk.StringVar()
entry_x = tk.Entry(base,textvariable=x)
entry_y = tk.Entry(base,textvariable=y)
entry_x.grid(column = 1,row = 0)
entry_y.grid(column = 3,row = 0)
def factorial(x):
res = 1
for i in range(1,x + 1):
res *= i
return res
root = lambda x,y:pow(x,y / 1)
log = math.log
def cal(op):
global x,y
_x = x.get()
_y = y.get()
if(op in ['+/-','x!','x√y','log x(y)','x^y']):
if(op == '+/-'):
op = '-x'
elif(op == 'x!'):
op = 'factorial(x)'
elif(op == 'x√y'):
op = 'root(x,y)'
elif(op == 'log x(y)'):
op = 'log(x,y)'
elif(op == 'x^y'):
op = 'x**y'
op = op.replace('x',_x)
op = op.replace('y',_y)
try:
_x = eval(op)
x.set(str(_x))
y.set('')
except:
msg.showerror('expression error','expression error')
tk.Button(base,text = '+/-',command = lambda:cal('+/-')).grid(column = 0,row = 1)
tk.Button(base,text = 'x+y',command = lambda:cal('x+y')).grid(column = 1,row = 1)
tk.Button(base,text = 'x-y',command = lambda:cal('x-y')).grid(column = 2,row = 1)
tk.Button(base,text = 'x*y',command = lambda:cal('x*y')).grid(column = 3,row = 1)
tk.Button(base,text = 'x/y',command = lambda:cal('x/y')).grid(column = 4,row = 1)
tk.Button(base,text = 'x!',command = lambda:cal('x!')).grid(column = 5,row = 1)
tk.Button(base,text = 'x√y',command = lambda:cal('x√y')).grid(column = 6,row = 1)
tk.Button(base,text = 'log x(y)',command = lambda:cal('log x(y)')).grid(column = 7,row = 1)
tk.Button(base,text = 'x^y',command = lambda:cal('x^y')).grid(column = 8,row = 1)
tk.mainloop()


### 代码剖析

##### 库的导入和魔法注释
# calculator.py
# magic remark
# -*- encoding:utf-8 -*-
try:
import math
except:
print('use pip install math to do it')
print('and use python -c "import math" to double-check it')
try:
import tkinter as tk
import tkinter.messagebox as msg
except:
import Tkinter as tk
import tkMessageBox as msg

##### 页面的初始化(第一部分)
base = tk.Tk()
n = 0
keys = ['+/-','x+y','x-y','x*y','x/y','x!','x√y','log x(y)','x^y']
tk.Label(base,text='x:').grid(column = 0,row = 0)
tk.Label(base,text='y:').grid(column = 2,row = 0)
x = tk.StringVar()
y = tk.StringVar()
entry_x = tk.Entry(base,textvariable=x)
entry_y = tk.Entry(base,textvariable=y)
entry_x.grid(column = 1,row = 0)
entry_y.grid(column = 3,row = 0)

##### 运算函数
def factorial(x):
res = 1
for i in range(1,x + 1):
res *= i
return res
root = lambda x,y:pow(x,y / 1)
log = math.log

##### 运算接口
def cal(op):
global x,y
_x = x.get()
_y = y.get()
if(op in ['+/-','x!','x√y','log x(y)','x^y']):
if(op == '+/-'):
op = '-x'
elif(op == 'x!'):
op = 'factorial(x)'
elif(op == 'x√y'):
op = 'root(x,y)'
elif(op == 'log x(y)'):
op = 'log(x,y)'
elif(op == 'x^y'):
op = 'x**y'
op = op.replace('x',_x)
op = op.replace('y',_y)
try:
_x = eval(op)
x.set(str(_x))
y.set('')
except:
msg.showerror('expression error','expression error')

##### 页面的初始化(第二部分)
tk.Button(base,text = '+/-',command = lambda:cal('+/-')).grid(column = 0,row = 1)
tk.Button(base,text = 'x+y',command = lambda:cal('x+y')).grid(column = 1,row = 1)
tk.Button(base,text = 'x-y',command = lambda:cal('x-y')).grid(column = 2,row = 1)
tk.Button(base,text = 'x*y',command = lambda:cal('x*y')).grid(column = 3,row = 1)
tk.Button(base,text = 'x/y',command = lambda:cal('x/y')).grid(column = 4,row = 1)
tk.Button(base,text = 'x!',command = lambda:cal('x!')).grid(column = 5,row = 1)
tk.Button(base,text = 'x√y',command = lambda:cal('x√y')).grid(column = 6,row = 1)
tk.Button(base,text = 'log x(y)',command = lambda:cal('log x(y)')).grid(column = 7,row = 1)
tk.Button(base,text = 'x^y',command = lambda:cal('x^y')).grid(column = 8,row = 1)

##### 页面主循环
tk.mainloop()


### 代码github

###### 作者

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏 1
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
01-01 7814

03-13 163
08-09 3万+
05-29 1万+
10-28 4322
02-14 162
11-02 5477
04-03 790