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

Lấy dữ liệu nhập từ Entry widget Tkinter

Entry widget nhập một dòng. Lấy dữ liệu bằng get(). Có thể hiển thị lên Label.

Ví dụ

# Import thư viện
from tkinter import *
from tkinter import ttk

# Tạo khung
win = Tk()
win.geometry("700x250")

# Hàm lấy dữ liệu
def get_data():
    label.config(text=entry.get(), font=('Helvetica 13'))

# Entry widget
entry = Entry(win, width=42)
entry.place(relx=.5, rely=.5, anchor=CENTER)

# Label hiển thị
label = Label(win, text="", font=('Helvetica 13'))
label.pack()

# Nút lấy dữ liệu
ttk.Button(win, text="Click to Show", command=get_data).place(relx=.7, rely=.5, anchor=CENTER)

win.mainloop()

Kết quả

Entry và nút Click to Show. Nhập text, click nút hiển thị lên Label.

Lấy dữ liệu nhập từ Entry widget Tkinter

Lấy dữ liệu nhập từ Entry widget Tkinter