【问题标题】:Issue with WebKit.WebView signals in for loop循环中的 WebKit.WebView 信号问题
【发布时间】:2016-07-18 11:55:20
【问题描述】:

您好,我是 Python 和 WebKit 的新手。 所以我创建了一个简单的 Gtk 应用程序,它在 WebKit.WebView 中加载一个 Internet 页面列表,当一个页面被加载时,有一个 DOM 绑定来获取网站的标题。这是通过一个一个一个加载站点的for循环发生的。但在第一次运行 for 循环时,该过程绕过 WebView.connect 的“加载完成”信号并开始从列表的第二项获取结果。 我需要一些帮助...

# !/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()

【问题讨论】:

    标签: python python-3.x webkit gtk gtk3


    【解决方案1】:

    您不能从单独的线程调用涉及任何 UI 的函数,无论是 GTK+、WebKit 还是其他任何非 UI。您必须从调用Gtk.main() 的线程运行它们,可能使用GLib.idle_add()

    【讨论】:

      猜你喜欢
      • 2021-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      • 2018-04-13
      相关资源
      最近更新 更多