首页 > 编程 > Python > 正文

Python简易计算器制作方法代码详解

2019-11-25 11:30:21
字体:
来源:转载
供稿:网友

主要用到的工具是Python中的Tkinter库

比较简单

直接上图形界面和代码

在这里插入图片描述

引用Tkinter库

from tkinter import *

建立主窗口对象

window=Tk() #设置窗口对象window.title('counting machine')window.geometry("350x280")window['bg']='red'

建立标签框以及标签(将运算字符串显示在上面)

frame=LabelFrame(window,bg='yellow',width=350,height=50)frame.pack()frame.place(x=0,y=0)label=Label(frame,text="1+1=2",height=3,width=50,bg='yellow')label.pack() #显示框

设置全局变量字符串s,按一个按钮,将按钮对应的运算符加到这个字符串s中,最后利用eval函数进行计算。

global ss=""

按钮0-9以及小数点的实现(大致思路都是一样的)

#按钮.def figure_dot():  global s  s=s+"."  label.config(text=s)btn0=Button(window,text=".",width=4,command=figure_dot,bg='yellow')btn0.place(x=150,y=220) #按钮.#按钮0def figure_0():  global s  s=s+"0"  label.config(text=s)btn0=Button(window,text="0",width=4,command=figure_0,bg='yellow')btn0.place(x=80,y=220) #按钮0#按钮1def figure_1():  global s  s=s+"1"  label.config(text=s)btn1=Button(window,text="1",width=4,command=figure_1,bg='yellow')btn1.place(x=10,y=80) #按钮1#按钮2def figure_2():  global s  s=s+"2"  label.config(text=s)btn2=Button(window,text="2",width=4,command=figure_2,bg='yellow')btn2.place(x=80,y=80)#按钮2#按钮3def figure_3():  global s  s=s+"3"  label.config(text=s)btn3=Button(window,text="3",width=4,command=figure_3,bg='yellow')btn3.place(x=150,y=80)#按钮3#按钮4def figure_4():  global s  s=s+"4"  label.config(text=s)btn4=Button(window,text="4",width=4,command=figure_4,bg='yellow')btn4.place(x=10,y=130)#按钮4#按钮5def figure_5():  global s  s=s+"5"  label.config(text=s)btn5=Button(window,text="5",width=4,command=figure_5,bg='yellow')btn5.place(x=80,y=130)#按钮5#按钮6def figure_6():  global s  s=s+"6"  label.config(text=s)btn6=Button(window,text="6",width=4,command=figure_6,bg='yellow')btn6.place(x=150,y=130)#按钮6#按钮7def figure_7():  global s  s=s+"7"  label.config(text=s)btn7=Button(window,text="7",width=4,command=figure_7,bg='yellow')btn7.place(x=10,y=180)#按钮7#按钮8def figure_8():  global s  s=s+"8"  label.config(text=s)btn8=Button(window,text="8",width=4,command=figure_8,bg='yellow')btn8.place(x=80,y=180)#按钮8#按钮9def figure_9():  global s  s=s+"9"  label.config(text=s)btn9=Button(window,text="9",width=4,command=figure_9,bg='yellow')btn9.place(x=150,y=180)#按钮9运算符号的实现(±*/)#加法按钮def figure_addition():  global s  s=s+"+"  label.config(text=s)btn_add=Button(window,text="+",width=4,command=figure_addition,bg='yellow')btn_add.place(x=220,y=80)#加法按钮#减法按钮def figure_subtraction():  global s  s=s+"-"  label.config(text=s)btn_sub=Button(window,text="-",width=4,command=figure_subtraction,bg='yellow')btn_sub.place(x=220,y=130)#减法按钮#乘法按钮def figure_multiplication():  global s  s=s+"*"  label.config(text=s)btn_multi=Button(window,text="*",width=4,command=figure_multiplication,bg='yellow')btn_multi.place(x=290,y=80)#乘法按钮#除法按钮def figure_division():  global s  s=s+"/"  label.config(text=s)btn_divi=Button(window,text="/",width=4,command=figure_division,bg='yellow')btn_divi.place(x=290,y=130)#除法按钮

清空窗口按钮的实现

#清空按钮def figure_clear():  global s  s=""  label.config(text=s)btn_clear=Button(window,text="clear",width=4,command=figure_clear,bg='yellow')btn_clear.place(x=220,y=180)#清空按钮

结果输出的实现(eval函数)

#结果按钮def figure_value():  global s  x=eval(s)  s=str(x)  label.config(text=s)btn_value=Button(window,text="=",width=4,command=figure_value,bg='yellow')btn_value.place(x=290,y=180)

颜色变换的实现(红变粉)

def figure_colorchange():  window.config(bg="pink")btn_value=Button(window,text="color",width=4,command=figure_colorchange,bg='yellow')btn_value.place(x=10,y=220)#改变颜色window.mainloop()

变换后

在这里插入图片描述

这个简易计算器也就实现了,当然也可以加入其他的功能,如开方,乘幂等功能。

总结

以上所述是小编给大家介绍的Python简易计算器制作方法代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对武林网网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表