![如何让网站标题显示在菜单区域/网页标题中使用 python/快速创建的网络浏览器?](https://linux22.com/image/1098878/%E5%A6%82%E4%BD%95%E8%AE%A9%E7%BD%91%E7%AB%99%E6%A0%87%E9%A2%98%E6%98%BE%E7%A4%BA%E5%9C%A8%E8%8F%9C%E5%8D%95%E5%8C%BA%E5%9F%9F%2F%E7%BD%91%E9%A1%B5%E6%A0%87%E9%A2%98%E4%B8%AD%E4%BD%BF%E7%94%A8%20python%2F%E5%BF%AB%E9%80%9F%E5%88%9B%E5%BB%BA%E7%9A%84%E7%BD%91%E7%BB%9C%E6%B5%8F%E8%A7%88%E5%99%A8%EF%BC%9F.png)
你好,我正在使用快速和 Python 的混合创建一个网络浏览器,我想让网站标题显示在页面的最顶部。我不确定我需要添加到文件中的任何代码,我找遍了整个互联网但都没有用。
这是迄今为止我为浏览器获得的代码:
import gettext
from gettext import gettext as _
gettext.textdomain('dmbrowser')
from gi.repository import Gtk, WebKit # pylint: disable=E0611
import logging
logger = logging.getLogger('dmbrowser')
from dmbrowser_lib import Window
from dmbrowser.AboutDmbrowserDialog import AboutDmbrowserDialog
from dmbrowser.PreferencesDmbrowserDialog import PreferencesDmbrowserDialog
# See dmbrowser_lib.Window.py for more details about how this class works
class DmbrowserWindow(Window):
__gtype_name__ = "DmbrowserWindow"
def finish_initializing(self, builder): # pylint: disable=E1002
"""Set up the main window"""
super(DmbrowserWindow, self).finish_initializing(builder)
self.AboutDialog = AboutDmbrowserDialog
self.PreferencesDialog = PreferencesDmbrowserDialog
# Code for other initialization actions should be added here.
self.backbutton = self.builder.get_object("backbutton")
self.forwardbutton = self.builder.get_object("forwardbutton")
self.refreshbutton = self.builder.get_object("refreshbutton")
self.stopbutton = self.builder.get_object("stopbutton")
self.urlentry = self.builder.get_object("urlentry")
self.scrolledwindow = self.builder.get_object("scrolledwindow")
self.toolbar = self.builder.get_object("toolbar")
context = self.toolbar.get_style_context()
context.add_class(Gtk.STYLE_CLASS_PRIMARY_TOOLBAR)
self.webview = WebKit.WebView()
self.scrolledwindow.add(self.webview)
self.webview.show()
def on_backbutton_clicked(self, widget):
self.webview.go_back()
def on_forwardbutton_clicked(self,widget):
self.webview.go_forward()
def on_refreshbutton_clicked(self, widget):
self.webview.reload()
def on_stopbutton_clicked(self, widget):
self.webview.stop_loading()
def on_urlentry_activate(self, widget):
url = widget.get_text()
self.webview.open("http://" + url)
print url
答案1
未经测试,但以下内容应该可行。
self.webview.connect('notify::title', self._title_changed_cb)
def _title_changed_cb(self, webview, title):
self.set_title(webview.get_title())
您可能需要一个更好的标题,例如"%s - %s" % (title, APPNAME)
。
编辑:显然您不能title
直接使用该属性,因为它是 GParamString 类型。只需从 webview 中检索标题即可。
答案2
self.webview.connect('load_committed', self.change_url)
def change_url(self, widget, data=None):
self.urlentry.set_text( widget.get_main_frame().get_uri() )
它会起作用检查