for 循环中的 WebKit.WebView 信号问题
Issue with WebKit.WebView signals in for loop
您好,我是 Python 和 WebKit 的新手。
所以我创建了一个简单的 Gtk 应用程序,它在 WebKit.WebView 一个 Internet 页面列表中加载,当加载一个页面时,有一个 DOM 绑定来获取网站的标题。这是通过一个 for 循环发生的,该循环一个接一个地加载站点。但是在 for 循环的第一个 运行 处理 by-pass WebView.connect 的 'load-finished' 信号并开始从列表的第二项获取结果。
我需要一些帮助...
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import threading
from time import sleep
import gi
gi.require_version('WebKit', '3.0')
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, WebKit
source = ["http://imdb.com", "http://cnn.com",
"http://wikipedia.org", "http://nokia.com"]
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_border_width(10)
self.set_default_size(1024, 768)
self.notebook = Gtk.Notebook()
self.add(self.notebook)
self.page1 = Gtk.Box()
self.page1.set_border_width(10)
self.page1.set_margin_left(5)
self.page1.set_margin_right(5)
self.page1.set_margin_top(5)
mainbox = Gtk.Box()
box1 = Gtk.VBox()
box2 = Gtk.Box()
buttoncall = Gtk.Button("Start")
buttoncall.connect('clicked', self.help)
buttoncall.set_size_request(50, 50)
scrolled = Gtk.ScrolledWindow()
scrolled.set_border_width(10)
scrolled.set_size_request(300, 300)
scrolled.set_policy(Gtk.PolicyType.ALWAYS, Gtk.PolicyType.ALWAYS)
self.web1 = WebKit.WebView()
self.web1.set_editable(False)
self.web1.load_uri("")
scrolled.add(self.web1)
box1.pack_start(buttoncall, False, False, 10)
mainbox.pack_start(box1, False, False, 0)
mainbox.pack_start(box2, True, True, 0)
box1.add(scrolled)
self.page1.pack_start(mainbox, True, True, 0)
self.notebook.append_page(self.page1, Gtk.Label("Search info"))
def get_source(self):
print("enterded source")
html = self.web1.get_main_frame().get_title()
return html
def help(self, widget):
threading.Thread(target=self.start_it, args=(widget)).start()
def start_it(self,widget):
for i in range(len(source)):
print(source[i])
self.web1.load_uri(source[i])
while (int(self.web1.get_load_status() != 2)):
sleep(0.5)
print("loading page")
self.web1.connect('load-finished', self.finished_load)
def finished_load(self, web1, frame):
print("get finished")
c = self.get_source()
print(c)
window = MainWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
您不能从单独的线程调用涉及任何 UI 的函数,无论是 GTK+、WebKit 还是其他。您必须从调用 Gtk.main()
的线程中 运行 它们,也许使用 GLib.idle_add()
.
您好,我是 Python 和 WebKit 的新手。 所以我创建了一个简单的 Gtk 应用程序,它在 WebKit.WebView 一个 Internet 页面列表中加载,当加载一个页面时,有一个 DOM 绑定来获取网站的标题。这是通过一个 for 循环发生的,该循环一个接一个地加载站点。但是在 for 循环的第一个 运行 处理 by-pass WebView.connect 的 'load-finished' 信号并开始从列表的第二项获取结果。 我需要一些帮助...
# !/usr/bin/python
# -*- coding: UTF-8 -*-
import threading
from time import sleep
import gi
gi.require_version('WebKit', '3.0')
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, WebKit
source = ["http://imdb.com", "http://cnn.com",
"http://wikipedia.org", "http://nokia.com"]
class MainWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self)
self.set_border_width(10)
self.set_default_size(1024, 768)
self.notebook = Gtk.Notebook()
self.add(self.notebook)
self.page1 = Gtk.Box()
self.page1.set_border_width(10)
self.page1.set_margin_left(5)
self.page1.set_margin_right(5)
self.page1.set_margin_top(5)
mainbox = Gtk.Box()
box1 = Gtk.VBox()
box2 = Gtk.Box()
buttoncall = Gtk.Button("Start")
buttoncall.connect('clicked', self.help)
buttoncall.set_size_request(50, 50)
scrolled = Gtk.ScrolledWindow()
scrolled.set_border_width(10)
scrolled.set_size_request(300, 300)
scrolled.set_policy(Gtk.PolicyType.ALWAYS, Gtk.PolicyType.ALWAYS)
self.web1 = WebKit.WebView()
self.web1.set_editable(False)
self.web1.load_uri("")
scrolled.add(self.web1)
box1.pack_start(buttoncall, False, False, 10)
mainbox.pack_start(box1, False, False, 0)
mainbox.pack_start(box2, True, True, 0)
box1.add(scrolled)
self.page1.pack_start(mainbox, True, True, 0)
self.notebook.append_page(self.page1, Gtk.Label("Search info"))
def get_source(self):
print("enterded source")
html = self.web1.get_main_frame().get_title()
return html
def help(self, widget):
threading.Thread(target=self.start_it, args=(widget)).start()
def start_it(self,widget):
for i in range(len(source)):
print(source[i])
self.web1.load_uri(source[i])
while (int(self.web1.get_load_status() != 2)):
sleep(0.5)
print("loading page")
self.web1.connect('load-finished', self.finished_load)
def finished_load(self, web1, frame):
print("get finished")
c = self.get_source()
print(c)
window = MainWindow()
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
您不能从单独的线程调用涉及任何 UI 的函数,无论是 GTK+、WebKit 还是其他。您必须从调用 Gtk.main()
的线程中 运行 它们,也许使用 GLib.idle_add()
.