Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีพิมพ์และรับข้อมูลจากผู้ใช้ใน Text Box ของ Tkinter


ใช้ widget text ของ Tkinter แทรกข้อความ แสดงข้อมูล และดึงข้อมูลจาก text widget เพื่อรับข้อมูลจากผู้ใช้ใน text widget ใช้เมธอด get() ตัวอย่างด้านล่าง

ตัวอย่าง

# Import the required library 
from tkinter import * 
from tkinter import ttk 
# Create an instance of tkinter frame 
win=Tk() 
# Set the geometry 
win.geometry("700x350") 
def get_input(): 
    label.config(text=""+text.get(1.0, "end-1c")) 
# Add a text widget 
text=Text(win, width=80, height=15) 
text.insert(END, "") 
text.pack() 
# Create a button to get the text input 
b=ttk.Button(win, text="Print", command=get_input) 
b.pack() 
# Create a Label widget 
label=Label(win, text="", font=('Calibri 15')) 
label.pack() 
win.mainloop()

ผลลัพธ์

รันโค้ด จะแสดงหน้าต่างมี text widget พิมพ์ข้อความใน text widget แล้วคลิกปุ่ม "Print" เพื่อแสดงผลลัพธ์

วิธีพิมพ์และรับข้อมูลจากผู้ใช้ใน Text Box ของ Tkinter