Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

In và lấy input từ Text box Tkinter

Text widget: insert text, hiển thị info, lấy output. Lấy user input: text.get(1.0, "end-1c").

Ví dụ

from tkinter import *
from tkinter import ttk

win = Tk()
win.geometry("700x350")

def get_input():
    label.config(text="" + text.get(1.0, "end-1c"))

text = Text(win, width=80, height=15)
text.insert(END, "")
text.pack()

ttk.Button(win, text="Print", command=get_input).pack()
Label(win, text="", font=('Calibri 15')).pack()
win.mainloop()

Kết quả

Text widget trống. Gõ text → click "Print" → Label hiển thị nội dung vừa gõ.

In và lấy input từ Text box Tkinter

Giải thích: 1.0 = bắt đầu. end-1c = kết thúc trừ 1 char (bỏ newline tự động). get() trả về string toàn bộ nội dung.