hi i have text in a tkinter text widget, how would i highlight specific letters? -
-
Text.tag_config("s", background="yellow")
-
Text.insert(END,("s"))
-
-
-
the code above just adds s to the end of the text, do you know what i am doing wrong ?
14 14384 bvdet 2,851
Recognized Expert Moderator Specialist
You are telling Tkinter to insert text "s" at the end. The following will highlight a sequence of characters: - ....snip....
-
self.Btn1 = Tkinter.Button(self.mainFrame,
-
text="Highlight 's'",
-
command=lambda: self.highlight("s"))
-
self.Btn1.grid(row=3, column=0, sticky="ns")
-
-
def highlight(self, seq):
-
if "highlight" in self.textWidget.tag_names():
-
self.textWidget.tag_delete("highlight")
-
i = len(seq)
-
idx = "1.0"
-
while True:
-
idx = self.textWidget.search(seq, idx, nocase=1, stopindex='end')
-
if idx:
-
idx2 = self.textWidget.index("%s+%dc" % (idx, i))
-
self.textWidget.tag_add("highlight", idx, idx2)
-
self.textWidget.tag_config("highlight", background="yellow")
-
idx = idx2
-
else: return
-
....snip....
Maybe there's a better way?
is there any tutorial, or documentation on using tags and doing highlights in python 3 ?
i have tried this but its not working -
def highlight(seq):
-
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
-
as im trying to highlight white spaces, is there away of highlighting in a for loop, for example for " " in ttk.Text, highlight in yellow ??
bvdet 2,851
Recognized Expert Moderator Specialist
You can highlight any sequence of text, including a space character, with the code I posted.
i tried to modify the code, but something went wrong it is not highlighting anything -
def highlight(seq):
-
-
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
-
-
-
bvdet 2,851
Recognized Expert Moderator Specialist
It's possible the ttk.Text widget doesn't work like a Tkinter.Text widget. Following is the complete code for a Tkinter Text widget with a button to highlight the text "bolt". It works in Python 2.7.2. - import Tkinter
-
"""
-
Edit a file and save the text.
-
"""
-
-
textFont1 = ("Courier New", 16, "normal")
-
-
class ScrollbarX(Tkinter.Scrollbar):
-
def set(self, low, high):
-
if float(low) <= 0.0 and float(high) >= 1.0:
-
self.grid_remove()
-
else:
-
self.grid()
-
Tkinter.Scrollbar.set(self, low, high)
-
-
class App(Tkinter.Tk):
-
def __init__(self, fn, fnout):
-
Tkinter.Tk.__init__(self)
-
self.title("Text Widget")
-
self.fin = open(fn, 'r')
-
self.fnout = fnout
-
self.mainFrame = Tkinter.Frame(self)
-
-
top=self.winfo_toplevel()
-
top.columnconfigure(0, weight=1)
-
top.rowconfigure(0, weight=1)
-
-
self.mainFrame.grid(row=0, column=0, sticky="nsew")
-
self.exit = Tkinter.Button(self.mainFrame,
-
text="Save and Exit",
-
command=self.finish)
-
self.exit.grid(row=0, column=0, sticky="ns")
-
-
-
self.mainFrame.columnconfigure(0, weight=1)
-
self.mainFrame.rowconfigure(1, weight=1)
-
-
vscrollbar = ScrollbarX(self.mainFrame)
-
vscrollbar.grid(row=1, column=1, sticky="ns")
-
hscrollbar = ScrollbarX(self.mainFrame, orient=Tkinter.HORIZONTAL)
-
hscrollbar.grid(row=2, column=0, sticky="ew")
-
-
self.textWidget = Tkinter.Text(self.mainFrame,
-
yscrollcommand=vscrollbar.set,
-
xscrollcommand=hscrollbar.set,
-
wrap=Tkinter.NONE,
-
height=24,
-
width=60,
-
font=textFont1)
-
self.textWidget.insert("1.0", self.fin.read())
-
self.textWidget.grid(row=1, column=0, sticky="nsew")
-
-
hscrollbar["command"] = self.textWidget.xview
-
vscrollbar["command"] = self.textWidget.yview
-
-
self.Btn1 = Tkinter.Button(self.mainFrame,
-
text="Highlight 'bolt'",
-
command=lambda: self.highlight("bolt"))
-
self.Btn1.grid(row=3, column=0, sticky="ns")
-
-
def finish(self):
-
fout = open(self.fnout, 'w')
-
fout.write(self.textWidget.get("1.0", "end"))
-
fout.close()
-
self.fin.close()
-
app.destroy()
-
-
def highlight(self, seq):
-
if "highlight" in self.textWidget.tag_names():
-
self.textWidget.tag_delete("highlight")
-
i = len(seq)
-
idx = "1.0"
-
while True:
-
idx = self.textWidget.search(seq, idx, nocase=1, stopindex='end')
-
if idx:
-
idx2 = self.textWidget.index("%s+%dc" % (idx, i))
-
self.textWidget.tag_add("highlight", idx, idx2)
-
self.textWidget.tag_config("highlight", background="yellow")
-
idx = idx2
-
else: return
-
-
if __name__ == "__main__":
-
fn = "text1.txt"
-
fnout = "text2.txt"
-
app = App(fn, fnout)
-
app.mainloop()
im using tkinter.Text widget ?, im confused, so the example wont work in python 3
bvdet 2,851
Recognized Expert Moderator Specialist
Since I don't have Python 3 installed, there is no way for me to test it. I believe it can easily be converted though. Replace every occurrence of "Tkinter" with "tkinter". That should take care of most if not all of it.
yeah i replaced it and the program does work in python 3, i'm struggling to understand how the highlighting is done in that program, still cant get the highlighting to work in my program
bvdet 2,851
Recognized Expert Moderator Specialist
Function highlight() basically works like this: - If the Text widget has a tag named "highlight", delete all occurrences
- Save the length of the text string "seq"
- Set the starting index of the widget to "1.0"
- Start a loop
- Use Text widget method "search" to find the next occurrence of "seq"
- If no occurrence of "seq", return
- If there is an occurrence, calculate the next index from the old index and the length of "seq"
- Add a tag named "highlight" from idx to idx2
- Configure the tag "yellow"
- Reassign idx to idx2
so basically i think the highlight function is not working with my button
- button4 = ttk.Button(content,text="highlight", command=lambda: highlight(" "))
bvdet 2,851
Recognized Expert Moderator Specialist
moroccanplaya - That should work. If it doesn't, there is something else wrong.
i have used the same highlight function as in the example you gave me i just removed the self and tried using both tkinter.text and textWidget to see if they work, i used - if "highlight" in tkinter.Text.tag_names():
and i revived an error stating that tag_names takes at least 1 argument
then i changed it back just to : - if "highlight" in Text.tag_names():
i get no error
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Cox |
last post by:
How would I go about highlighting text in a textbox?
|
by: parm |
last post by:
How do I highlight text in my first (tabindex = 1) asp control (eg:
textbox) on the webform on page load. If I try to set focus using
javascript then I get script error "Cannot move focus to...
|
by: abc my vclass |
last post by:
which properties to focus automatic highlight text on text when on fous?
|
by: ken |
last post by:
Hi,
I have two questions the first is: in the example below how can I call
an event from within a statement, such as replace Stop1 with cmdStop1
which is a button on my form?
My second question...
|
by: Sunil Korah |
last post by:
Hi,
I haven't used access reports much. I have a problem in getting the
total of a group.
I have 3 fields, ProgName (Program name), Pname (Participant's name)
and PCategory (Participant...
| |
by: shapper |
last post by:
Hello,
I have an ASP.NET page and in its runtime code I am trying to get an
user profile, change it and save it.
It works if I use Profile, which is the profile for the current
authenticated...
|
by: watkinsdev |
last post by:
Hi,
I have created a mesh class in visual studio 6.0 c++.
I can create a device, render objects and can edit the objects by for
instancnce selecting a cluster of vertices and processing the...
|
by: =?Utf-8?B?Tmlja28u?= |
last post by:
Hi,
I'm at my wits-end here. I'm a beginner with ASP/C# (using .NET 2003)
and I'm trying to post variables from a classic ASP form to a ASP.NET
form. The Classic ASP form was scripted with...
|
by: Dave |
last post by:
Is there a way to selectively highlight text in an OverLib popup? I'd
like to be able to make some text stand out from the rest of the text
that is displayed.
I tried using a one-cell table with...
|
by: mbatestblrock |
last post by:
I have no idea how to even phrase this.. I assume this is something easy.
http://www.picment.com/articles/css/funwithforms/
just an example. When you highlight text with your mouse (as if you...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| | |