1.使用模块
tkinter
2.代码
import tkinter as tk
window = tk.Tk()
window.title("计算机——Eric开发") #设置窗口标题
window.geometry('300x400') #设置窗口大小长宽
#标签设置提示
l = tk.Label(window,text='请输入第一个数:',width=30,height=2)
l.pack()
#创建计算器的输入框
a1 = tk.Variable() #定义变量
e = tk.Entry(window,textvariable=a1,show=None) #输入框
e.pack()
l2 = tk.Label(window,text='请输入第二个数:',width=30,height=2)
l2.pack()
#创建计算器的输入框
a2 = tk.Variable()
e2 = tk.Entry(window,textvariable=a2,show=None) #输入框
e2.pack()
#回调函数
def funjia():
a = a1.get()
b = a2.get()
data = int(a)+int(b)
text.delete(0.0,"end")
text.insert(tk.INSERT,data)
#回调函数
def funjian():
a = a1.get()
b = a2.get()
data = int(a)-int(b)
text.delete(0.0,"end")
text.insert(tk.INSERT,data)
#回调函数
def funcheng():
a = a1.get()
b = a2.get()
data = int(a)*int(b)
text.delete(0.0,"end")
text.insert(tk.INSERT,data)
#回调函数
def funchu():
a = a1.get()
b = a2.get()
data = int(a)/int(b)
text.delete(0.0,"end")
text.insert(tk.INSERT,data)
#按键添加
btn = tk.Button(window,text="+",command = funjia)
btn.pack()
#按键添加
btn1 = tk.Button(window,text="-",command = funjian)
btn1.pack()
#按键添加
btn2 = tk.Button(window,text="*",command = funcheng)
btn2.pack()
#按键添加
btn3 = tk.Button(window,text="/",command = funchu)
btn3.pack()
#标签设置提示
l = tk.Label(window,text='运算结果:',width=30,height=2)
l.pack()
text = tk.Text(window,width=30,height=2)
text.pack()
window.mainloop()
3.效果
4.tkinter text学习
text