# coding=utf-8
"""计算器软件界面设计"""
from tkinter import *
from tkinter import messagebox
class Application(Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.creatWidget()
def creatWidget(self):
"""实现计算器"""
btnText =(("mc","m+","m-","mr"),
("c","±","÷","*"),
(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=="=":
# rowspan 跨行
# columnspan 跨列
Button(self,text=c,width=2).
grid(row=rindex+1,column=cindex,rowspan=2,sticky=NSEW)
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=NSEW)
else:
Button(self,text=c,width=2).
grid(row=rindex+1,column=cindex,sticky=EW)
if __name__=="__main__":
root = Tk()
root.geometry("160x240+100+200")
app = Application(master=root)
root.mainloop()
# 学习grid布局 做了个小的计算器界面
# 体会 grid布局主要是根据坐标的位置来确定具体在哪里填充控件
# 尤其是对于cowspan 和 columnspan的操作使用
# 初学Python 如有不足 请多多指教