简体   繁体   中英

Tagging in a text widget - tkinter

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 在此处输入图片说明

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM