wx python,HTTP link in text - python

I wanna add a link in a text but a found a solution, but it's don't work as well...
Then my code look like :
self.AddText('Some text here...'.decode('utf-8'))
self.AddText('Some text here too...'.decode('utf-8'))
self.linkweb = hl.HyperLinkCtrl(self, wx.ID_ANY, 'my_adress#box.net'.decode('utf-8'), URL="http://www.my_website.com/", pos=(545,88))
Then have you some ideas too make that more simple, cause here, i have to put my linkweb with the position in my frame... It's not really easy, and don't have the same positions, on all PC...
Thanks you all ;)

The wx.richtext.RichTextCtrl supports inserting hyperlinks in its text. You might be able to use the wx.TE_RICH style flag with a regular wx.TextCtrl to get the same capabilities, but I'm not sure if that will work. See the wxPython demo for a good example.

Related

Scrapy/XPath extract text from different tags (<p> OR <li>)

I'm trying to scrape text from a website. Sometimes, the text is written in bullet points, sometimes just in plain text.
Text in Bullet points (XPath): /article/div[#class='border-bottom-grau'][1]/ul/li[1]
Text in Plain text (XPath): /article/div[#class='border-bottom-grau'][1]/p
I need the respectice text to be extracted (without div/ul/li/p tags etc). This is what I have tried so far:
info_Aufgabengebiet = info.xpath(".//article/div[#class='border-bottom-grau'][1][descendant::text()]").extract()
Output: see image Output
I also experimented with descendant-or-self, a /text() at the end, but neither worked. Simply, I want to extract all text no matter in bullet point or plain text. Pullet points should just be added, maybe with a ";" or ",".
Any help is much appreciated
Thanks
You can use XPath with combined conditions
"/article/div[#class='border-bottom-grau'][1]/ul/li[1] | /article/div[#class='border-bottom-grau'][1]/p"
The union operator | mentioned in the other answer is a good solution. Alternately, depending on your output needs, you might try
/article/div[#class='border-bottom-grau'][1]//*[self::p or self::li]

Python Tkinter cant unbind Triple Click

So I have some tags (strings the user made), they are on display in a text widget at all times. Basically if there are tags to display, you need to be able to triple click on them and it will lead to a editing menu. But if there aren't any tags to display, I don't want people to be able to triple click on it.
So my thought was bind triple click to the appropriate function if there are tags to display, and unbind it if there aren't any tags to display.
for tag in sorted(tags_pre_listed):#This loop will just check the tags and OK them for use.
if tag[0:4]=='TAG-' and tag not in used_tags: # Just avoids duplicates.
tags_display_box.insert(Tk.END, '#'+tag[4:]+' ') #inserts the tag to the display.
used_tags.append(tag)
if len(used_tags)>0: #If any tags were used to display, it will bind Triple click.
tags_display_box.bind("<Triple-1>", delete_tag)
else: #This is where it tries to unbind if there are no tags, but fails.
tags_display_box.unbind('<Button-1>',"<Triple-1>")
The issue I get is
TclError: can't delete Tcl command
Sorry It may be a rookie answer for all I know but I have done my research and can't find a way around it at all T-T
Thanks so much for reading and for any advice!
The line:
tags_display_box.unbind('<Button 1>',"<Triple-1>")
should read:
tags_display_box.unbind('<Triple-1>')
As it is you're trying to unbind something that's not bound from a command that does not exist.

PyQt. Highliting some places in text using QSyntaxHighlighter

I have had a program in PyQt5. The program have had a QTextEdit field for printing clear HTML using Python’s method html.escape(). Every string have added in the field using .append(). Some tags in some strings needed to be highlighted. And I have used next code:
self.textEdit.append('{0}{color}{1}{endcolor}{2}'.format(begin,
color_text, end, color='<font color="red">', endcolor='</font>')
Lately I found a module in Internet that can print strings number. This module use QPlainTextEdit. Also I found a module for syntax highlight, that was arranged by QSyntaxHighlighter and it’s for QPlainTextEdit too. It all works good, but now I can‘t highlight additional tags using code above. I have to highlight not only html syntax, but also additional some tags by other color. I tried to create another QSyntaxHighlighter that would highlight a whole text it get.
def format(color, style=''):
_color = QColor()
_color.setNamedColor(color)
_format = QTextCharFormat()
_format.setForeground(_color)
return _format
class PythonHighlighter (QSyntaxHighlighter):
def __init__(self, document):
QSyntaxHighlighter.__init__(self, document)
def highlightBlock(self, text):
self.setFormat(0, len(text), format('red'))
self.setCurrentBlockState(0)
Then in necessary place I write:
highlight = syntax.PythonHighlighter(self.textEdit.document())
self.textEdit.show()
self.textEdit.appendPlainText(...)
I thought to apply this highlighter to places of html that between{color} and {endcolor} above in code. Can I apply highlighter only to part of field? How? And if I can't, which are there methods to solve my problem?
P.S. Sorry for mistakes in my English. You can tell me about them.

Python Selenium xpath white spaces

I would like to get usernames from one page, but for some reason I just can't get it working..
After browsing internet and other Stackoverflow posts; I think the problem is that there are white spaces in #class, and it just doesn't work. Then I found solution to do it the other way, but the problem is that after first class, I would like to go to second class as well, and this is the only way I know I can do it with find_elements_by_xpath.
Picture to inspect element of what I want to get
In this picture, 'text' is one of the usernames it should scrap.
My code:
usernames = driver.find_elements_by_xpath("//a[#class='kik-widget card card-just-text card-with-shadow']//h3[#class='kik-widget-text-muted']")
usernames2 = [x.text for x in usernames]
print(usernames2)
Any help much appreciated.
Try to use contains in your case to avoid spaces.
For example:
//h3[contains(#class, 'kik-widget-text-muted')
Contains will skip spaces.
Hope this will help you.

Python - remove excessive html tags

So I'm currently having this text:
<i>This article is written </i><i>TEST</i><i>.</i>
I think this is a good HTML, however, I want to clean it up, remove all the excessive <i> tags and simplify it to a single <i> tag:
<i>This article is written TEST.</i>
I tried to clean it up myself, but I'd need to look ahead for the text, and haven't had much success with this. Is there a package I can use or a way that I can do it or I'd have to manually do it?
Thank you
The use of an HTML parser is definitely the most reliable solution. It would be able to cope with the tags split across many lines.
The following will solve your example, but probably not much more...
def OuterI(text):
outer = re.search("(.*?)(\<i\>.*<\/i\>)(.*)", text)
if outer:
return "%s<i>%s</i>%s" % (outer.group(1), re.sub(r"(\<\/?[iI]\>)", "", outer.group(2)), outer.group(3))
else:
return text
print OuterI('<i>This article is written </i><i>TEST</i><i>.</i>')
print OuterI('text before <i>This article is written </i><i>TEST</i><i>.</i> text after')

Categories