How to display rich text in a gtk.MenuItem label? - python

I would like to display rich text in a gtk.MenuItem(), e.g. displaying bold text. So far I have only found that one could pass in a string.
Is it possible to display rich text in such a label? If so, how?
If not, how appalling that would be! :)
Thanks.

You cannot set the menu bold, but you may set the label on it as bold by calling the child :
label = menuItem.get_children()[0]
label.set_markup("<b>Hi Pete!</b>")

Related

I want to add a color for the text for every new line added PyQt5

I am trying to figure out how to color the text using TextEdit in a line Edit, but for any new text I will choose the color.
Let's say if it is an error I want it to be red or for warning I want it to be yellow and in a normal case to remain black.
Also if there is a possibility to highlight the text in color red let's say.
self.main_window.textEdit.append(get_proj)
I am using the command above to append the text in the QlineEdit, is there something I can use with TextEdit to color the text?
The question is quite ambiguous since you are using both the terms QLineEdit and QTextEdit which are essentially two different type of widgets, I'm assuming its QTextEdit widget since QLineEdit does not have an associated method named append.
QTextEdit supports rich text, so you can use css styling with html for the texts in QTextEdit. You can append different rich texts with different styles. For ease, you can just create some formatted texts, and pass the corresponding text to the format method of python string for such formatted texts to create such rich texts, and append it to QTextEdit. Consider below example for your use case:
import sys
from PyQt5.QtWidgets import QTextEdit, QApplication
if __name__=='__main__':
app = QApplication([])
textEdit = QTextEdit()
# Create formatted strings
errorFormat = '<span style="color:red;">{}</span>'
warningFormat = '<span style="color:orange;">{}</span>'
validFormat = '<span style="color:green;">{}</span>'
# Append different texts
textEdit.append(errorFormat.format('This is erroneous text'))
textEdit.append(warningFormat.format('This is warning text'))
textEdit.append(validFormat.format('This is a valid text'))
textEdit.show()
sys.exit(app.exec_())
Additionally, if you want to color code the text for each new lines, QTextEdit has a textChanged signal, you can connect it to a method where you read the new line from QTextEdit and check for the validity of the text, and set the color accordingly.

How to get the font color of a certain area in tkinter.Text Widget?

So I am actually creating a coding editor and obviously I needed the code to be colored when it appeared on screen. I was successfully able to do that but now, whenever the already colored code is edited, the text remains colored. (for example the user writes "print" and it gets colored. Then he erases the 't' from the end of it and it becomes 'prin' and it is still colored) I was hoping there was a way to detect the font color of a tkinter.Text index.
For Example:
textArea.get_color("1.0", "1.4"))
# Returns: Black (or whatever color)
So the app can change it if it isn't the proper word and still colored. Can Anyone Help Me?
You can get the color at any text position by first getting the list of tags for that character, and then getting the foreground color of the highest priority tag. If there are no tags with a foreground color, the color is whatever is configured for the text widget.
For example, it might look something like the following:
def get_color(index):
for tag in text.tag_names(index)[::-1]:
fg = text.tag_cget(tag, "foreground")
if fg != "":
return fg
return text.cget("foreground")

Replacing particular text in all sides of a ppt using python-pptx

I am new with python-pptx. But I am familiar with its basic working. I have searched a lot but I could not find a way to change a particular text by another text in all slides. That text may be in any text_frame of a slide. like all slides in a ppt have 'java' keyword, I want to change it by 'python' using python pptx in slides.
for slide in ppt.slides:
if slide.has_text_frame:
#do something with text frames
Something like this should help, you'll need to iterate the shape objects in each slide.shapes and check for a TextFrame and the existence of your keyword:
def replace_text_by_keyword(ppt, keyword, replacement):
for slide in ppt.slides:
for shp in slide.shapes:
if shp.has_text_frame and keyword in shp.text:
thisText = shp.text.replace(keyword, replacement)
shp.text = thisText
This example is just a simple str.replace of course if you have more complicated replacement/text-updating algorithm, you can modify as needed.
In addition when replacing text, you cannot simply replace it, you will loose all formatting.
Your text is contained in text_frame. Text_frame contain paragraphs, and paragraphs are made up of runs. A run contains all your formatting. You need to get to paragraph, then the run, then update text.
"A run exists to provide character level formatting, including font typeface, size, and color, an optional hyperlink target URL, bold, italic, and underline styles, strikethrough, kerning, and a few capitalization styles like all caps." (see reference below)
You'll need to do something like this:
prs = Presentation('data/p1.pptx')
for slide in prs.slides:
for shape in slide.shapes:
if not shape.has_text_frame:
continue
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.text=newText(run.text)
prs.save('data/p1.pptx')
Official documentation(working with text): python-pptx.readthedocs.io
Visual representation of what this means Duplicate post

listctrl new line for an data item

I have created a listctrl with some of the data in the listctrl are very long, and instead of showing all of the text it ends with .... For example Att PSSM_r1_0_T is [-10.179077,0.944198]|Att PSSM_r1_0_Y is.... How would i be able to make it so it shows all of the text. Something like
Att PSSM_r1_0_T is [-10.179077,0.944198]|Att PSSM_r1_0_Y is
[-4.820935,9.914433]|Att PSSM_r1_2_I is [-8.527803,1.953804]|Att PSSM_r1_2_K is [-12.083334,-0.183813]|Att PSSM_r1_2_V is
[-14.112536,5.857771]|1
As the text is very long I would prefer if it covered more than one line.
I don't think that is possible to do with a standard listctrl.
Try poking around at the UltimateListCtrl, being a full owner drawn listctrl it has the ability to change the way its looks far more than a standard listctrl.

wxPython - wx.TextCtrl - forcing styles

just a simple question on a wx.TextCtrl element.
i have this text field where on an application, where the user can add a string on it.
i want a text field with a red text on it.
so i've generated this code:
self.hRepositoryTextfield = wx.TextCtrl(self.hPanel)
self.hRepositoryTextfield.SetDefaultStyle(wx.TextAttr(wx.RED))
if the user copy on this text field some string with applied font on it (for example a black coloured string, or a string with a particular font) the red color, anyway the default style is not respected.
i would like the style i decide for my wx.TextCtrl is always forced according my settings.
how can i do?
thank you in advance
axel
The SetForegroundColor might work on one OS and not on another. It depends on the native widget. I would set the TextCtrl's style flag to wx.TE_RICH. Check out the wxPython demo for an example. You can also use the StyledTextCtrl or FancyText of even the HTMLCtrl.
self.hRepositoryTextfield.SetForegroundColor(wx.RED)
that should work ....
i solved the problem in this way:
in the first part of the code it is defined my textfield style...
self.hRepositoryTextfield.SetStyle(0, len(self.hRepositoryTextfield.GetValue()), wx.TextAttr(wx.RED))
self.hRepositoryTextfield.SetFont(self.hFontLabel)
self.hRepositoryTextfield.Bind(wx.EVT_TEXT, self.forceDefaultStyle)
... then i bind every text change to my forcing-style function:
def forceDefaultStyle(self, event):
hEventObject = event.GetEventObject()
hEventObject.SetStyle(0, len(self.hRepositoryTextfield.GetValue()), wx.TextAttr(wx.RED))
hEventObject.SetFont(self.hFontLabel)
and it works!

Categories

Resources