你好,我正在使用快速和 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() )
它会起作用检查