经典项目----计算器,这个代码仅实现了两数操作,但是部分功能已经具备,比如删除单个字符,采用的是字符串分片来操作的,清空,则直接改变显示器的数字;在做的过程中本以为要把每个数字进行×10算法才可以进行计算,之后才想到Python可以使用强制转换来直接把字符串转化为int类型来进行运算;在删除操作时,引用了一个变量l,来辅助删除;开发过程中遇到了一个小问题,就是在函数中修改全局变量时报错,报错信息如下 local variable 'num1' referenced before assignment ,因为要想在函数里面改变全局变量的值,需要在函数里面使用global声明变量为全局变量,比如:global num1,问题得到了解决。
不多说了,附上代码
from tkinter import*
root = Tk()
#设置窗口的长宽
#注意:300x380 ,其中的x就是英文字母x
root.geometry('300x380')
#设置窗口的标题
root.title("计算器")
#定义面板
frame_show = Frame(width=300, height=150, bg="#dddddd")
frame_show.pack()
#定义顶部区域
sv = StringVar() #prthon内定义函数,作用是使字符串显示在指定控件上
sv.set('0') #设置字符串
#定义显示字符串的标签,justify设置字符串对齐方式,anchor设置控件的锚点(东南西北)
show_label = Label(frame_show, textvariable=sv, bg='white', width=12, height=1, font=("黑体", 20, 'bold'),\
justify=LEFT, anchor='e')
show_label.pack(padx=10, pady=10)
#按键区
num1 = ''
num2 = ''
operator = None
l = '' #定义了一个字符串,里面包含运算符和第二个数,目的是方便deldte()使用
def delete():
global num1
global num2
global operator
global l;
if operator == None:
num1 = num1[:-1]#利用切片法删除最后一个字符串
sv.set(num1)
else:
l = l[:-1]
if(l ==''):
operator = None
sv.set(num1+l)
def clear():
global num1
global num2
global operator
num1 = ''
num2 = ''
operator = None
sv.set('0')
def fan():
global num1
global num2
global operator
def ce():
global num1
global num2
global operator
def change(num):
global num1
global num2
global operator
global l
if operator == None:
num1 = num1 + num
sv.set(num1)
else:
num2 = num2 + num
l = operator + num2
sv.set(num1+operator+num2)
def operation(op):
global operator
if op in ['+','-','x','/']:
operator = op
else:
if operator == '+':
sv.set(int(num1)+int(num2))
if operator == '-':
sv.set(int(num1)-int(num2))
if operator == 'x':
sv.set(int(num1)*int(num2))
if operator == '/':
sv.set(int(num1)/int(num2))
#定义第二个面板
frame_bord = Frame(width=300, height=350, bg="#cccccc")
b_del = Button(frame_bord, text="←", width=5, height=1, command=delete)
b_del.grid(row=0, column=0)#网格布局
#设计功能键
b_clear = Button(frame_bord, text="C", width=5, height=1, command=clear)
b_clear.grid(row=0, column=1)
b_fan = Button(frame_bord, text="±", width=5, height=1, command=fan)
b_fan.grid(row=0, column=2)
b_ce = Button(frame_bord, text="CE", width=5, height=1, command=ce)
b_ce.grid(row=0, column=3)
#设计数字键
b_1 = Button(frame_bord, text="1", width=5, height=2,command=lambda:change("1")).grid(row=1, column=0)
b_2 = Button(frame_bord, text="2", width=5, height=2,command=lambda:change("2")).grid(row=1, column=1)
b_3 = Button(frame_bord, text="3", width=5, height=2,command=lambda:change("3")).grid(row=1, column=2)
b_4 = Button(frame_bord, text="4", width=5, height=2,command=lambda:change("4")).grid(row=1, column=3)
b_5 = Button(frame_bord, text="5", width=5, height=2,command=lambda:change("5")).grid(row=2, column=0)
b_6 = Button(frame_bord, text="6", width=5, height=2,command=lambda:change("6")).grid(row=2, column=1)
b_7 = Button(frame_bord, text="7", width=5, height=2,command=lambda:change("7")).grid(row=2, column=2)
b_8 = Button(frame_bord, text="8", width=5, height=2,command=lambda:change("8")).grid(row=2, column=3)
b_9 = Button(frame_bord, text="9", width=5, height=2,command=lambda:change("9")).grid(row=3, column=0)
b_0 = Button(frame_bord, text="0", width=5, height=2,command=lambda:change("0")).grid(row=3, column=1)
#运算符号
b_jia = Button(frame_bord, text="+", width=5, height=2,command=lambda:operation("+")).grid(row=3, column=2)
b_jian = Button(frame_bord, text="-", width=5, height=2,command=lambda:operation("-")).grid(row=3, column=3)
b_cheng = Button(frame_bord, text="x", width=5, height=2,command=lambda:operation("x")).grid(row=4, column=0)
b_chu = Button(frame_bord, text="/", width=5, height=2,command=lambda:operation("/")).grid(row=4, column=1)
b_deng = Button(frame_bord, text="=", width=5, height=2,command=lambda:operation("=")).grid(row=4, column=2)
frame_bord.pack(padx=10, pady=10)
root.mainloop()