4

I'm having some trouble understanding how to get tags to work on my particular application, what I have is a text widget called 'dwgoutputbox' that displays a number of fields after reading a CSV file

In this instance, the dwgoutputbox text widget displays a number of string variables descDwg1,descDwg2,descDwg3 which are the items in the first column, followed by the 'issue' number which are other variables

I'm trying to get the items in the first column to highlight so eventually I can make them clickable as they will link to files.

As the items in the first column will change, depending on the CSV search (but remain in the below general format) I'm unsure of how to get the tag_config to work

    self.outputQty.insert(INSERT,descQty)
                    self.outputDesc.insert(INSERT,descPN, END," ", END, descInfo)
                    self.dwgoutputbox.insert(INSERT, descDwg1, END, "   ", END, " Issue:  ",END,descIss1,END, "\n")
                    self.dwgoutputbox.insert(INSERT, descDwg2, END, "   ", END, " Issue:  ",END,descIss2,END, "\n")
                    self.dwgoutputbox.insert(INSERT, descDwg3, END, "   ", END, " Issue:  ",END,descIss3,END, "\n")
                    format_link()


    def format_link(dwgoutputbox,tag,apply_tag):
        self.dwgoutputbox.tag_config(tagName="19",foreground="blue",underline=1)
        dwgoutputbox.tag_bind(tag,"<Button-1>",apply_tag)

Ok, managed to get it to work as expected, largely due to the help and patience of Bryan Oakley - thank you its greatly appreciated.

self.dwgoutputbox = Text(root, borderwidth=0, width=50, height=15, foreground="#ffffff",background="#3F3F3F", font="system_font 10")
self.dwgoutputbox.grid(row=3, column=2, columnspan=5, padx=2, pady=3)
self.dwgoutputbox.tag_config("dwg",foreground="lightblue")

self.dwgoutputbox.insert(1.0, descDwg3, "dwg", "  Issue: ", "", descIss3, "", "\n")
self.dwgoutputbox.insert(1.0, descDwg2, "dwg", "  Issue: ", "", descIss2, "", "\n")
self.dwgoutputbox.insert(1.0, descDwg1, "dwg", "  Issue: ", "", descIss1, "", "\n")

half my problem is there doesnt appear to be many / any good 'basic' explanations of the more advanced stuff, which presents a newbie of a month of playing with python a good challenge. Enjoying it though :)

The result of the above now gives me enter image description here

LesM76
  • 63
  • 1
  • 9
  • Please show some code that illustrates what you've tried. Highlighting text is well documented, and there are plenty of examples on stackoverflow and other websites. – Bryan Oakley Mar 13 '16 at 23:34
  • Hi Bryan, updated to include some code, at the moment I'm trying to get it to work so its most likely very rough – LesM76 Mar 13 '16 at 23:50

1 Answers1

6

There are at least two problems in your code.

First, you're using the insert statement incorrectly. This is your code:

self.outputDesc.insert(INSERT,descPN, END," ", END, descInfo)

The insert command must have an index as its first argument, and then text as the next argument. Any arguments after that alternate as tags and text. In your case, the text is the value of descPN, and then a tag named "end" (the value of the END constant), a space, the tag of "end"m, and then the text descInfo.

Second, you never configure the tag "end". You do, however, configure a tag named 19, yet you're not applying that tag to any text. You need to either use the tag with the insert statement, or with the tag_add statement.

You need to do two things to using tags to highlight text in a text widget: you must configure a tag to have whatever attributes that you want (colors, fonts, etc), and you must add that tag to a range of text.

You can configure a tag with tag_configure. For example, to have a tag named "red" that gives text a red background, you would do something like this:

text_widget.tag_configure("red", background="red")

Next, you need to apply that tag to a range of text. There are two ways to do it. First, you can apply the tag to text when you insert it by including the tag as a parameter to the insert command:

text_widget.insert('end", "this text is red", "red")

The second way is to apply the tag to a range. For example, to make the second line red you could do it like this:

text_widget.tag_add("red", "2.0", "2.0 lineend")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • I was attempting baby steps by first getting the appropriate text to highlight before any actions, but didnt realise an additional step was required. Thank you for pointing it out, unfortunately the examples I've seeked out dont seem to be overly user friendly in explaining how things work :) – LesM76 Mar 14 '16 at 00:20
  • @LesM76: try reading this: http://www.tkdocs.com/tutorial/text.html#tags and http://stackoverflow.com/q/14786507/7432 and http://stackoverflow.com/q/3781670/7432, among others. – Bryan Oakley Mar 14 '16 at 00:25
  • self.dwgoutputbox.insert(INSERT, descDwg1, "end", "", "end", " Issue: ", "end", descIss1, "end", "\n") just want to verify this is the correct use of the insert statement, it displays correctly, but so did my incorrect use of it. – LesM76 Mar 15 '16 at 23:46
  • @LesM76: it depends on your definition of "correct". Yes, that's valid syntax. It's not doing what you think it's doing. In the code you posted, `"end"` is a tag, not an index. I doubt you have a tag named `"end"`. If you do have a tag named "end", it's correct. If you don't, and you think that `"end"` represents the end of the widget, then it is incorrect. – Bryan Oakley Mar 15 '16 at 23:48
  • you are exactly right with the latter. This inserting and tagging thing is uber confusing for a novice, I'll get there tho :) – LesM76 Mar 16 '16 at 00:08
  • managed to get the tagging thing pretty much sorted, using the tag_add, however I'm stumped using tags with .insert method. e.g. self.dwgoutputbox.insert(1.0, descDwg1, "dwg", "", " Issue: ", "", descIss1, "", "\n") adding the tag after 'descDwg1' tags that text but the rest of the string vanishes, – LesM76 Mar 16 '16 at 23:51
  • @LesM76: the syntax is simple: `widget.insert(, , , , , , ...)`. You've got the arguments after the first three reversed. All of the things you think are text are being treated as tags, and visa versa. – Bryan Oakley Mar 17 '16 at 00:06