计算器合集
- 一:极简版计算器
- 二:简易版计算器
- 三:不简易的计算器
- 四:还可以计算器
一:极简版计算器
运行效果:
import tkinter as tk
import tkinter.messagebox
win = tk.Tk()
win.title("计算器")
win.geometry('650x600')
b=tk.Label(win, text='计算器',justify=tk.CENTER, height=2 , width=50 ,font=('楷体', 20),background='lightcyan')
b.grid(row=0,columnspan=6)
b1=tk.Label(win, text='a:',justify=tk.CENTER, height=2 , width=5 ,font=('楷体', 15))
b1.grid(row=1,column=0)
a1 = tk.Entry(win, justify=tk.LEFT, width=15 , font=('楷体', 15))
a1.grid(row=1,column=1)
b2 = tk.Label(win, text='b:',justify=tk.CENTER, height=2 , width=5 , font=('楷体', 15))
b2.grid(row=2,column=0)
a2 = tk.Entry(win, justify=tk.LEFT, width=15 , font=('楷体', 15) )
a2.grid(row=2,column=1)
txt = tk.Text(win,height=20, width=40 ,font=('楷体', 15))
txt.grid(row=1,column=4,rowspan=4,columnspan=4)
num=0
def sum():
if a1.get()=='' or a2.get()=='':
return tkinter.messagebox.showinfo('消息提示', '输入不能为空')
global num
num += 1
a = float(a1.get())
b = float(a2.get())
c = a + b
s = str('%d'%a + ' + '+'%d'%b + ' = ' + '%d'%c)
if num%2==0:
s=s+ '\n'
print(num)
else:
s=s+'\t'
txt.insert(tk.END,s)
tkinter.messagebox.showinfo('消息提示', '求和结果是:%d'%c)
a1.delete(0, tk.END)
a2.delete(0, tk.END)
def multiply():
if a1.get()=='' or a2.get()=='':
return tkinter.messagebox.showinfo('消息提示', '输入不能为空')
global num
num += 1
a = float(a1.get())
b = float(a2.get())
c = a * b
s = str('%d' % a + ' * ' + '%d' % b + ' = ' + '%d' % c)
if num%2==0:
s=s+ '\n'
print(num)
else:
s=s+'\t'
txt.insert(tk.END, s)
tkinter.messagebox.showinfo('消息提示', '求积结果是:%d'%c)
a1.delete(0, tk.END)
a2.delete(0, tk.END)
def subtract():
if a1.get()=='' or a2.get()=='':
return tkinter.messagebox.showinfo('消息提示', '输入不能为空')
global num
num += 1
a = float(a1.get())
b = float(a2.get())
c = a - b
s = str('%d' % a + ' - ' + '%d' % b + ' = ' + '%d' % c)
if num%2==0:
s=s+ '\n'
print(num)
else:
s=s+'\t'
txt.insert(tk.END, s)
tkinter.messagebox.showinfo('消息提示', '求差结果是:%d'%c)
a1.delete(0, tk.END)
a2.delete(0, tk.END)
def divide():
if a1.get()=='' or a2.get()=='':
return tkinter.messagebox.showinfo('消息提示', '输入不能为空')
global num
num += 1
a = float(a1.get())
b = float(a2.get())
if float(b)==0.0:
tkinter.messagebox.showinfo('消息提示', '除数不能为零!!!')
a2.delete(0 , tk.END)
else:
c = a / b
s = str('%d' % a + ' / ' + '%d' % b + ' = ' + '%d' % c)
if num % 2 == 0:
s = s + '\n'
print(num)
else:
s = s + '\t'
txt.insert(tk.END, s)
tkinter.messagebox.showinfo('消息提示', '求商结果是:%d'%c)
a1.delete(0, tk.END)
a2.delete(0, tk.END)
a4 = tk.Button(win, text='求和',justify=tk.CENTER, width=10, font=('楷体', 15) ,command = lambda :sum() )
a4.grid(row=3, column=0)
a5 = tk.Button(win, text='求积', justify=tk.CENTER, width=10 , font=('楷体', 15), command = lambda :multiply())
a5.grid(row=3, column=1)
a6 = tk.Button(win, text='求差', justify=tk.CENTER, width=10, font=('楷体', 15), command = lambda :subtract())
a6.grid(row=4, column=0)
a7 = tk.Button(win, text='求商', justify=tk.CENTER, width=10, font=('楷体', 15), command = lambda :divide())
a7.grid(row=4, column=1)
win.mainloop()
二:简易版计算器
运行效果:
import tkinter as tk
class Calculator(tk.Tk):
def __init__(self):
super().__init__()
self.title("Calculator")
self.geometry("400x400")
self.result_var = tk.StringVar()
self.result_var.set("")
self.build_ui()
def build_ui(self):
# 创建输入框
entry = tk.Entry(self, textvariable=self.result_var, font=("Arial", 24), bd=10, insertbackground="white", width=14, justify="right")
entry.grid(row=0, column=0, columnspan=4)
# 创建按钮
self.create_button("7", 1, 0)
self.create_button("8", 1, 1)
self.create_button("9", 1, 2)
self.create_button("÷", 1, 3)
self.create_button("4", 2, 0)
self.create_button("5", 2, 1)
self.create_button("6", 2, 2)
self.create_button("×", 2, 3)
self.create_button("1", 3, 0)
self.create_button("2", 3, 1)
self.create_button("3", 3, 2)
self.create_button("-", 3, 3)
self.create_button("C", 4, 0)
self.create_button("0", 4, 1)
self.create_button("=", 4, 2)
self.create_button("+", 4, 3)
def create_button(self, text, row, column):
button = tk.Button(self, text=text, font=("Arial", 18), width=5, height=2, command=lambda: self.on_button_click(text))
button.grid(row=row, column=column)
def on_button_click(self, text):
if text == "C":
self.result_var.set("")
elif text == "=":
try:
result = eval(self.result_var.get().replace("×", "*").replace("÷", "/"))
self.result_var.set(result)
except Exception as e:
self.result_var.set("Error")
else:
current_value = self.result_var.get()
self.result_var.set(current_value + text)
if __name__ == "__main__":
calculator = Calculator()
calculator.mainloop()
三:不简易的计算器
运行效果:
import tkinter as tk
class Calc(tk.Tk):
"""计算器窗体类"""
def __init__(self):
"""初始化实例"""
tk.Tk.__init__(self)
self.title("计算器")
self.memory = 0 # 暂存数值
self.create()
def create(self):
"""创建界面"""
btn_list = ["C", "M->", "->M", "/",
"7", "8", "9", "*",
"4", "5", "6", "-",
"1", "2", "3", "+",
"+/-", "0", ".", "="]
r = 1
c = 0
for b in btn_list:
self.button = tk.Button(self, text=b, width=5,
command=(lambda x=b: self.click(x)))
self.button.grid(row=r, column=c, padx=3, pady=6)
c += 1
if c > 3:
c = 0
r += 1
self.entry = tk.Entry(self, width=24, borderwidth=2,
bg="yellow", font=("Consolas", 12))
self.entry.grid(row=0, column=0, columnspan=4, padx=8, pady=6)
def click(self, key):
"""响应按钮"""
if key == "=": # 输出结果
result = eval(self.entry.get())
self.entry.insert(tk.END, " = " + str(result))
elif key == "C": # 清空输入框
self.entry.delete(0, tk.END)
elif key == "->M": # 存入数值
self.memory = self.entry.get()
if "=" in self.memory:
ix = self.memory.find("=")
self.memory = self.memory[ix + 2:]
self.title("M=" + self.memory)
elif key == "M->": # 取出数值
if self.memory:
self.entry.insert(tk.END, self.memory)
elif key == "+/-": # 正负翻转
if "=" in self.entry.get():
self.entry.delete(0, tk.END)
elif self.entry.get()[0] == "-":
self.entry.delete(0)
else:
self.entry.insert(0, "-")
else: # 其他键
if "=" in self.entry.get():
self.entry.delete(0, tk.END)
self.entry.insert(tk.END, key)
if __name__ == "__main__":
Calc().mainloop()
四:还可以计算器
运行效果:
import tkinter as tk
t = ""
color1 = "skyblue"
color2 = "yellow"
color3 = "pink"
color4 = "red"
def Add(num):
global t
t = t + str(num)
result.set(t)
def Result():
try:
global t
total = str(eval(t))
result.set(total)
t = ""
except:
result.set(" error ")
t = ""
def Clear():
global t
t = ""
result.set("")
root = tk.Tk()
root.configure(background="black")
root.title("还可以计算器")
sh = root.winfo_screenheight()
sw = root.winfo_screenwidth()
h = 150
w = 295
x = (sw - w) // 2
y = (sh - h) // 2
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
result = tk.StringVar()
entrys = tk.Entry(root, textvariable=result, width=22)
entrys.grid(columnspan=4, ipadx=70)
button1 = tk.Button(root, text=' 1 ', bg=color1, command=lambda: Add(1), height=1, width=7)
button1.grid(row=2, column=0)
button2 = tk.Button(root, text=' 2 ', bg=color1, command=lambda: Add(2), height=1, width=7)
button2.grid(row=2, column=1)
button3 = tk.Button(root, text=' 3 ', bg=color1, command=lambda: Add(3), height=1, width=7)
button3.grid(row=2, column=2)
button4 = tk.Button(root, text=' 4 ', bg=color1, command=lambda: Add(4), height=1, width=7)
button4.grid(row=3, column=0)
button5 = tk.Button(root, text=' 5 ', bg=color1, command=lambda: Add(5), height=1, width=7)
button5.grid(row=3, column=1)
button6 = tk.Button(root, text=' 6 ', bg=color1, command=lambda: Add(6), height=1, width=7)
button6.grid(row=3, column=2)
button7 = tk.Button(root, text=' 7 ', bg=color1, command=lambda: Add(7), height=1, width=7)
button7.grid(row=4, column=0)
button8 = tk.Button(root, text=' 8 ', bg=color1, command=lambda: Add(8), height=1, width=7)
button8.grid(row=4, column=1)
button9 = tk.Button(root, text=' 9 ', bg=color1, command=lambda: Add(9), height=1, width=7)
button9.grid(row=4, column=2)
button0 = tk.Button(root, text=' 0 ', bg=color1, command=lambda: Add(0), height=1, width=7)
button0.grid(row=5, column=0)
add = tk.Button(root, text=' + ', bg=color2, command=lambda: Add("+"), height=1, width=7)
add.grid(row=2, column=3)
subtract = tk.Button(root, text=' - ', bg=color2, command=lambda: Add("-"), height=1, width=7)
subtract.grid(row=3, column=3)
multiply = tk.Button(root, text=' * ', bg=color2, command=lambda: Add("*"), height=1, width=7)
multiply.grid(row=4, column=3)
divide = tk.Button(root, text=' / ', bg=color2, command=lambda: Add("/"), height=1, width=7)
divide.grid(row=5, column=3)
equal = tk.Button(root, text=' = ', bg=color3, command=Result, height=1, width=7)
equal.grid(row=5, column=2)
clear = tk.Button(root, text='Clear', bg=color4, command=Clear, height=1, width=7)
clear.grid(row=5, column=1)
root.mainloop()