龙空技术网

使用Python的tkinter库创建计算器

厚积薄发爱python 372

前言:

如今同学们对“python编写一个简单计算器程序”可能比较珍视,看官们都想要剖析一些“python编写一个简单计算器程序”的相关内容。那么小编也在网摘上汇集了一些对于“python编写一个简单计算器程序””的相关内容,希望看官们能喜欢,兄弟们一起来了解一下吧!

要求:实现一个跟电脑计算器一样功能的计算器,输入数字和符号,按=号后自动计算结果。

代码如下:

import tkinter as tkroot=tk.Tk()root.title("计算器")  #设置窗体标题是“计算器”root.geometry("285x280+100+100")   #设置窗体的页面大小root.attributes('-topmost',True)   #设置窗体置顶显示root.attributes("-alpha",0.9)    #设置不透明度为0.9root["background"]="#ffffff"   #设置背景颜色result_num=tk.StringVar()result_num.set('')font=('宋体',20)font_16=('宋体',16)color1='#b1b2b2'color2='#eacda1'tk.Label(root,textvariable=result_num,font=font,height=2,width=20,justify=tk.LEFT,anchor=tk.SE).grid(row=1,column=1,columnspan=4)#设置清除按钮,在root窗体中,文本是c,宽度是5,字体是16号宋体,button_clear=tk.Button(root,text='C',width=5,font=font_16,relief=tk.FLAT,bg=color1)#设置回退按钮button_back=tk.Button(root,text="←",width=5,font=font_16,relief=tk.FLAT,bg=color1)button_division=tk.Button(root,text="/",width=5,font=font_16,relief=tk.FLAT,bg=color1)button_multiplication=tk.Button(root,text='x',width=5,font=font_16,relief=tk.FLAT,bg=color1)#设置清除按钮的位置,行号是2,列号是1,左边距是3,上边距是2button_clear.grid(row=2,column=1,padx=3,pady=2)button_back.grid(row=2,column=2,padx=3,pady=2)button_division.grid(row=2,column=3,padx=3,pady=2)button_multiplication.grid(row=2,column=4,padx=3,pady=2)#设置7,8,9,-button_7=tk.Button(root,text='7',width=5,font=font_16,relief=tk.FLAT,bg=color2)button_8=tk.Button(root,text='8',width=5,relief=tk.FLAT,font=font_16,bg=color2)button_9=tk.Button(root,text='9',width=5,relief=tk.FLAT,font=font_16,bg=color2)button_minus=tk.Button(root,text='-',width=5,font=font_16,relief=tk.FLAT,bg=color1)button_7.grid(row=3,column=1,padx=3,pady=2)button_8.grid(row=3,column=2,padx=3,pady=2)button_9.grid(row=3,column=3,padx=3,pady=2)button_minus.grid(row=3,column=4,padx=3,pady=2)#设置4,5,6,+button_4=tk.Button(root,text='4',width=5,relief=tk.FLAT,font=font_16,bg=color2)button_5=tk.Button(root,text='5',width=5,relief=tk.FLAT,font=font_16,bg=color2)button_6=tk.Button(root,text='6',width=5,relief=tk.FLAT,font=font_16,bg=color2)button_plus=tk.Button(root,text='+',width=5,relief=tk.FLAT,font=font_16,bg=color1)button_4.grid(row=4,column=1,padx=3,pady=2)button_5.grid(row=4,column=2,padx=3,pady=2)button_6.grid(row=4,column=3,padx=3,pady=2)button_plus.grid(row=4,column=4,padx=3,pady=2)#设置1,2,3,=button_1=tk.Button(root,text='1',width=5,relief=tk.FLAT,font=font_16,bg=color2)button_2=tk.Button(root,text='2',width=5,relief=tk.FLAT,font=font_16,bg=color2)button_3=tk.Button(root,text='3',width=5,relief=tk.FLAT,font=font_16,bg=color2)button_equal=tk.Button(root,text='=',width=5,height=3,relief=tk.FLAT,font=font_16,bg=color1)button_1.grid(row=5,column=1,padx=3,pady=2)    #设置按钮1,即数字键1的位置,行号设置为5,列号设置为1,左边距为3,上边距为2button_2.grid(row=5,column=2,padx=3,pady=2)button_3.grid(row=5,column=3,padx=3,pady=2)button_equal.grid(row=5,column=4,padx=3,pady=2,rowspan=2)#设置0,.button_0=tk.Button(root,text='0',width=12,relief=tk.FLAT,font=font_16,bg=color2)# button_zero=tk.Button(root,text='0',width=5,relief=tk.FLAT,font=font_16,bg=color2)button_dot=tk.Button(root,text='.',width=5,relief=tk.FLAT,font=font_16,bg=color2)# button_equal2=tk.Button(root,text='=',width=5,relief=tk.FLAT,font=font_16,bg=color1)button_0.grid(row=6,column=1,padx=3,pady=2,columnspan=2)# button_zero.grid(row=6,column=2,padx=3,pady=2)button_dot.grid(row=6,column=3,padx=3,pady=2)# button_equal2.grid(row=6,column=4,padx=3,pady=2)#点击事件def click_button(x):    print("X:\t",x)    result_num.set(result_num.get()+x)#设置计算函数def calculate():    opt_str=result_num.get()    result=eval(opt_str)    result_num.set(str(result))button_1.config(command=lambda:click_button('1'))button_2.config(command=lambda:click_button('2'))button_3.config(command=lambda :click_button('3'))button_4.config(command=lambda :click_button('4'))button_5.config(command=lambda :click_button('5'))button_6.config(command=lambda:click_button('6'))button_7.config(command=lambda:click_button('7'))button_8.config(command=lambda:click_button('8'))button_9.config(command=lambda :click_button('9'))button_0.config(command=lambda:click_button('0'))button_division.config(command=lambda:click_button('/'))button_plus.config(command=lambda :click_button('+'))button_minus.config(command=lambda :click_button('-'))button_multiplication.config(command=lambda :click_button('*'))button_equal.config(command=calculate)   #当点击=按钮时,执行计算函数root.mainloop()

运行结果如下:

标签: #python编写一个简单计算器程序