python - 我如何在 wxpython 中添加指向我的菜单项的链接?

标签 python wxpython

我如何在 wxpython 中添加指向我的菜单项的链接?我想要的是当用户单击菜单项时它将转到我的网站。这可能吗?

提前致谢!!哦,我对编程和 python 还很陌生,所以如果你能把它简化一点,我将不胜感激!!谢谢

这是我的代码:

    import wx

class MainWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Python Test App',size=(600,400))
        panel=wx.Panel(self)
        wx.Frame.CenterOnScreen(self)
        self.SetBackgroundColour(wx.BLACK)

    ##MENU AND STATUS BAR
        status=self.CreateStatusBar()
        menubar=wx.MenuBar()
        file_menu=wx.Menu()
        help_menu=wx.Menu()

        ID_FILE_NEW = 1
        ID_FILE_EXIT = 2

        ID_HELP_ABOUT = 3
        ID_HELP_WEB = 4


        file_menu.Append(ID_FILE_NEW,"New Window","This will open a new window")
        file_menu.Append(ID_FILE_EXIT,"Exit","This will exit the program")

        help_menu.Append(ID_HELP_ABOUT,"About","This will tell you about %name%")
        help_menu.Append(ID_HELP_WEB,"Visit Website","This will take you to worm-media.host56.com")


        menubar.Append(file_menu,"File")
        menubar.Append(help_menu,"Help")
        self.SetMenuBar(menubar)

        self.Bind(wx.EVT_MENU, self.newWin, None, 1)
        self.Bind(wx.EVT_MENU, self.close, None, 2)
        self.Bind(wx.EVT_MENU, self.about, None, 3)
        ##self.Bind(wx.EVT_MENU, self.web, None, 4)

        heading = wx.StaticText(panel, -1, 'Welcome to My App', (144,10))
        heading.SetForegroundColour(wx.RED)
        font1 = wx.Font(20, wx.DEFAULT, wx.NORMAL, wx.BOLD)
        heading.SetFont(font1)


    def newWin(self, event):
        self.new = NewWindow(parent=None, id=-1)
        self.new.Show()

    def close(self, event):
        box=wx.MessageDialog(None, 'Are you sure you want to exit?', 'Exit program?', wx.YES_NO)
        answer=box.ShowModal()
        if answer==wx.ID_YES:
            self.Destroy()

    def about(self, event):
        self.new = AboutWindow(parent=None, id=-1)
        self.new.Show()

    ##def web(self, event):


class NewWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self, parent, id, 'New Window', size=(400,300))
        wx.Frame.CenterOnScreen(self)
        ##panel2=wx.Panel(self)
        name_text = wx.StaticText(self, -1, 'What is your name?')
        name = wx.TextCtrl(self, -1, '', (100,0))
        font2 = wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        name_text.SetFont(font2)


class AboutWindow(wx.Frame):

    def __init__(self,parent,id):
        wx.Frame.__init__(self, parent, id, 'About', size=(300,190))
        wx.Frame.CenterOnScreen(self)
        self.SetBackgroundColour(wx.WHITE)
        about_bg = 'about_bg.jpg'
        bmp1 = wx.Image(about_bg, wx.BITMAP_TYPE_ANY).ConvertToBitmap()
        self.bitmap1 = wx.StaticBitmap(self, -1, bmp1, (0,0))

        ##ABOUT_TEXT
        by_text =  wx.StaticText(self, -1, 'Made By: Worm', (50,70))
        version_text = wx.StaticText(self, -1, 'Version: 1.0', (50,90))
        website_text = wx.StaticText(self, -1, 'Website: worm-media.host56.com', (50,110))

        ##ABOUT_FONTS
        by_font = wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        version_font = wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL)
        website_font = wx.Font(10, wx.DEFAULT, wx.NORMAL, wx.NORMAL)

        by_text.SetFont(by_font)
        version_text.SetFont(version_font)
        website_text.SetFont(website_font)


        ##ABOUT_COLORS
        by_text.SetBackgroundColour(wx.WHITE)
        version_text.SetBackgroundColour(wx.WHITE)
        website_text.SetBackgroundColour(wx.WHITE)


##RUN##

if __name__=='__main__':
        app=wx.PySimpleApp()
        frame=MainWindow(parent=None,id=-1)
        frame.Show()
        app.MainLoop()

到目前为止,这是我的代码。我真的没有尝试过任何东西,因为我什至不知道从哪里开始。我试着在网上寻找如何做到这一点,但一无所获。我想我想问的只是如何从 about/Visit Website 下的菜单栏中用 python 链接到我的网站。

最佳答案

您想要的功能实际上内置于 Python 中。检查webbrowser API .具体来说,查看 webbrowser.open() 函数。

关于python - 我如何在 wxpython 中添加指向我的菜单项的链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11236485/

相关文章:

python - 使用 wxglade 创建的 "custom"对话框中的确定/取消订单

python - Python 和 wxPython 可以在 Windows 98 上运行吗?

python-3.x - 有什么方法可以在 wxPython 应用程序中显示来自 base64 数据的图像吗?

python - 如何在wxpython中关闭窗口时结束线程

python - Anaconda Prompt 不会启动 _NamespacePath

python - python中的安全身份验证系统?

python - GPU内存溢出: Hyperparameter Tuning loop with varying models

python - 元组的最大值

python - wxPython 事件在启动时启动

c++ - 通过 python ctypes 使用时,opencv 调用共享库段错误