如何在某些条件下在wxpython中取消设置或禁用超链接控制

时间:2016-03-15 09:33:23

标签: wxpython

我在我的面板上创建了hyperlinkctrl。在某些情况下,它应该是超链接,但在其他情况下,它应该只是文本而不是链接。

怎么做?

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")

"N/A"的其他情况下,它仍然是链接。

任何人都可以告诉我如何在wxPython中取消设置url?

1 个答案:

答案 0 :(得分:0)

只需在:

之间切换
self.Author.Enable()

和:

self.Author.Disable()

编辑: 要重新显示self.Author而没有带超链接的下划线

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()