Separate Command For Clicking Each Line In Python Tkinter Text Widget
Background Each one of the items appearing in my text widget represent tasks. I am using a Text widget over a ListBox because Text widget allows different colors for different item
Solution 1:
Just set a binding on <1>
. It's easy to get the line number that was clicked on using the index
method of the widget and the x/y coordinate of the event.
Here's a simple example:
import Tkinter as tk
classExampleApp(tk.Tk):
def__init__(self):
tk.Tk.__init__(self)
self.status = tk.Label(self, anchor="w")
self.status.pack(side="bottom", fill="x")
self.text = tk.Text(self, wrap="word", width=40, height=8)
self.text.pack(fill="both", expand=True)
self.text.bind("<1>", self.on_text_button)
for n inrange(1,20):
self.text.insert("end", "this is line %s\n" % n)
defon_text_button(self, event):
index = self.text.index("@%s,%s" % (event.x, event.y))
line, char = index.split(".")
self.status.configure(text="you clicked line %s" % line)
if __name__ == "__main__":
app = ExampleApp()
app.mainloop()
Solution 2:
I think you can do something like this. tkHyperlinkManger does it ( http://effbot.org/zone/tkinter-text-hyperlink.htm )
Since you're already coloring the lines differently, I assume you're using tag_config
. Then all you need is tag_bind
to bind a callback to the region of text.
Post a Comment for "Separate Command For Clicking Each Line In Python Tkinter Text Widget"