一、成品图
二、知识点
1. 窗口的创建
from tkinter import *
#初始化Tk()
myWindow = Tk()
myWindow.title('第一个tkinter窗口')
#进入消息循环(这句不能漏,否则窗口不显示)
myWindow.mainloop()
2. 控件介绍
控件具体使用可参考:https://www.cnblogs.com/pywjh/p/9527828.html#jhgl
3.控件管理
利用框架对控件布局,布局方式如下三种
pack()属简单布局,grid()和place()布局运用于较为复杂场景。
三、代码
# -*- coding:utf-8 -*-
#@Time :2021/2/26 16:24
#@Author :Came Deng
#@Project :practical
#@File :calculator
#@description: GUI开发计算器
from tkinter import *
class Calculator(Frame):
def __init__(self,master=None):
super().__init__(master)
self.master=master
self.result = StringVar()
self.result.set(0)
self.keylist=['']
self.pack()
self.CreateWight()
self.pack()
#键盘布局
def CreateWight(self):
'''(('MC','MR','MS','M+','M-'),
('←','CE','C','±','√'),
(7,8,9,'/','%'),
(4,5,6,'*','1/x'),
(1,2,3,'-','='),
(0,'.','+'))'''
Label(self,bd=3,bg='white',font=('宋体',30),anchor='e',width=10,height=2,justify=RIGHT,textvariable=self.result).grid(row=0,column=0,rowspan=2,columnspan=5,pady=10)
#第一行
Button(self,text='MC',width=2,command=lambda :self.LabelView('MC')).grid(row=2,column=0,sticky=NSEW)
Button(self, text='MR', width=2, command=lambda: self.LabelView('MR')).grid(row=2, column=1, sticky=NSEW)
Button(self, text='MS', width=2, command=lambda: self.LabelView('MS')).grid(row=2, column=2, sticky=NSEW)
Button(self, text='M+', width=2, command=lambda: self.LabelView('M+')).grid(row=2, column=3, sticky=NSEW)
Button(self, text='M-', width=2, command=lambda: self.LabelView('M-')).grid(row=2, column=4, sticky=NSEW)
#第二行
Button(self, text='←', width=2, command=self.back).grid(row=3, column=0, sticky=NSEW)
Button(self, text='CE', width=2, command=self.clear).grid(row=3, column=1, sticky=NSEW)
Button(self, text='C', width=2, command=self.clear).grid(row=3, column=2, sticky=NSEW)
Button(self, text='±', width=2, command=lambda: self.LabelView('±')).grid(row=3, column=3, sticky=NSEW)
Button(self, text='√', width=2, command=lambda: self.LabelView('√')).grid(row=3, column=4, sticky=NSEW)
#第三行
Button(self, text='7', width=2, command=lambda: self.LabelView('7')).grid(row=4, column=0, sticky=NSEW)
Button(self, text='8', width=2, command=lambda: self.LabelView('8')).grid(row=4, column=1, sticky=NSEW)
Button(self, text='9', width=2, command=lambda: self.LabelView('9')).grid(row=4, column=2, sticky=NSEW)
Button(self, text='/', width=2, command=lambda: self.LabelView('/')).grid(row=4, column=3, sticky=NSEW)
Button(self, text='%', width=2, command=lambda: self.LabelView('%')).grid(row=4, column=4, sticky=NSEW)
#第四行
Button(self, text='4', width=2, command=lambda: self.LabelView('4')).grid(row=5, column=0, sticky=NSEW)
Button(self, text='5', width=2, command=lambda: self.LabelView('5')).grid(row=5, column=1, sticky=NSEW)
Button(self, text='6', width=2, command=lambda: self.LabelView('6')).grid(row=5, column=2, sticky=NSEW)
Button(self, text='*', width=2, command=lambda: self.LabelView('*')).grid(row=5, column=3, sticky=NSEW)
Button(self, text='1/x', width=2, command=lambda: self.LabelView('1/x')).grid(row=5, column=4, sticky=NSEW)
#第五行
Button(self, text='1', width=2, command=lambda: self.LabelView('1')).grid(row=6, column=0, sticky=NSEW)
Button(self, text='2', width=2, command=lambda: self.LabelView('2')).grid(row=6, column=1, sticky=NSEW)
Button(self, text='3', width=2, command=lambda: self.LabelView('3')).grid(row=6, column=2, sticky=NSEW)
Button(self, text='-', width=2, command=lambda: self.LabelView('-')).grid(row=6, column=3, sticky=NSEW)
Button(self, text='=', width=2, command=self.operation).grid(row=6, column=4, rowspan=2,sticky=NSEW)
#第六行
Button(self, text='0', width=2, command=lambda: self.LabelView('0')).grid(row=7, column=0, columnspan=2,sticky=NSEW)
Button(self, text='.', width=2, command=lambda: self.LabelView('.')).grid(row=7, column=2, sticky=NSEW)
Button(self, text='+', width=2, command=lambda: self.LabelView('+')).grid(row=7, column=3, sticky=NSEW)
#数字显示
def LabelView(self,key):
self.keylist.append(key)
kt=''.join(self.keylist)
if '=' in kt:
kt=self.operation(kt)
self.result.set(kt)
#回退←
def back(self):
keylist=self.result.get()
num=len(keylist)
if (num == 0) or (num ==1):
self.result.set(0)
self.keylist=['']
else:
self.result.set(keylist[:num-1])
#清零C \CE
def clear(self):
self.keylist=['']
self.result.set(0)
#运算加减乘除
def operation(self):
formula=self.result.get()
v_result=eval(formula)
self.keylist=[str(v_result)]
self.result.set(v_result)
if __name__ == '__main__':
width=228
height=422
root=Tk()
root.title('python计算器')
root.geometry('%dx%d+%d+%d' %(width,height,root.winfo_screenwidth()/2,root.winfo_screenheight()/2))
calcultor=Calculator(root)
root.mainloop()