如何在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链接到我的网站。
答案 0 :(得分:5)
您想要的功能实际上是内置于Python中的。检查webbrowser API。具体来说,请查看webbrowser.open()
函数。
答案 1 :(得分:0)
在特定菜单的事件处理程序中,您可以使用Python自己的webbrowser,open()方法。或者您可以使用子进程打开特定的浏览器(假设安装了多个浏览器)。后者会给你更多的控制权。还有os.startfile(),但我不确定在这种情况下是否可行。