4
Contributors
10
Replies
11
Views
5 Years
Discussion Span
Last Post by moroccanplaya
0

You could use a prompt.

prompt = ("TEST")
label1 = Label(root, text=prompt, width=len(prompt), bg='yellow')
label1.pack()

0

i got a text in a text widget, how would highlight text that's in a tkinter text widget, i think you cant put lables ontop of a text widget ?

1

Here try this.

import Tkinter as tk

root = tk.Tk()
text = tk.Text(root, font=("Helvetica", 50))
text.pack()

text.insert(tk.END, "Hello, World")
#Designate what letters to highlight
#in this case Hello and W is highlighted
text.tag_add("Hello", "1.0", "1.5")
text.tag_add("W", "1.7", "1.8")
text.tag_config("Hello", background="yellow", foreground="blue")
text.tag_config("W", background="black", foreground="green")

root.mainloop()
0

thanks for your great example, can you please explain what does the numbers in the text.tag_add do, "1.0" "1.5" ?

0

so for example if i wanted to highlight all the l in a text i would have to pick out each position ?

0

Vegaseat has a nice example in the "Python GUI Programming" sticky, but I refuse to be someone else's research assistant. If memory serves it is somewhere in the middle of the thread's pages.

0

it still doesnt work do you know where im going wrong ???

def highlight(seq):
    #remove highlights
    if "highlight" in Text.tag_names():
        Text.tag_delete("highlight")

        i = len(seq)
        index = "1.0"
        while True:
            index = Text.search(seq, index, nocase=1, stopindex='end')
            if index:
                index2 = Text.index("%s+%d" % (index, i))
                Text.tag_add("highlight", index, index2)
                Text.tag_config("highlight", background="yellow")
                index = index2
            else: return
button4 = ttk.Button(content,text="highlight", command=lambda:highlight("s"))
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.