Text widget hỗ trợ nhập nhiều dòng. Tạo app đếm ký tự real-time khi gõ.
- Tạo Text widget width/height
- Label hiển thị tổng ký tự
- Bind event
<KeyPress>và<KeyRelease> - Function cập nhật label:
len(text.get("1.0", 'end-1c')) - Pack widgets
Ví dụ
from tkinter import *
win = Tk()
win.geometry("700x350")
def update(event):
label.config(text="Total Characters: " + str(len(text.get("1.0", 'end-1c'))))
text = Text(win, width=50, height=10, font=('Calibri 14'))
text.pack()
label = Label(win, text="", justify=CENTER, font=('11'))
label.pack()
text.bind('', update)
text.bind('', update)
win.mainloop()
Kết quả
Text editor + label dưới cùng. Gõ text → label cập nhật "Total Characters: N" real-time.

Giải thích: "1.0" = dòng 1 ký tự 0 (đầu). 'end-1c' = cuối trừ 1 ký tự (bỏ newline cuối). len() đếm ký tự.