Search code examples
wxpython

How to unset or disable hyperlinkctrl in wxpython only on some conditions


I have created hyperlinkctrl on my panel.Under some conditions it should be hyperlink but other cases it should be just text not link.

How to do this?

self.Author = wx.HyperlinkCtrl(self, -1, "", "~")

if true:
   self.Author.SetLabel(str(self.aList['Author']))
   self.Author.SetURL("mailto:%s" % str(self.aList['Author']))
else:
   self.Author.SetLabel("N/A")
   self.Author.SetURL("N/A")

In the else case with "N/A" still it is linking.

can any one tell me how to unset the url in wxPython?


Solution

  • Simply toggle between:

    self.Author.Enable()
    

    and:

    self.Author.Disable()
    

    Edit: To redisplay self.Author without the underline which goes with a hyperlink

    import wx
    
    class MyFrame(wx.Frame):
        def __init__(self, parent, id, title):
            wx.Frame.__init__(self, parent, id, title, (-1, -1), wx.Size(300, 200))
            self.panel1 = wx.Panel(self)
            self.Author = wx.HyperlinkCtrl(self.panel1, -1, "", "http://127.0.0.1/some_directory/",pos=(30,50))
            self.Button = wx.Button(self.panel1, -1, "Click Me", pos=(80,100))
            self.Button.Bind(wx.EVT_BUTTON, self.OnButton)
            self.Show()
    
        def OnButton(self,event):
            self.Author.Hide()
            if self.Author.IsEnabled():
                self.Author = wx.StaticText(self.panel1, -1, "http://127.0.0.1/some_directory/", pos=(30,50))
                self.Author.Disable()
            else:
                self.Author = wx.HyperlinkCtrl(self.panel1, -1, "", "http://127.0.0.1/some_directory/", pos=(30,50))
                self.Author.Enable()
            self.Author.Show()
            self.Update()
    
    if __name__ == '__main__':
        app = wx.App()
        frame = MyFrame(None, -1, 'Hyperlink')
        app.MainLoop()