當我在 Python 中使用 tkinter 文本時,如果我用滑鼠選擇一個單詞塊并按下 tab 命令,則只會縮進第一個單詞。有沒有辦法讓所有選定的單詞縮進?
uj5u.com熱心網友回復:
實作此目的的一種方法是獲取選擇索引并插入\t每一行。
這是一個例子
import tkinter as tk
def tab(event):
try:
start_line = int(text.index("sel.first")[0])
last_line = int(text.index("sel.last")[0])
for x in range(start_line, last_line 1):
text.insert(f"{x}.0", "\t")
return 'break'
except tk.TclError:
pass
root = tk.Tk()
text = tk.Text(root)
text.pack(expand=True, fill="both")
text.bind("<Tab>", tab)
root.mainloop()
text.index("sel.first")回傳第一個選擇text.index("sel.last")的索引并回傳最后一個選擇的索引。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/345018.html
上一篇:如何在函式執行完成后立即終止視窗
下一篇:如何使影像永久顯示在樹視圖中?
