1.Text(root,width,height,bg) 主窗口,寬度,高度,背景色 2.使用 .insert() 方法添加內容 Text 對象.insert(幾行.幾列,"內容") w1.insert(2.3,"···") END 為最后位置 self.w1.insert(END,'[end]') 3.Button(窗口對象,text = "內容",command = "self.函數名").pack([side = "left"]) Button(self,text = "返回文本",command = self.returnText).pack(side = "left") text 顯示的內容 command 運行的函數 pack 位置,使用 side 后,按鈕按照 pack 來 4.在類中定義的屬性,不會因為運行函數方法后,就銷毀 self.photo 不用再使用 global 進行聲明 5.使用 PhotoImage 將圖片存起來后,將圖片顯示在多行文本 Text 中 self.photo = PhotoImage(file = '圖片路徑/圖片名.gif') self.photo = PhotoImage(file = 'images/logo.gif') 使用 .image_create(位置,image = self.photo) 進行添加 self.w1.image_create(END,image = self.photo) 6.添加按鈕組件到文本中 btn1 = Button(文本內容,text = "內容") 7.self.w1.tag_config (內容,background 背景顏色,foreground 文字顏色) 8.self.w1.tag_add("內容",起始位置,終止位置) tag_add 加入內容 9.self.w1.tag_bind("內容","事件",self.函數名) self.w1.tag_bind("baidu","<Button-1>",self.webshow) 10.webbrowser.open("網址") 打開一個網址
from tkinter import * from tkinter import messagebox # 顯示消息 import webbrowser # 導入 webbrowser 到時候點擊字體跳轉使用 class Application(Frame): '''GUI程序經典寫法''' def __init__(self,master = None): super().__init__(master) # super() 表示父類的定義,父類使用 master 參數 self.master = master # 子類定義一個屬性接收傳遞過來的 master 參數 self.pack() # .pack 設置布局管理器 self.createWidget() # 在初始化時,將按鈕也實現 # master傳遞給父類 Frame 使用后,子類中再定義一個 master 對象 def createWidget(self): '''創建組件''' # 創建文字 Text(root 主窗口對象,width 寬度,height 高度,bg 背景色) # 只對於文本有效 self.w1 = Text(root,width = 100,height = 40,bg = "gray") # 設置背景色 bg = "gray" self.w1.pack() self.w1.insert(1.0,"0123456789\nabcdefg") # 1.0 在 第一行 第零列 插入數據 self.w1.insert(2.3,"活在當下\n結發為夫妻,恩愛兩不疑\n言行在於美,不在於多") # 2.3 在 第二行 第三列 Button(self,text = "重復插入文本",command = self.insertText).pack(side = "left") # 水平排列 side = "left" Button(self,text = "返回文本",command = self.returnText).pack(side = "left") Button(self,text = "添加圖片",command = self.addImage).pack(side = "left") Button(self,text = "添加組件",command = self.addWidget).pack(side = "left") Button(self,text = "通過 tag 控制文本",command = self.testTag).pack(side = "left") def insertText(self): '''INSERT 索引表示在光標處插入''' self.w1.insert(INSERT,'Hany') # END 索引號表示在最后插入 self.w1.insert(END,'[end]') # 在文本區域最后 self.w1.insert(1.2,"(.-_-.)") def returnText(self): '''返回文本內容''' # Indexes(索引) 是用來指向 Text 組件中文本的位置 # Text 的組件索引 也是對應實際字符之間的位置 # 核心:行號從 1 開始,列號從 0 開始 print(self.w1.get(1.2,1.6)) print("文本內容:\n" + self.w1.get(1.0,END)) def addImage(self): '''增加圖片''' self.photo = PhotoImage(file = 'images/logo.gif') self.w1.image_create(END,image = self.photo) def addWidget(self): '''添加組件''' btn1 = Button(self.w1,text = "Submit") self.w1.window_create(INSERT,window = btn1) # 添加組件 def testTag(self): '''將某一塊作為特殊標記,並使用函數''' self.w1.delete(1.0,END) self.w1.insert(INSERT,"Come on, you're the best.\n博客園\nHany 加油!!!") # self.w1.tag_add("good",1.0,1.9) # 選中標記區域 # self.w1.tag_config("good",background = "yellow",foreground = "red") # 單獨標記某一句,背景色 字體色 self.w1.tag_add("baidu",3.0,3.4) # self.w1.tag_config("baidu",underline = True,background = "yellow",foreground = "red") self.w1.tag_bind("baidu","<Button-1>",self.webshow) def webshow(self,event): webbrowser.open("http://www.baidu.com") if __name__ == '__main__': root = Tk() # 定義主窗口對象 root.geometry("500x300+400+300") # 創建大小 root.title("Button 測試") # 設置標題 app = Application(master = root) # 傳遞 master 參數為 主窗口對象 root.mainloop()
2020-04-20