|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
from tkinter import *
str1 = ['+', '-', '*', '/']
num = []
sym = ''
root = Tk()
root.title('计算器')
root.geometry('290x350+550+170')
root.resizable(width = False, height = False)
def count(sym, num):
num = num
num[0] = int(num[0])
num[1] = int(num[1])
if sym == '+':
num[0] += num[1]
del num[1]
elif sym == '-':
num[0] -= num[1]
del num[1]
elif sym == '*':
num[0] *= num[1]
del num[1]
elif sym == '/':
num[0] /= num[1]
del num[1]
else:
pass
return num[0]
def yi():
E1.delete(0, END)
E1.insert(INSERT, 1)
def er():
E1.delete(0, END)
E1.insert(INSERT, 2)
def san():
E1.delete(0, END)
E1.insert(INSERT, 3)
def si():
E1.delete(0, END)
E1.insert(INSERT, 4)
def wu():
E1.delete(0, END)
E1.insert(INSERT, 5)
def liu():
if E1.get() == str(0):
E1.delete(0, END)
E1.insert(INSERT, 6)
def qi():
E1.delete(0, END)
E1.insert(INSERT, 7)
def ba():
E1.delete(0, END)
E1.insert(INSERT, 8)
def jiu():
E1.delete(0, END)
E1.insert(INSERT, 9)
def ling():
E1.delete(0, END)
E1.insert(INSERT, 0)
def jia():
global sym #变为全局变量
if len(num) == 0:
sym = '+'
num.append(E1.get())
E2.insert(INSERT, (E1.get() + '+'))
E1.delete(0, END)
elif len(num) == 1:
if sym in str1:
num.append(E1.get())
E2.insert(INSERT, (E1.get() + '%s' % sym))
E1.delete(0, END)
num[0] = count(sym, num)
E1.insert(INSERT, num[0])
sym = '+'
else:
sym = '+'
num.append(E1.get())
E2.insert(INSERT, (E1.get() + '%s' % sym))
E1.delete(0, END)
num[0] = count(sym, num)
E1.insert(INSERT, num[0])
def jian():
global sym
if len(num) == 0:
sym = '-'
num.append(E1.get())
E2.insert(INSERT, (E1.get() + '-'))
E1.delete(0, END)
elif len(num) == 1:
if sym in str1:
num.append(E1.get())
E2.insert(INSERT, (E1.get() + '%s' % sym))
E1.delete(0, END)
num[0] = count(sym, num)
E1.insert(INSERT, num[0])
sym = '-'
else:
sym = '-'
num.append(E1.get())
E2.insert(INSERT, (E1.get() + '%s' % sym))
E1.delete(0, END)
num[0] = count(sym, num)
E1.insert(INSERT, num[0])
def cheng():
global sym
if len(num) == 0:
sym = '*'
num.append(E1.get())
E2.insert(INSERT, (E1.get() + '*'))
E1.delete(0, END)
elif len(num) == 1:
if sym in str1:
num.append(E1.get())
E2.insert(INSERT, (E1.get() + '%s' % sym))
E1.delete(0, END)
num[0] = count(sym, num)
E1.insert(INSERT, num[0])
sym = '*'
else:
sym = '*'
num.append(E1.get())
E2.insert(INSERT, (E1.get() + '%s' % sym))
E1.delete(0, END)
num[0] = count(sym, num)
E1.insert(INSERT, num[0])
def chu():
global sym
if len(num) == 0:
sym = '/'
num.append(E1.get())
E2.insert(INSERT, (E1.get() + '/'))
E1.delete(0, END)
elif len(num) == 1:
if sym in str1:
num.append(E1.get())
E2.insert(INSERT, (E1.get() + '%s' % sym))
E1.delete(0, END)
num[0] = count(sym, num)
E1.insert(INSERT, num[0])
sym = '/'
else:
sym = '/'
num.append(E1.get())
E2.insert(INSERT, (E1.get() + '%s' % sym))
E1.delete(0, END)
num[0] = count(sym, num)
E1.insert(INSERT, num[0])
def deng():
num.append(E1.get())
E2.insert(INSERT, (E1.get() + '%s' % sym))
E1.delete(0, END)
num[0] = count(sym, num)
E1.insert(INSERT, num[0])
def c0():
n = len(E1.get())
E1.delete(n - 1,INSERT)
def ce():
if len(num):
for i in num:
num.remove(i)
E1.delete(0,END)
E1.insert(0, 0)
E2.delete(0, END)
# 输入框
E1 = Entry(root,justify = RIGHT, font = ('黑体', 20))
E1.place(x = 10, y = 5, width = 270, height = 60)
E1.insert(0,0)
E2 = Entry(root,justify = RIGHT)
E2.place(x = 10, y = 5, width = 270)
Fa = Frame(root)
Fa.place(x = 5,y = 70, width = 290, height = 590)
# 按钮
B1 = Button(Fa, text = '1', command = yi)
B1.place(x = 5, y = 165, width = 50, height = 50)
B2 = Button(Fa, text = '2', command = er)
B2.place(x = 60, y = 165, width = 50, height = 50)
B3 = Button(Fa, text = '3',command = san)
B3.place(x = 115, y = 165, width = 50, height = 50)
B4 = Button(Fa, text = '4', command = si)
B4.place(x = 5, y = 110, width = 50, height = 50)
B5 = Button(Fa, text = '5', command = wu)
B5.place(x = 60, y = 110, width = 50, height = 50)
B6 = Button(Fa, text = '6', command = liu)
B6.place(x = 115, y = 110, width = 50, height = 50)
B7 = Button(Fa, text = '7', command = qi)
B7.place(x = 5, y = 55, width = 50, height = 50)
B8 = Button(Fa, text = '8', command = ba)
B8.place(x = 60, y = 55, width = 50, height = 50)
B9 = Button(Fa, text = '9', command = jiu)
B9.place(x = 115, y = 55, width = 50, height = 50)
B0 = Button(Fa, text = '0', command = ling)
B0.place(x = 5, y = 220, width = 100, height = 50)
# 加
Ba = Button(Fa, text = '+', command = jia)
Ba.place(x = 170, y = 220, width = 50, height = 50)
# 减
Bb = Button(Fa, text = '-', command = jian)
Bb.place(x = 170, y = 165, width = 50, height = 50)
# 乘
Bc = Button(Fa, text = '*', command = cheng)
Bc.place(x = 170, y = 110, width = 50, height = 50)
# 除
Bd = Button(Fa, text = '/', command = chu)
Bd.place(x = 170, y = 55, width = 50, height = 50)
# 等于
Be = Button(Fa, text = '=', command = deng)
Be.place(x = 225, y = 165, width = 50, height = 105)
# 点
Bdian = Button(Fa, text = '.')
Bdian.place(x = 115, y = 220, width = 50, height = 50)
# C
BC = Button(Fa, text = 'C', command = c0)
BC.place(x = 115, y = 0, width = 50, height = 50)
# CE
BCE = Button(Fa, text = 'CE', command = ce)
BCE.place(x = 60, y = 0, width = 50, height = 50)
# 左箭头
Bzuo = Button(Fa, text = '←')
Bzuo.place(x = 5, y = 0, width = 50, height = 50)
# + -
Bzheng = Button(Fa, text = '±')
Bzheng.place(x = 170, y = 0, width = 50, height = 50)
# 平方根
Bp = Button(Fa, text = '√')
Bp.place(x = 225, y = 0, width = 50, height = 50)
# 百分号
Bbfh = Button(Fa, text = '%')
Bbfh.place(x = 225, y = 55, width = 50, height = 50)
# 1/x
Bpai = Button(Fa, text = '1/x')
Bpai.place(x = 225, y = 110, width = 50, height = 50)
mainloop()
只能按顺序计算,不能先乘除后加减,还有代码太乱,不整洁易懂,请老师们,师兄师姐们 批评指点,拜谢大家~~!!!!
|
|