#导入tkinter库
import tkinter as tk
from tkinter import *
#布局窗口
#创建一个tk对象
root = tk.Tk()
#设置宽600,高500,距离上边100,距离左边100
root.geometry("600x500+100+100")
root.title("简易计算器")
#定义一个可变量
result_num = tk.StringVar()
result_num.set(0)#默认值设置为0
#标签(运算数1)
yunsuanshu1 = tk.Label(root, text="运算数1:")
yunsuanshu1.pack(side=LEFT)
#输入框(运算数1)
E1 = Entry(root, bd =5,width=15)
E1.pack(side= LEFT)
yunsuanfu = tk.Label(root, text="运算符:")
yunsuanfu.pack(side=LEFT)
EF = Entry(root, bd =3,width=5)
EF.pack(side = LEFT)
yunsuanshu2 = tk.Label(root, text="运算数2:")
yunsuanshu2.pack(side=LEFT)
E2 = Entry(root, bd =5,width=15)
E2.pack(side = LEFT)
buttonY = tk.Button(root,text="计算", command=lambda:dayin(E1,EF,E2) )
buttonY.pack(side = LEFT)
buttonC = tk.Button(root,text=" 清空", command=lambda:clear_text(E1,EF,E2) )
buttonC.pack(side = LEFT)
jieguo = tk.Label(root, text="结果:")
jieguo.pack(side=LEFT)
w = tk.Label(root,textvariable=result_num)
w.pack(side = LEFT)
def clear_text(e1,ef,e2):
e1.delete(0, 'end')
e2.delete(0, 'end')
ef.delete(0, 'end')
result_num.set(0)
def dayin(e1,ef,e2):
a=e1.get()
op=ef.get()
b=e2.get()
a = eval(a)
b = eval(b)
if op == '+':
ans = a+b
if op == '-':
ans = a-b
if op == '*':
ans = a*b
if op == '/':
ans = a/b
result_num.set(ans)
root.mainloop()
自己编写了一个简单的计算器给学生上课使用
最新推荐文章于 2024-09-15 22:31:42 发布
本文详细描述了如何使用Python的Tkinter库创建一个基本的计算器应用,包括窗口布局、输入框、运算符选择和结果展示,以及处理用户输入和计算功能。
摘要由CSDN通过智能技术生成