Tkinter的Text組件


Text: 文本控件;用於顯示多行文本

一、基本使用

1、插入文字、組件、圖片

插入內容如下:

image

代碼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()

效果如下:

image


讀書和健身總有一個在路上


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM