強調表示された領域のインデックスの長さをカウントするプログラムがあります。結果を表示するために tix balloon を使用したかったのです。しかし、tix バルーンを使用するには、ウィジェットにバインドする必要がありました。ウィジェット全体にバインドする代わりに、特定のイベントが呼び出されたときにバルーンを 1 回だけ表示するようにします。
私のプログラムのデモ (強調表示された領域の開始インデックスと終了インデックスをカウントします)
期待される出力
バルーンは、強調表示された領域の横に結果を表示します
コード
root.mainloop()from tkinter import *
import tkinter.tix as tkx
def print_count(event):
if text.tag_ranges('sel'):
global s0 , s1
s0 = text.index("sel.first")
s1 = text.index("sel.last")
countstringstart = s0.split('.')[1]
countstringend = s1.split('.')[1]
print(countstringstart)
print(countstringend)
waitshowballon()
root = tkx.Tk()
global text
text = Text(root)
text.bind('<ButtonRelease-1>', print_count)
text.pack()
def waitshowballon():
tooltip = tkx.Balloon(root, initwait=100)
tooltip.bind_widget(text, balloonmsg=s0)
root.mainloop()