from tkinter import *
root = Tk()
root.title("Small Calculator")
root.geometry('500x300')
#第一个数
label1 = Label(text="The First Number", width=20, height=2)
label1.pack()
entry1 = Entry(width=20)
entry1.pack()
#运算法则
label2 = Label(text="The Operator", width=20, height=2)
label2.pack()
entry2 = Entry(width=20)
entry2.pack()
#第二个数
label3 = Label(text="The Second Number", width=20, height=2)
label3.pack()
entry3 = Entry(width=20)
entry3.pack()
#运算结果
var4 = IntVar()
label4 = Label(text="The Solution", width=20, height=2)
label4.pack()
label5 = Label(textvar=var4, width=30, height=1,)
label5.pack()
#主函数
def clickFun():
global var4
va11 = int(entry1.get()) #获得第一个数
var2 = entry2.get() #获得运算法则
var3 = int(entry3.get()) #获得第二个数
if var2 == '+':
var4.set(int(va11+var3))
elif var2 == '-':
var4.set(int(va11-var3))
elif var2 == '*':
var4.set(int(va11*var3))
elif var2 == '/':
if var3 == 0:
var4.set("请重新输入数据") #除法判断分母是否为0
else:
var4.set(int(va11/var3))
else:
var4.set("运算符出错,请重新输入数据")
button1 = Button(root, text="Deal", width=10, height=2, command=clickFun) #Deal按钮
button1.pack(side=LEFT)
root.mainloop()
在原程序的“Deal”按钮中弹出一个提示框(MessageBox),提示“是否进行计算”,点击“确定”时进行相应计算,点击“取消”则不计算。
from tkinter import *
from tkinter import messagebox as msgbox
root = Tk()
root.title("Small Calculator")
root.geometry('500x300')
label1 = Label(text="The First Number", width=20, height=2)
label1.pack()
entry1 = Entry(width=20)
entry1.pack()
label2 = Label(text="The Operator", width=20, height=2)
label2.pack()
entry2 = Entry(width=20)
entry2.pack()
label3 = Label(text="The Second Number", width=20, height=2)
label3.pack()
entry3 = Entry(width=20)
entry3.pack()
var4 = IntVar()
label4 = Label(text="The Solution", width=20, height=2)
label4.pack()
label5 = Label(textvar=var4, width=30, height=1,)
label5.pack()
def clickFun():
global var4
va11 = int(entry1.get())
var2 = entry2.get()
var3 = int(entry3.get())
if var2 == '+':
var4.set(int(va11+var3))
elif var2 == '-':
var4.set(int(va11-var3))
elif var2 == '*':
var4.set(int(va11*var3))
elif var2 == '/':
if var3 == 0:
var4.set("请重新输入数据")
else:
var4.set(int(va11/var3))
else:
var4.set("运算符出错,请重新输入数据")
def btn_clicked():
if msgbox.askokcancel('提示', '要执行此操作吗'):
clickFun()
button1 = Button(root, text="Deal", width=10, height=2, command=btn_clicked)
button1.pack(side=LEFT)
root.mainloop()
在原程序中增加“+”“-”“×”“÷”四个按钮(Button),点击相应Button时,实现相应的数学计算
from tkinter import *
from tkinter import messagebox as msgbox
root = Tk()
root.title("Small Calculator")
root.geometry('500x300')
label1 = Label(text="The First Number", width=20, height=2)
label1.pack()
entry1 = Entry(width=20)
entry1.pack()
label3 = Label(text="The Second Number", width=20, height=2)
label3.pack()
entry3 = Entry(width=20)
entry3.pack()
var4 = IntVar()
label4 = Label(text="The Solution", width=20, height=2)
label4.pack()
label5 = Label(textvar=var4, width=30, height=1)
label5.pack()
def clickFun(var2):
global var4
va11 = int(entry1.get())
var3 = int(entry3.get())
if var2 == '+':
var4.set(int(va11+var3))
elif var2 == '-':
var4.set(int(va11-var3))
elif var2 == '*':
var4.set(int(va11*var3))
elif var2 == '/':
if var3 == 0:
var4.set("请重新输入数据")
else:
var4.set(int(va11/var3))
else:
var4.set("运算符出错,请重新输入数据")
def btn1_clicked():
if msgbox.askokcancel('提示', '要执行此操作吗'):
clickFun('+')
def btn2_clicked():
if msgbox.askokcancel('提示', '要执行此操作吗'):
clickFun( '-')
def btn3_clicked():
if msgbox.askokcancel('提示', '要执行此操作吗'):
clickFun('*')
def btn4_clicked():
if msgbox.askokcancel('提示', '要执行此操作吗'):
clickFun('/')
button1 = Button(root, text="add", width=17, height=2, command=btn1_clicked)
button1.pack(side=LEFT)
button2 = Button(root, text="sub", width=17, height=2, command=btn2_clicked)
button2.pack(side=LEFT)
button3 = Button(root, text="mul", width=17, height=2, command=btn3_clicked)
button3.pack(side=LEFT)
button4 = Button(root, text="div", width=17, height=2, command=btn4_clicked)
button4.pack(side=LEFT)
在原程序中增加“+”“-”“×”“÷”四个单选按钮(Radiobutton),点击相应Radiobutton时,实现相应的数学计算。
from tkinter import *
root = Tk()
root.title("Small Calculator")
root.geometry('500x300')
label1 = Label(text="The First Number", width=20, height=2)
label1.pack()
entry1 = Entry(width=20)
entry1.pack()
label3 = Label(text="The Second Number", width=20, height=2)
label3.pack()
entry3 = Entry(width=20)
entry3.pack()
var4 = IntVar()
label4 = Label(text="The Solution", width=20, height=2)
label4.pack()
label5 = Label(textvar=var4, width=30, height=1,)
label5.pack()
def clickFun():
global var4
va11 = int(entry1.get())
var3 = int(entry3.get())
a = foo.get()
if a==1:
var4.set(int(va11+var3))
elif a==2:
var4.set(int(va11-var3))
elif a==3:
var4.set(int(va11*var3))
elif a==4:
if var3 == 0:
var4.set("请重新输入数据")
else:
var4.set(int(va11/var3))
else:
var4.set("运算符出错,请重新输入数据")
foo = IntVar()
ls = [('add',1),('sub',2),('mul',3),('div', 4)]
for text ,value in ls:
r = Radiobutton(root, text=text, value=value, command=clickFun, variable=foo)
r.pack(anchor=N)
root.mainloop()