ใน tkinter เราสามารถสร้างวิดเจ็ตข้อความโดยใช้แอตทริบิวต์ข้อความโดยใช้แพ็คเกจ อย่างไรก็ตาม ขณะสร้างแอปพลิเคชัน GUI บางครั้งเราต้องบันทึกอินพุตจากวิดเจ็ตข้อความ
เราสามารถรับข้อมูลจากผู้ใช้ในวิดเจ็ตข้อความโดยใช้ .get() กระบวนการ. เราจำเป็นต้องระบุช่วงการป้อนข้อมูลซึ่งจะเริ่มตั้งแต่ 1.0 ถึง END ซึ่งจะแสดงอักขระที่เริ่มต้นและสิ้นสุดจนถึง END
ตัวอย่าง
#Import tkinter library
from tkinter import *
#Create an instance of tkinter window or frame
win=Tk()
win.geometry("700x300")
def get_input():
value=my_text_box.get("1.0","end-1c")
print(value)
#Creating a text box widget
my_text_box=Text(win, height=5, width=40)
my_text_box.pack()
#Create a button for Comment
comment= Button(win, height=5, width=10, text="Comment", command=lambda: get_input())
#command=get_input() will wait for the key to press and displays the entered text
comment.pack()
win.mainloop() ผลลัพธ์
การเรียกใช้โค้ดด้านบนจะแสดงกล่องข้อความซึ่งจะยอมรับอินพุตจากผู้ใช้และพิมพ์เอาต์พุตบนคอนโซล
