1

How would I add a link to my menu item in wxpython? What I want is when the user clicks the menu item it will go to my website. Is this even possible?

Thanks in advance!! Oh and I am fairly new to programming and to python so if you could dumb it down alittle that would be appreciated!! Thanks

THIS IS MY CODE:

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

This is my code so far. I really haven't tried anything with this because I'm not even sure where to start. I tried looking how to do this online and came up empty. I guess all I'm asking is how to make a link in python to my website from the menu bar under about/Visit Website.

4

2 に答える 2

5

The functionality you want is actually built into Python. Check the webbrowser API. Specifically, look at the webbrowser.open() function.

于 2012-06-28T01:57:39.387 に答える
0

In the event handler for the specific menu, you can use Python's own webbrowser,open() method. Or you can use subprocess to open a specific browser (assuming there's more than one installed). The latter would give you more control. There's also os.startfile(), but I'm not sure that would work in this case.

于 2012-06-28T14:04:13.307 に答える