Welcome folks today in this blog post we will be displaying custom html tags
in gui desktop app in python. All the full source code of the application is given below.
Get Started
In order to get started you need to install the below library using the pip
command as shown below
pip install wxpython
After installing this library you need to make an app.py
file and copy paste the following code
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
import wx import wx.html page = """<html><body> This silly example shows how custom tags can be defined and used in a wx.HtmlWindow. We've defined a new tag, <blue> that will change the <blue>foreground color</blue> of the portions of the document that it encloses to some shade of blue. The tag handler can also use parameters specifed in the tag, for example: <ul> <li> <blue shade='sky'>Sky Blue</blue> <li> <blue shade='midnight'>Midnight Blue</blue> <li> <blue shade='dark'>Dark Blue</blue> <li> <blue shade='navy'>Navy Blue</blue> </ul> </body></html> """ class BlueTagHandler(wx.html.HtmlWinTagHandler): def __init__(self): wx.html.HtmlWinTagHandler.__init__(self) def GetSupportedTags(self): return "BLUE" def HandleTag(self, tag): old = self.GetParser().GetActualColor() clr = "#0000FF" if tag.HasParam("SHADE"): shade = tag.GetParam("SHADE") if shade.upper() == "SKY": clr = "#3299CC" if shade.upper() == "MIDNIGHT": clr = "#2F2F4F" elif shade.upper() == "DARK": clr = "#00008B" elif shade.upper == "NAVY": clr = "#23238E" self.GetParser().SetActualColor(clr) self.GetParser().GetContainer().InsertCell(wx.html.HtmlColourCell(clr)) self.ParseInner(tag) self.GetParser().SetActualColor(old) self.GetParser().GetContainer().InsertCell(wx.html.HtmlColourCell(old)) return True wx.html.HtmlWinParser_AddTagHandler(BlueTagHandler) class MyHtmlFrame(wx.Frame): def __init__(self, parent, title): wx.Frame.__init__(self, parent, -1, title) html = wx.html.HtmlWindow(self) if "gtk2" in wx.PlatformInfo: html.SetStandardFonts() html.SetPage(page) app = wx.PySimpleApp() frm = MyHtmlFrame(None, "Custom HTML Tag Handler") frm.Show() app.MainLoop() |
If you execute the above python script
by typing the below command
python app.py