0
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
### BEGIN LICENSE
# Copyright (C) 2012 Marios Papachristou mrmarios97@gmail.com
# This program is free software: you can redistribute it and/or modify it 
# under the terms of the GNU General Public License version 3, as published 
# by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful, but 
# WITHOUT ANY WARRANTY; without even the implied warranties of 
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR 
# PURPOSE.  See the GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License along 
# with this program.  If not, see <http://www.gnu.org/licenses/>.
### END LICENSE


import gettext
from gettext import gettext as _
gettext.textdomain('quickbrowser')

from gi.repository import Gtk, WebKit # pylint: disable=E0611
import logging
logger = logging.getLogger('quickbrowser')

from quickbrowser_lib import Window
from quickbrowser.AboutQuickbrowserDialog import AboutQuickbrowserDialog
from quickbrowser.PreferencesQuickbrowserDialog import PreferencesQuickbrowserDialog


# See quickbrowser_lib.Window.py for more details about how this class works
class QuickbrowserWindow(Window):
    __gtype_name__ = "QuickbrowserWindow"

    def finish_initializing(self, builder): # pylint: disable=E1002
        """Set up the main window"""
        super(QuickbrowserWindow, self).finish_initializing(builder)

        self.AboutDialog = AboutQuickbrowserDialog
        self.PreferencesDialog = PreferencesQuickbrowserDialog
        self.goBack = self.builder.get_object('goBack')
        self.homeButton = self.builder.get_object('homeButton')
        self.refreshButton = self.builder.get_object('refreshButton')
        self.goButton = self.builder.get_object('goButton')
        self.currentaddresslabel = self.builder.get_object('currentaddresslabel')
        self.addressbar = self.builder.get_object('addressbar')
        self.viewwindow = self.builder.get_object('viewwindow')
        self.goForward = self.builder.get_object('goForward')
        self.zoomIn = self.builder.get_object('zoomIn')
        self.zoomOut = self.builder.get_object('zoomOut')
        self.webview = WebKit.WebView()
        self.viewwindow.add(self.webview)
        self.webview.show()




    def on_addressbar_activate(self, widget):
        address = widget.get_text()
        self.webview.open(address)


    def on_refreshButton_clicked(self, widget):
        self.webview.reload()

    def on_goBack_clicked(self,widget):
        self.webview.go_back();

    def on_goForward_clicked(self,widget):
        self.webview.go_forward();

    def on_zoomIn_activate(self,widget):
        self.webview.zoom_in();

    def on_zoomOut_activate(self,widget):
        self.webview.zoom_out();

    def on_goButton_clicked(self,widget):
        self.webview.open(self.addressbar.get_text())

我目前正在使用 python-webkit 在 Python 中开发 Web 浏览器。

上面的源代码是为了管理主窗口而编写的。

如何在标签内使用 webview.get_uri() 方法返回值不断显示当前 URL?

提前致谢

4

1 回答 1

0
while True:
    updateLocationBar() # ;)

当然位置栏已经提供了这个?self.webview.open()您可以通过编写类似的包装器来调用您的标签修改函数

def self.webview.mysuperawesomeopen(uri):
    updateLocationBar()
    self.webview.open(uri)

然后适当地修改调用。或者,如果您不想更改调用,只需移动现有.open()功能,例如

self.webview.openit = self.window.open
def self.window.open(uri):
    updateLocationBar()
    self.webview.openit(uri)

编辑:您也可以在self.window.open()like上使用函数装饰器

def labelled(f):
    def labelgo(*args, **kwargs):
        updateLocationBar(*args, **kwargs)
        self.webview.open(*args, **kwargs)
    return labelgo

@labelled # You add this and leave the rest of self.window.open alone
def self.window.open(uri):
    ...

如果您想进行概括,这将是一个更好的解决方案(并且您会使标记的装饰器更通用 - 我以这个简单为例)。

于 2013-02-03T07:14:24.590 回答