WxPython Web浏览器集标题
我已经使用WxPython在python中创建了一个web浏览器。我已经能够让一切正常运行,前进/后退,重新加载,等等。我唯一的问题是我希望能够将程序顶部的标题设置为网页的标题。我了解如何使用self.setTitletite设置标题,我还知道,通过我所做的一些研究,您可以使用self.browser.GetCurrentTitle获得页面的平铺。使用它的唯一问题是,当用户单击新链接时,它是一次性的,不会刷新自身。我假设在加载新页面时,有某种函数或东西可以捕获,并告诉python这样做:WxPython Web浏览器集标题,python,wxpython,Python,Wxpython,我已经使用WxPython在python中创建了一个web浏览器。我已经能够让一切正常运行,前进/后退,重新加载,等等。我唯一的问题是我希望能够将程序顶部的标题设置为网页的标题。我了解如何使用self.setTitletite设置标题,我还知道,通过我所做的一些研究,您可以使用self.browser.GetCurrentTitle获得页面的平铺。使用它的唯一问题是,当用户单击新链接时,它是一次性的,不会刷新自身。我假设在加载新页面时,有某种函数或东西可以捕获,并告诉python这样做: def
def OnLoad(self, event):
self.webtitle = self.browser.GetCurrentTitle()
self.browser.SetTitle(self.webtitle)
def onLoaded(self, event):
title = self.browser.GetCurrentTitle()
self.SetTitle(title)
#Binding the function the event to the function.
self.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.OnPageLoad, self.browser)
#Function binding to the page loading
def OnPageLoad(self, event):
self.WebTitle = self.browser.GetCurrentTitle()
self.SetTitle(self.WebTitle)
或者沿着这些路线,我只是不确定我在哪里,如何连接或捕获该功能。我已经阅读了wx.html2.WebView的文档,但是我不知道如何做到这一点,我也浏览了这个网站以及一些邮件列表,但是我似乎找不到任何可以解释如何做到这一点的东西
这是运行我的浏览器的主要代码,显然我缩短了它
class PyBrowser(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.SetTitle('PyBrowser')
sizer = wx.BoxSizer(wx.VERTICAL)
self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
#Element Variables
self.browser = wx.html2.WebView.New(self)
self.address = wx.TextCtrl(self, value="http://",size=(500, 26))
self.go = wx.Button(self, label="Go", id=wx.ID_OK)
self.back = wx.BitmapButton(self, -1, wx.Bitmap('img/Back Button.png', wx.BITMAP_TYPE_PNG), size=(26,26) ,style=wx.NO_BORDER)#wx.Button(self, label="Back")
self.forward = wx.BitmapButton(self, -1, wx.Bitmap('img/Forward Button.png', wx.BITMAP_TYPE_PNG), size=(26,26) ,style=wx.NO_BORDER)#wx.Button(self, label="Forward")
self.reload = wx.BitmapButton(self, -1, wx.Bitmap('img/Reload Button.png', wx.BITMAP_TYPE_PNG), size=(26,26) ,style=wx.NO_BORDER)#wx.Button(self, label = "Refresh")
self.result = None
#Nav area
addressarea = wx.BoxSizer()
addressarea.Add(self.address, proportion = 1, border = 0)
...
#Adding elements
sizer.Add(addressarea, proportion = 0, flag = wx.EXPAND, border = 0)
sizer.Add(self.browser, 1, wx.EXPAND, 10)
#Button binding
self.Bind(wx.EVT_BUTTON, self.OnGo, id=wx.ID_OK)
...
#Menu bar stuff
#File Menu
self.fileMenu = wx.Menu()
self.New = self.fileMenu.Append(wx.ID_ANY, 'New Window')
...
#Help Menu
self.helpMenu = wx.Menu()
self.Help = self.helpMenu.Append(wx.ID_ANY, 'Help')
...
self.Bind(wx.EVT_MENU,self.OnHelp,self.Help)
...
#Adding the actual Menu Bar
self.menuBar = wx.MenuBar()
self.menuBar.Append(self.fileMenu, 'File')
self.menuBar.Append(self.helpMenu, 'Help')
self.SetMenuBar(self.menuBar)
self.SetSizer(sizer)
self.SetSize((1000, 700))
def OnGo(self, event):
self.result = self.address.GetValue()
self.browser.LoadURL(self.result)
if __name__ == '__main__':
app = wx.App()
dialog = PyBrowser(None, -1)
dialog.browser.LoadURL("http://www.google.com")
dialog.Show()
app.MainLoop()
总结一下,我要问的是如何将wx.Frame的标题设置为wx.html2.WebView中网页的当前标题,并在每次单击新链接时更改名称 列出了您需要的webview事件我将捕获EVT_webview_加载的事件并更新事件处理程序中的标题。我相信你可以这样做:
def OnLoad(self, event):
self.webtitle = self.browser.GetCurrentTitle()
self.browser.SetTitle(self.webtitle)
def onLoaded(self, event):
title = self.browser.GetCurrentTitle()
self.SetTitle(title)
#Binding the function the event to the function.
self.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.OnPageLoad, self.browser)
#Function binding to the page loading
def OnPageLoad(self, event):
self.WebTitle = self.browser.GetCurrentTitle()
self.SetTitle(self.WebTitle)
更好的方法可能是捕获EVT_WEBVIEW_TITLE_的更改,因为它可以检测页面标题何时更改 根据迈克的话,我发现用一个事件来捕捉它是可行的。因此,我没有使用EVT_WEBVIEW_TITLE_CHANGED,而是使用EVT_WEBVIEW_LOADED,然后将事件绑定到我的函数以处理标题的更改。看起来是这样的:
def OnLoad(self, event):
self.webtitle = self.browser.GetCurrentTitle()
self.browser.SetTitle(self.webtitle)
def onLoaded(self, event):
title = self.browser.GetCurrentTitle()
self.SetTitle(title)
#Binding the function the event to the function.
self.Bind(wx.html2.EVT_WEBVIEW_LOADED, self.OnPageLoad, self.browser)
#Function binding to the page loading
def OnPageLoad(self, event):
self.WebTitle = self.browser.GetCurrentTitle()
self.SetTitle(self.WebTitle)
花了我一分钟,但我发现了你在说什么。这不是一个完整的答案,但确实回答了一点