立即学习:https://edu.csdn.net/course/play/19711/343112?utm_source=blogtoedu
1.Frame:是内嵌布局管理器,针对不同功能的组件进行区域划分,在各自的区域内可以使用不同的布局,但是总的frame布局方式还是一致的
2.以计算器为例
步骤:
1)将组件划分成不同的frame
2)对每一个frame内部的组件进行布局
3)对组件进行事件的绑定
4)最终实现的是一级计算的简单计算器
知识点:
1)tkinter.Entry():单行输入文本框
2)tkinter.Text():多行输入文本框
3)event.widget["text"]:获取绑定了该事件的组件上面的文本
4)正则表达式的匹配与分割
5)tkinter.StringVar()
完整代码
import tkinter
import reclass Mainwindow():#------------在构造方法中创建窗体--------------def __init__(self):#----------定义窗体-----------self.root = tkinter.Tk()self.root.geometry("240x300")self.root.maxsize(300,300)self.root.title("linlianqin计算器")self.input_frame()#显示输入区域self.button_fram()#显示按钮组区域self.root.mainloop()#--------------创建各个部分的区域显示,frame----------------def input_frame(self):#定义数字输入显示区域,实现功能的显示self.inputframe = tkinter.Frame(self.root,width = 20)#文本输入区域创建self.content = tkinter.StringVar()#为了修改#Entry组件是单行输入的文本输入框,而text是多行输入文本框#在文本输入区域进行Entry组件的创建self.entry = tkinter.Entry(self.inputframe,textvariable = self.content ,font = ("微软雅黑",20),fg = "#000000")self.clean = False#用于给输入框清0的标记self.entry.pack(fill = "x",expand = 1)self.inputframe.pack(side = "top")def button_fram(self):#定义数字及运算符按钮区域#创建按钮组件区域self.buttonframe = tkinter.Frame(self.root)self.button_list = [[],[],[],[]]self.button_list[0].append( tkinter.Button(self.buttonframe,width = 3,text = "1",font = ("微软雅黑",20),fg = "#000000"))self.button_list[0].append( tkinter.Button(self.buttonframe,width = 3,text = "2",font = ("微软雅黑",20),fg = "#000000"))self.button_list[0].append( tkinter.Button(self.buttonframe,width = 3,text = "3",font = ("微软雅黑",20),fg = "#000000"))self.button_list[0].append( tkinter.Button(self.buttonframe,width = 3,text = "+",font = ("微软雅黑",20),fg = "#000000"))self.button_list[1].append( tkinter.Button(self.buttonframe,width = 3,text = "4",font = ("微软雅黑",20),fg = "#000000"))self.button_list[1].append( tkinter.Button(self.buttonframe,width = 3,text = "5",font = ("微软雅黑",20),fg = "#000000"))self.button_list[1].append( tkinter.Button(self.buttonframe,width = 3,text = "6",font = ("微软雅黑",20),fg = "#000000"))self.button_list[1].append( tkinter.Button(self.buttonframe,width = 3,text = "-",font = ("微软雅黑",20),fg = "#000000"))self.button_list[2].append( tkinter.Button(self.buttonframe,width = 3,text = "7",font = ("微软雅黑",20),fg = "#000000"))self.button_list[2].append( tkinter.Button(self.buttonframe,width = 3,text = "8",font = ("微软雅黑",20),fg = "#000000"))self.button_list[2].append( tkinter.Button(self.buttonframe,width = 3,text = "9",font = ("微软雅黑",20),fg = "#000000"))self.button_list[2].append( tkinter.Button(self.buttonframe,width = 3,text = "*",font = ("微软雅黑",20),fg = "#000000"))self.button_list[3].append( tkinter.Button(self.buttonframe,width = 3,text = ".",font = ("微软雅黑",20),fg = "#000000"))self.button_list[3].append( tkinter.Button(self.buttonframe,width = 3,text = "0",font = ("微软雅黑",20),fg = "#000000"))self.button_list[3].append( tkinter.Button(self.buttonframe,width = 3,text = "=",font = ("微软雅黑",20),fg = "#000000"))self.button_list[3].append( tkinter.Button(self.buttonframe,width = 3,text = "/",font = ("微软雅黑",20),fg = "#000000"))self.row = 0for button_group in self.button_list:self.column = 0for self.button in button_group:self.button.bind("<Button-1>", lambda event: self.button_handle(event))self.button.grid(row = self.row,column = self.column)self.column += 1self.row += 1self.buttonframe.pack(side = "bottom")#-----------------------定义事件处理的方法----------------------def button_handle(self,event):per = event.widget["text"]#可以获得绑定了该事件的组件上的文本if self.clean:self.content.set("")#清零self.clean = Falseif per != "=":#在文本输入框的末端插入新按下的按键的值self.entry.insert("end",per)elif per == "=":#如果检测到按下的是等号时就进行数学运算exp = self.entry.get()#获得输入框中的数学运算表达式patter = r"\+|\-|\*|\/"#正则表达式nums = re.split(patter,exp)#根据正则表达式分割字符串flag = re.findall(patter,exp)[0]#根据正则表达式来找到相应的符号,必须进行取零操作,因为正则返回一个字符串result = 0if flag == "+":result = float(nums[0]) + float(nums[1])#必须将其规定数字类型,因为分割后得到的是字符串elif flag == "-":result = float(nums[0]) - float(nums[1])elif flag == "*":result = float(nums[0]) * float(nums[1])elif flag == "/":result = float(nums[0]) / float(nums[1])self.entry.insert("end","=%s"%result)self.clean = Truedef main():Mainwindow()if __name__ == '__main__':main()
运行结果: