立即學習: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()
運行結果: