在原有的计算器基础上,加了一个变颜色的功能,鼠标经过按钮时变换颜色,离开按钮后颜色恢复之前的颜色
源代码奉上:
import tkinter
# 计算
def calculate():
# 计算
# equ.get()获取此时equ内容
# eval 计算
result = eval(equ.get())
equ.set(equ.get() + "=\n" + str(result))
# 将我们的数字打在文本框上
def show(buttonString):
# 获取内容
content = equ.get()
# 判断,如果内容为0的话相当于没计算,所以空字符串拼接,还是0
# 如果内容为10的话,content就为10,再拼接我们所按的数字
if content == "0":
content = ""
equ.set(content + buttonString)
# 清除,置为0
def clear():
equ.set("0")
# 退位
def backSpace():
# 删除前一个字符
equ.set(str(equ.get()[:-1]))
# 更改颜色的方法
def moved(event, i, color):
i.config(bg=color)
# 使函数传递多个参数,lambda只能传递一个参数
def handlerAdaptor(fun, **kwds):
return lambda event, func=fun, kwds=kwds: fun(event, **kwds)
def fun(list):
for i in list:
# 试图获取bg的属性值,
x = i.cget("bg")
# 当鼠标进入控件时,执行moved操作,变颜色
i.bind("<Enter>", handlerAdaptor(moved, i=i, color="lightyellow"))
# 当鼠标移出控件时,执行moved操作,恢复颜色
i.bind("<Leave>", handlerAdaptor(moved, i=i, color=x))
# 主程序
root = tkinter.Tk()
# 设置程序标题
root.title("简易计算器")
# 框体可拖拽,0,0表示不能拖拽
root.resizable(0, 0)
# equ 是存放显示区的数字
equ = tkinter.StringVar()
# 默认为0
equ.set("0")
# 设置文本框
# width 宽为25
# height 高为2
# relief 指
# font指字体这里我们只设置了字体的大小
label = tkinter.Label(root, width=25, height=2, relief="raised", anchor=tkinter.SE, textvariable=equ)
# row 指entry组件在网格中的横向位置
# column 指entry组件在网格中的纵向位置
# columnspan 指设定entry组件在column方向的合并数量
# padx/pady 设定组件边界与容器(可想成窗口边界)的距离或是控件边界间的距离
label.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
# row1的按钮
# 清除显示区按钮AC
buttonClear = tkinter.Button(root, text="AC", width=5, bg="#ff6348", command=clear)
buttonClear.grid(row=1, column=0)
# 退位键按钮
buttonBack = tkinter.Button(root, text="<-", width=5, bg="#ff6348", command=backSpace)
buttonBack.grid(row=1, column=1)
# 余按钮
buttonYu = tkinter.Button(root, text="%", width=5, bg="lightblue", command=lambda: show("%"))
buttonYu.grid(row=1, column=2)
# 除按钮
buttonDivision = tkinter.Button(root, text="/", width=5, bg="lightblue", command=lambda: show("/"))
buttonDivision.grid(row=1, column=3)
# row2的按钮
# 数字7
button7 = tkinter.Button(root, text="7", width=5, bg="orange", command=lambda: show("7"))
button7.grid(row=2, column=0)
# 数字8
button8 = tkinter.Button(root, text="8", width=5, bg="orange", command=lambda: show("8"))
button8.grid(row=2, column=1)
# 数字9
button9 = tkinter.Button(root, text="9", width=5, bg="orange", command=lambda: show("9"))
button9.grid(row=2, column=2)
# 乘法按钮
buttonMultiplication = tkinter.Button(root, text="*", width=5, bg="lightblue", command=lambda: show("*"))
buttonMultiplication.grid(row=2, column=3)
# row3的按钮
# 数字4
button4 = tkinter.Button(root, text="4", width=5, bg="orange", command=lambda: show("4"))
button4.grid(row=3, column=0)
# 数字5
button5 = tkinter.Button(root, text="5", width=5, bg="orange", command=lambda: show("5"))
button5.grid(row=3, column=1)
# 数字6
button6 = tkinter.Button(root, text="6", width=5, bg="orange", command=lambda: show("6"))
button6.grid(row=3, column=2)
# 减法按钮
buttonSubtraction = tkinter.Button(root, text="-", width=5, bg="lightblue", command=lambda: show("-"))
buttonSubtraction.grid(row=3, column=3)
# row4的按钮
# 数字1
button1 = tkinter.Button(root, text="1", width=5, bg="orange", command=lambda: show("1"))
button1.grid(row=4, column=0)
# 数字2
button2 = tkinter.Button(root, text="2", width=5, bg="orange", command=lambda: show("2"))
button2.grid(row=4, column=1)
# 数字3
button3 = tkinter.Button(root, text="3", width=5, bg="orange", command=lambda: show("3"))
button3.grid(row=4, column=2)
# 加法按钮
buttonAdd = tkinter.Button(root, text="+", width=5, bg="lightblue", command=lambda: show("+"))
buttonAdd.grid(row=4, column=3)
# row5的按钮
# 小数点按钮
buttonPoint = tkinter.Button(root, text=".", width=5, bg="orange", command=lambda: show("."))
buttonPoint.grid(row=5, column=0)
# 数字0
button0 = tkinter.Button(root, text="0", width=5, bg="orange", command=lambda: show("0"))
button0.grid(row=5, column=1)
# 等于号
buttonEqual = tkinter.Button(root, text="=", width=12, bg="lightgreen", command=calculate)
buttonEqual.grid(row=5, column=2, columnspan=2)
# 各个选项的变量名
list = [button1, button2, button3, button4, button5, button6, button7, button8, button9, button0,
buttonAdd, buttonSubtraction, buttonMultiplication, buttonDivision, buttonPoint,
buttonEqual, buttonYu, buttonClear, buttonBack]
fun(list)
# 程序循环进行
root.mainloop()
运行结果:
谢谢观看,笔者会持续更新,如有错误或者建议,请私信我