【问题标题】:How would I add a link to my menu item in wxpython?如何在 wxpython 中添加指向我的菜单项的链接?
【发布时间】:2012-06-28 00:16:00
【问题描述】:

如何在 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 链接到我的网站。

【问题讨论】:

  • 我通过添加我的代码来编辑我之前的问题。请看一下
  • 顺便说一句,您不应该这样定义您的 ID。试试wx.NewId()。这样您就可以确定没有两个对象具有相同的 ID。

标签: python wxpython


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在特定菜单的事件处理程序中,可以使用 Python 自己的 webbrowser,open() 方法。或者您可以使用 subprocess 打开特定的浏览器(假设安装了多个浏览器)。后者会给你更多的控制权。还有 os.startfile(),但我不确定在这种情况下是否可行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-27
      • 1970-01-01
      • 2023-03-21
      • 2018-10-05
      • 2013-08-09
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多