使用tkinter+面对对象实现计算器功能
题目要求可以看出这是最简单的计算器,甚至连除法都没有,所以这是最最基本的计算器,进行进行计算后也无法清零
博主格外吧除法添加上去,利用宽为两格的0按钮
import tkinter as tk
class jisuanqi:
def TK(self):
self.jisuan = []
self.win = tk.Tk()
self.win.title('计算器')
self.numStringvar = tk.StringVar()
self.numEntry = tk.Entry(self.win, textvariable=self.numStringvar, width=30)
self.numEntry.grid(row=0, column=0, columnspan=4)
self.one_button = tk.Button(self.win, text='1', width=5, command=self.one).grid(row=3, column=0, pady=5)
self.two_button = tk.Button(self.win, text='2', width=5, command=self.two).grid(row=3, column=1, pady=5)
self.three_button = tk.Button(self.win, text='3', width=5, command=self.three).grid(row=3, column=2, pady=5)
self.four_button = tk.Button(self.win, text='4', width=5, command=self.four).grid(row=2, column=0, pady=5)
self.five_button = tk.Button(self.win, text='5', width=5, command=self.five).grid(row=2, column=1, pady=5)
self.six_button = tk.Button(self.win, text='6', width=5, command=self.six).grid(row=2, column=2, pady=5)
self.seven_button = tk.Button(self.win, text='7', width=5, command=self.seven).grid(row=1, column=0, pady=5)
self.eight_button = tk.Button(self.win, text='8', width=5, command=self.eight).grid(row=1, column=1, pady=5)
self.nine_button = tk.Button(self.win, text='9', width=5, command=self.nine).grid(row=1, column=2, pady=5)
self.zero_button = tk.Button(self.win, text='0', width=5, command=self.zero).grid(row=4, column=1, pady=5)
self.point_button = tk.Button(self.win, text='.', width=5, command=self.point).grid(row=4, column=0, pady=5)
self.And_button = tk.Button(self.win, text='+', width=5, command=self.And).grid(row=4, column=3, pady=5)
self.Less_button = tk.Button(self.win, text='-', width=5, command=self.Less).grid(row=3, column=3, pady=5)
self.Multiplication_button = tk.Button(self.win, text='*', width=5, command=self.Multiplication).grid(row=2,
column=3,
pady=5)
self.Division_button = tk.Button(self.win, text='/', width=5, command=self.Division).grid(row=1, column=3,
pady=5)
self.Equal_button = tk.Button(self.win, text='=', width=5, command=self.Equal).grid(row=4, column=2, pady=5)
self.win.mainloop()
def one(self):
self.numStringvar.set(self.numStringvar.get() + '1')
def two(self):
self.numStringvar.set(self.numStringvar.get() + '2')
def three(self):
self.numStringvar.set(self.numStringvar.get() + '3')
def four(self):
self.numStringvar.set(self.numStringvar.get() + '4')
def five(self):
self.numStringvar.set(self.numStringvar.get() + '5')
def six(self):
self.numStringvar.set(self.numStringvar.get() + '6')
def seven(self):
self.numStringvar.set(self.numStringvar.get() + '7')
def eight(self):
self.numStringvar.set(self.numStringvar.get() + '8')
def nine(self):
self.numStringvar.set(self.numStringvar.get() + '9')
def zero(self):
self.numStringvar.set(self.numStringvar.get() + '0')
def point(self):
self.numStringvar.set(self.numStringvar.get() + '.')
def And(self):
self.jisuan.append(self.numStringvar.get())
self.jisuan.append('+')
self.numStringvar.set('')
def Less(self):
self.jisuan.append(self.numStringvar.get())
self.jisuan.append('-')
self.numStringvar.set('')
def Multiplication(self):
self.jisuan.append(self.numStringvar.get())
self.jisuan.append('*')
self.numStringvar.set('')
def Division(self):
self.jisuan.append(self.numStringvar.get())
self.jisuan.append('/')
self.numStringvar.set('')
def Equal(self):
if self.jisuan[1] == '+':
self.numStringvar.set(float(self.jisuan[0]) + float(self.numStringvar.get()))
elif self.jisuan[1] == '-':
self.numStringvar.set(float(self.jisuan[0]) - float(self.numStringvar.get()))
elif self.jisuan[1] == '*':
self.numStringvar.set(float(self.jisuan[0]) * float(self.numStringvar.get()))
elif self.jisuan[1] == '/':
self.numStringvar.set(float(self.jisuan[0]) / float(self.numStringvar.get()))
a = jisuanqi()
a.TK()