如何在不換行的情況下使用 Tkinter 文字小元件?


可使用 configure(**options)** 函式配置 Tkinter 文字小元件。我們可以利用它配置文字小元件的背景顏色、前景色、換行和其他屬性。

文字小元件的 wrap 屬性表示當檢測到新的一行時,游標將改變其位置。然而,在 Tkinter 中,文字小元件可以按單詞和字元換行。為了使我們的文字小元件保持在一行內,我們可以使用 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()

輸出

執行以上程式碼將顯示一個文字小元件,其中文字將換行到 None。

更新於: 18-Jun-2021

1 千+ 瀏覽量

開啟你的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.