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

วิธีบังคับให้ Tkinter Text Widget อยู่บรรทัดเดียว


Tkinter Text widget สามารถกำหนดค่าได้ด้วยฟังก์ชัน configure(**options) ใช้กำหนดสีพื้นหลัง สีตัวอักษร การ wrapping และคุณสมบัติอื่นๆ

คุณสมบัติ wrap ของ Text widget ระบุว่าเคอร์เซอร์จะเปลี่ยนตำแหน่งเมื่อตรวจพบบรรทัดใหม่ ใน Tkinter Text widget สามารถ wrap ตามคำและตัวอักษรได้ เพื่อให้ Text widget อยู่บรรทัดเดียว ให้ใช้ wrap=None

ตัวอย่าง

# Import the required libraries
from tkinter import *
import lorem

# Create an instance of tkinter frame or window
win=Tk()
# Set the size of the window
win.geometry("700x350")

# Add a Text widget
text=Text(win, width=60, height=20)
text.pack()

# Add Some text into it
text.insert(END, lorem.sentence())

# Configure the text widget to make the text sticky on one line
text.configure(wrap=None)
win.mainloop()

ผลลัพธ์

การรันโค้ดจะแสดง Text widget ที่มีข้อความ wrap เป็น None

วิธีบังคับให้ Tkinter Text Widget อยู่บรรทัดเดียว