mport tkinter as tk
def jf():
entry2.insert(0," ")
v3=float(v1.get())+float(v2.get())
entry2.insert(0,v3)
def j1f():
entry2.insert(0," ")
v3=float(v1.get())-float(v2.get())
entry2.insert(0,v3)
def cf():
entry2.insert(0," ")
v3=float(v1.get())*float(v2.get())
entry2.insert(0,v3)
def c1f():
entry2.insert(0," ")
v3=float(v1.get())/float(v2.get())
entry2.insert(0,v3)
jsq=tk.Tk()
jsq.title("简易计算器")
frame=tk.Frame(jsq)
frame1=tk.Frame(jsq)
frame2=tk.Frame(jsq)
la1=tk.Label(frame,text="欢迎使用",font=("仿宋",50),fg="black")
la2=tk.Label(frame,text="请选择功能",font=("仿宋",20),fg="black")
v3=tk.StringVar()
v1=tk.StringVar()
v2=tk.StringVar()
entry=tk.Entry(frame,textvariable=v1)
entry1=tk.Entry(frame,textvariable=v2)
entry2=tk.Entry(frame)
la3=tk.Label(frame,text="第一个数据",font=("仿宋",20),fg="black")
la4=tk.Label(frame,text="第二个数据",font=("仿宋",20),fg="black")
la5=tk.Label(frame,text="结果是",font=("仿宋",20),fg="black")
la1.pack()
la3.pack()
entry.pack()
la4.pack()
entry1.pack()
la5.pack()
entry2.pack()
button1=tk.Button(frame,text="加法",command=jf)
button2=tk.Button(frame,text="减法",command=j1f)
button3=tk.Button(frame,text="乘法",command=cf)
button4=tk.Button(frame,text="除法",command=c1f)
frame.pack(padx=40,pady=40)
la2.pack()
button1.pack()
button2.pack()
button3.pack()
button4.pack()
jsq.mainloop()
话不多说先放源码,主要涉及的label和entry,还有button的GUI界面组成
简易计算机执行逻辑,
执行的GUI界面如图所示
运行的逻辑是对输入框输入要计算的数值,
结果会自动出现
但是我们万一想保存计算器的日志可以在函数中插入写入文件
j=open("1.txt",mode="a")
j.write("\n")
j.write(str(v1.get())+"/"+str(v2.get()))
j.write("=")
j.write(str(v3))
j.close()
当我们继续进行计算时,我们计算的日志可以保存在该文件目录下
全代码
import tkinter as tk
import os
def jf():
entry2.delete(0,"end")
v3=float(v1.get())+float(v2.get())
entry2.insert(0,v3)
j=open("1.txt",mode="a")
j.write("\n")
j.write(str(v1.get())+"+"+str(v2.get()))
j.write("=")
j.write(str(v3))
j.close()
def j1f():
entry2.delete(0,"end")
v3=float(v1.get())-float(v2.get())
entry2.insert(0,v3)
j=open("1.txt",mode="a")
j.write("\n")
j.write(str(v1.get())+"-"+str(v2.get()))
j.write("=")
j.write(str(v3))
j.close()
def cf():
entry2.delete(0,"end")
v3=float(v1.get())*float(v2.get())
entry2.insert(0,v3)
j=open("1.txt",mode="a")
j.write("\n")
j.write(str(v1.get())+"*"+str(v2.get()))
j.write("=")
j.write(str(v3))
j.close()
def c1f():
entry2.delete(0,"end")
v3=float(v1.get())/float(v2.get())
entry2.insert(0,v3)
j=open("1.txt",mode="a")
j.write("\n")
j.write(str(v1.get())+"/"+str(v2.get()))
j.write("=")
j.write(str(v3))
j.close()
jsq=tk.Tk()
jsq.title("简易计算器")
frame=tk.Frame(jsq)
frame1=tk.Frame(jsq)
frame2=tk.Frame(jsq)
la1=tk.Label(frame,text="欢迎使用",font=("仿宋",50),fg="black")
la2=tk.Label(frame,text="请选择功能",font=("仿宋",20),fg="black")
v3=tk.StringVar()
v1=tk.StringVar()
v2=tk.StringVar()
entry=tk.Entry(frame,textvariable=v1)
entry1=tk.Entry(frame,textvariable=v2)
entry2=tk.Entry(frame)
la3=tk.Label(frame,text="第一个数据",font=("仿宋",20),fg="black")
la4=tk.Label(frame,text="第二个数据",font=("仿宋",20),fg="black")
la5=tk.Label(frame,text="结果是",font=("仿宋",20),fg="black")
la1.pack()
la3.pack()
entry.pack()
la4.pack()
entry1.pack()
la5.pack()
entry2.pack()
button1=tk.Button(frame,text="加法",command=jf)
button2=tk.Button(frame,text="减法",command=j1f)
button3=tk.Button(frame,text="乘法",command=cf)
button4=tk.Button(frame,text="除法",command=c1f)
frame.pack(padx=40,pady=40)
la2.pack()
button1.pack()
button2.pack()
button3.pack()
button4.pack()
jsq.mainloop()