""" 计算器软件的制作 the making of calculator"""
"""使用面向对象的方式写GUI"""
import random
"""Write GUI in an object-oriented manner"""
from tkinter import *
class Application(Frame):
def __init__(self,master=None):
super().__init__(master)
self.master = master
self.pack()
self.createWidget()
def createWidget(self):
""" 通过grid布局实现计算器页面"""
""" calculator pages are implemented through grid layout"""
btnText = (
("mc", "m+", "m-", "mr"),
("c", "+", "/", "x"),
(7, 8, 9, "-"),
(4, 5, 6, "+"),
(1, 2, 3, "="),
(0, ".")
)
Entry(self).grid(row=0, column=0, columnspan=4, pady=10)
for rindex, r in enumerate(btnText):
for cindex, c in enumerate(r):
if c == "=":
Button(self, text=c, width=2).grid(row=rindex+1, column=cindex, rowspan=3, sticky=EW)
elif c == 0:
Button(self, text=c, width=2).grid(row=rindex+1, column=cindex, columnspan=2, sticky=NSEW)
elif c == ".":
Button(self, text=c, width=2).grid(row=rindex + 1, column=cindex+1, sticky=EW)
else:
Button(self, text=c, width=2).grid(row=rindex+1, column=cindex, sticky=EW)
if __name__ == '__main__':
root = Tk()
root.geometry("180x240+200+300")
app = Application(master=root)
root.title('Test for calculator')
root.mainloop()