I am writing an app of a Python editor of my own. I am using a Text widget and want to highlight words as the words are typed in like Python editor. As soon as the character # is typed in, I want to begin highlighting all characters followed from the character # with color of red.
Below is the partial code for the purpose. When the character # was identified as it was typed in to the Text widget, I added a tag of "CM" from the typed-in-character to the end of the line (I thought this would do the job for me).
import tkinter as tk
def onModification(event=None):
c=event.char
if not c: return
pos=hT0.index(tk.INSERT)
if c=='#':
hT0.tag_add('CM',pos,f'{int(pos.split(".")[0])}.end')
return
hW=tk.Tk()
hT0=tk.Text(hW,wrap='none',font=('Times New Roman'12))
hT0.insert('end','')
hT0.place(x=27, y=0, height=515,width=460)
hT0.bind('<Key>', onModification)
hT0.tag_config('CM', foreground='#DD0000')
But the output highlights only characters already existed even without the just-typed-in-character #.
An idea for the job I want?
Thank you so much in advance.
I obtained an idea from the Get position in tkinter Text widget
def onModification(event=None):
...
pos=hT0.index(tk.INSERT)
lineN, ColN=[int(c) for c in pos.split('.')]
if c=='#':
#hT0.tag_add('CM',pos,f'{int(pos.split(".")[0])}.end')
hT0.tag_add('CM',f'{lineN}.{ColN-1}',f'{lineN}.end')
return
...
#hT0.binds('<key>', onModification) needs to be changed to...
hT0.bindtags(('Text','post-class-bindings','.','all'))
hT0.bind_class('post-class-bindings', '<KeyPress>', onModification)
Related
So I got this weird issue where I simply want to display a Hebrew text (with special characters, accents, diacritics, etc.) on a Label.
lines = None
with open("hebrew.txt", encoding="utf-8") as f:
lines = f.read().split("\n")
window = Tk()
lbl = Label(window, text=lines[11], font=("Narkisim", 14))
lbl.grid(column=0, row=0)
window.geometry('850x200')
window.mainloop()
Hebrew is a RTL language. The thing is - up until a specific length of the line - it displays correctly. But once the line is longer than that length, the next words appear to the right of the text (in English, think of it as if words started appearing to the left of the text instead of each word being the right-most). This only happens with lines over a certain length... (e.g. if I do text=lines[11][:108] it's still good. The "good" length changes a bit from line to line.
This only happens with all nikkud/diacritics/accents. If I use regular Hebrew text without these special characters - it's all good. Any ideas what could be the issue? It's driving me crazy.
Hi im having some trouble getting sometimes longer texts which should be line breaked at specific lines onto an image it always just prints the \n with it without breaking the line and i cant find any info online if this is even possible or if it just sees the raw string to put on the img without checking for linebreaks. The Text is just some random stuff from a CSV
def place_text(self,text,x,y):
temp = self.csv_input[int(c)][count]
font = ImageFont.truetype('arial.ttf', 35) # font z.b.: arial.ttf
w_txt, h_txt = font.getsize(text)
print("Jetzt sind wie in der zweiten möglichkeit")
draw_text = ImageDraw.Draw(self.card[self.cardCount])
draw_text.text((x, y), temp, fill="black", font=font, align="left")
Yeah i know this Code is kinda all over the place but for putting the text on the image that shouldnt cause any issues does it?
Writing stuff on an Imgae with that results in just one line of continuous text with the \n's still in there and no line breaks.
Found the answer the String pulled fomr the CSV had to be decoded again before beeing placed
text = bytes(text, 'utf-8').decode("unicode_escape")
did the trick
I have many lines like these:
_ÙÓ´Immediate Transformation With Vee_ÙÓ´
‰ÛÏThe Real Pernell Stacks‰Û
I want to get something like this:
Immediate Transformation With Vee
The Real Pernell Stacks
I tried this:
for t in test:
t.isalpha()
but characters like this Ó count as well
So I also thought that I can create a list of English words, a space and punctuation marks and delete all the elements from the line that are not in this list, but I do not think that this is the right option, since the line can contain not only English words and that's fine.
Using Regex.
Ex:
import re
data = """_ÙÓ´Immediate Transformation With Vee_ÙÓ´
‰ÛÏThe Real Pernell Stacks‰Û"""
for line in data.splitlines(keepends=False):
print(re.sub(r"[^A-Za-z\s]", "", line))
Output:
Immediate Transformation With Vee
The Real Pernell Stacks
use re
result = ' '.join(re.split(r'[^A-Za-z]', s))
I am making a game in pygame for my sister for practising in pygame and I am trying to add a random line from a text file in to window name:
import pygame,random,sys
RandomMess = open('GameText.txt')
pygame.display.set_caption('Dream Land: {}').
#The .txt file currently has 4 lines but will be a lot more in the future...
#These are the test lines: This is in beta, hi :), hello, hi
I have all the rest of the code in case anyone says that I haven't done any other code to make the window or anything.
I want it to say: Dream Land: {} then in the braces add a random line from the file.
You're looking for readlines() and random.choice().
import random,sys
RandomMess = open('GameText.txt')
# .readlines() will create a list of the lines in the file.
# So in our case ['This is in beta', 'hi :)', 'hello', 'hi']
# random.choice() then picks one item from this list.
line = random.choice(RandomMess.readlines())
# Then use .format() to add the line into the caption
pygame.display.set_caption('Dream Land: {}'.format(line))
I want to get the line number of a textEdit in pyqt when the mouse cursor is clicked on a particular line . Kindly suggest
Maybe this gives you an idea:
self.connect(self.ui.textEdit, QtCore.SIGNAL("cursorPositionChanged()"),self.lineNumber)
and later:
def lineNumber(self):
# we try to get the lineNumber on change of cursor position
theLineNumber = self.ui.textEdit.textCursor().blockNumber()
self.ui.label.setText('Ln : '+str(theLineNumber))