2013-02-03 148 views
0
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*- 
### BEGIN LICENSE 
# Copyright (C) 2012 Marios Papachristou [email protected] 
# 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瀏覽器。自動刷新標籤python

上面的源代碼是爲了管理主窗口而編寫的。

如何使用webview.get_uri()方法返回值在標籤內持續顯示當前URL?

在此先感謝

回答

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()

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): 
    ... 

這將是一個更好的解決方案,如果你想推廣(你會使標籤裝飾器更通用 - 我把這個簡單的例子)。

+0

所以我必須重新定義** open()**方法? – bolzano

+0

不一定,每次導航發生時都可以調用標籤更新函數,但在我的答案中編寫類似底部代碼片段的包裝會更簡單(如果其他所有內容都保持標準,則不需要對代碼進行其他更改) 。 –