Text: 文本控件;用於顯示多行文本
一、基本使用
1、插入文字、組件、圖片
插入內容如下:
代碼1如下:
# coding:utf8 from tkinter import * """ 1.text插入組件;2.text插入圖片;""" class App: def __init__(self, master): photo = PhotoImage(file='456.png') # text組件 frame = Frame(master).pack(padx=5, pady=5) text = Text(frame, width=100, height=30) text.pack() # 插入數據 文檔、圖片、組件 text.insert(INSERT, "插入開頭\n") text.insert(END, "插入結尾") # 插入圖片 def show(): text.image_create(END, image=photo) b = Button(text, text='插入圖片', command=show) text.window_create(INSERT, window=b) root = Tk() win = App(root) root.mainloop()
2、讀取文本內容
代碼2如下:
# coding:utf8 from tkinter import * """ 1.text插入組件;2.text插入圖片;""" class App: def __init__(self, master): photo = PhotoImage(file='456.png') # text組件 frame = Frame(master).pack(padx=5, pady=5) Label(frame, text="請在文本框輸入您的內容").pack() text = Text(frame, width=100, height=30) text.pack() # 使用get獲取Text的內容 def printt1(): print("您輸入的內容是:", text.get("0.0", END)) Button(frame, text="點擊打印您輸入的內容", command=printt1).pack() root = Tk() win = App(root) root.mainloop()
效果如下:
讀書和健身總有一個在路上