python四则运算器tkinter_Python GUI(tkinter) 25行代码设计简易计算器-Go语言中文社区...

本文介绍了如何使用Python的Tkinter库设计一个简单的四则运算计算器,包括加、减、乘、除功能。通过定义计算函数和按钮事件,实现了输入、计算和结果显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设计一个简易的四则运算计算器,实现+,-,*,/功能。

代码部分

from tkinter import *

root = Tk(className = '计算器')

root.geometry('230x100')

#设置输入输出框

content1 = StringVar()

content2 = StringVar()

label1 = Label(root,text="输入:").grid(row = 0,column = 0)

entry1 = Entry(root,textvariable=content1,width=20,justify=RIGHT)

entry1.grid(row = 0,column = 1)

label2 = Label(root,text="结果:").grid(row = 1,column = 0)

entry2 = Entry(root,textvariable=content2,width=20,justify=RIGHT)

entry2.grid(row = 1,column = 1)

#定义计算函数

def equ():

entry2.delete(0,END)

entry2.insert(END,str(eval(entry1.get())))

#设置算数按钮

Button(root,text="+",command = lambda *add:entry1.insert(END,'+')).place(x = 10,y = 50)

Button(root,text="-",command = lambda *sub:entry1.insert(END,'-')).place(x = 40,y = 50)

Button(root,text="*",command = lambda *mul:entry1.insert(END,'*')).place(x = 70,y = 50)

Button(root,text="/",command = lambda *div:entry1.insert(END,'/')).place(x = 100,y = 50)

Button(root,text="()",command = lambda *bra:entry1.insert(END,'()')).place(x = 130,y = 50)

Button(root,text="C",command = lambda *clear:entry1.delete(0,END)).place(x = 160,y = 50)

Button(root,text="=",command = equ).place(x = 190,y= 50)

root.mainloop()

结果示例

a0f26162904b36de2e678d41ae7e7922.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值