【问题标题】:How to add hyperlinks in the help of a wxpython app?如何在 wxpython 应用程序的帮助下添加超链接?
【发布时间】:2013-11-07 11:39:08
【问题描述】:

我正在实现在 wxPython 中完成的应用程序的帮助菜单。到目前为止,我正在使用在框架中打开的 txt 文件。我想在帮助文本中添加超链接,以便在同一框架中打开其他 txt 文件。但是,我不知道该怎么做。我什至不知道这是否是实现帮助菜单的最优雅方式。任何建议都会非常有用。

您可以在下面找到我正在使用的部分代码(您需要一个名为“Help_Main_App.txt”的 txt 文件):

import wx

class Help_Frame(wx.Frame):
    title = "Help, I need somebody, help..."
    def __init__(self):
        wx.Frame.__init__(self, wx.GetApp().TopWindow, title=self.title, size=(450,500)) 
        self.CreateStatusBar()
        panel = wx.Panel(self, wx.ID_ANY)
        panel.SetBackgroundColour('#ededed')
        self.Centre()
        vBox = wx.BoxSizer(wx.VERTICAL)
        hBox = wx.BoxSizer(wx.HORIZONTAL)
        self.textbox = wx.TextCtrl(panel, style=wx.TE_MULTILINE, size=(-1, 295))
        hBox.Add(self.textbox, 1, flag=wx.EXPAND)
        vBox.Add(hBox, flag=wx.EXPAND | wx.LEFT | wx.RIGHT, border=10)
        panel.SetSizer(hBox)
        defaultdir, filename = './', 'Help_Main_App.txt'
        self.filePath = '/'.join((defaultdir, filename))
        self.textbox.LoadFile(self.filePath)
        self.textbox.Disable()


class Main_Window(wx.Frame):
    def __init__(self, parent, title):
        #wx.Frame.__init__(self, parent, title = title, pos = (0, 0), size = wx.DisplaySize())
        wx.Frame.__init__(self, parent, title=title, size=(1000,780))
        self.Center()

        # Setting up the menu.
        filemenu = wx.Menu()
        helpmenu = wx.Menu()
        menuExit = filemenu.Append(wx.ID_EXIT,"&Exit"," Close window and exit program")
        menuHelp = helpmenu.Append(wx.ID_HELP, "&Help"," Help of this program")

        # Creating the menubar.
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
        menuBar.Append(helpmenu,"&Help") # Adding the "helpmenu" to the MenuBar
        self.SetMenuBar(menuBar)         # Adding the MenuBar to the Frame content.

        # Set event handlers
        self.Bind(wx.EVT_MENU, self.OnHelp, menuHelp)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)


    def OnHelp(self,e):
        Help_Frame().Show()

    def OnExit(self,e):
        self.Close(True)  # Close the frame.                

def main():
    app = wx.App(False)
    frame = Main_Window(None, "Main App")
    frame.Show()
    app.MainLoop()


if __name__ == "__main__" :
    main()

【问题讨论】:

    标签: python hyperlink wxpython helper


    【解决方案1】:

    迟到了,但只是为了完整起见(看到 OP 的代码使用 wx.TextCtrl 来显示帮助文本),这里有一个关于如何添加和启动超链接的示例使用wx.TextCtrl(我附上了对代码cmets的任何解释):

    class HelpDialog(wx.Dialog):
        """Help Dialog."""
    
        def __init__(self, parent, title, style):
            """Init."""
            wx.Dialog.__init__(self, parent=parent, id=wx.ID_ANY, 
                title=title, pos=wx.DefaultPosition, size=wx.DefaultSize, style=style)
            # We need the 'wx.TE_AUTO_URL' style set.
            self.help = wx.TextCtrl(self, wx.ID_ANY, '', DPOS, DSIZE,
                wx.TE_AUTO_URL|wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2|wx.TE_WORDWRAP)
            # Events - this is the interesting part, 
            # we catch the mouse on hovering the hyperlink:
            self.help.Bind(wx.EVT_TEXT_URL, self.openHlpUrl)
            # Show dialog
            self.ShowModal()
            
        def openHlpUrl(self, event):
            """Open help URL."""
            # We get the starting and ending points on 
            # the text stored in our ctrl from this event
            # and we slice it: 
            url = self.help.GetValue()[event.GetURLStart():event.GetURLEnd()]
            # We want to capture the left up mouse event 
            # when hovering on the hyperlink:
            if event.MouseEvent.LeftDown():
                # Let's be wxpythion native and launch the browser this way:
                wx.LaunchDefaultBrowser(url)
    

    【讨论】:

      【解决方案2】:

      如果您能够使用 wxPython 2.9.4 或更高版本,您可以考虑使用更高级的支持 CSS 和 javascript 的 html2 webview 来回答 Mikes 的进一步问题。使用它,您可以将帮助作为一个可以在程序中查看的简单网站。

      http://wxpython.org/Phoenix/docs/html/html2.WebView.html

      还值得一提的是,如果(出于某种奇怪的原因)您不想使用 StyledTxtCtrl,您可以使用 StyledTxtCtrl 获得类似的结果。

      【讨论】:

        【解决方案3】:

        我建议使用 HTMLWindow 来完成类似的简单操作。它只能处理简单的 HTML,所以不要试图用它做网站,因为 HTMLWindow 不支持 CSS 或 javascript。

        我用它写了一个简单的关于框。你可以在这里阅读:

        http://www.blog.pythonlibrary.org/2008/06/11/wxpython-creating-an-about-box/

        基本思想是继承 HTMLWindow 并覆盖它的 OnLinkClicked 方法。然后就可以使用Python的webbrowser打开用户的默认浏览器了。或者您可以尝试使用子进程,尽管除非您始终知道目标计算机上安装了什么,否则这种方法不太可能奏效。

        【讨论】:

          猜你喜欢
          • 2011-05-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-22
          • 1970-01-01
          • 1970-01-01
          • 2011-01-22
          • 2011-06-28
          相关资源
          最近更新 更多