Python GTK + webkit - 在gtk.main()之后插入JavaScript [英] Python GTK + webkit - insert JavaScript after gtk.main()

查看:120
本文介绍了Python GTK + webkit - 在gtk.main()之后插入JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在终端上试了这个,一切正常,但是如果我运行这个脚本,我不能在gtk.main()之后插入JavaScript

  import gtk 
import webkit

w = gtk.Window()
b = webkit.WebView()
w.add(b)
b.open('http://example.com')
w.show_all()

gtk.main()#这个我不会在终端
内运行
#和显示窗口后我想插入一些JavaScript代码

js ='alert(a);'
b.execute_script(js)

如何解决这个问题?提前感谢!

解决方案

嗯,这个问题在GUI编程中实际上非常经典,所以它就是解决方案。直接操作(例如按下按钮)以及间接操作(webkit浏览器完成加载页面)需要始终启动位于不同进程或不同线程上的操作(功能)。

在这种情况下,您可以使用由 webkit 对象<$给出的load-finished c $ c> b 。



一旦网页完成加载,相关的函数就会启动执行JS代码。这是代码的样子:

  def load_finished(webview,frame):
js ='alert( a);'
b.execute_script(js)
$ bb = webkit.WebView()
b.connect(load-finished,load_finished)


I tried this in terminal and everything is OK, but if I run this inside script I can not insert JavaScript after gtk.main()

import gtk
import webkit

w = gtk.Window()
b = webkit.WebView()
w.add(b)
b.open('http://example.com')
w.show_all()

gtk.main() # this I don`t run inside terminal

#and after showing window I want to insert some JavaScript code

js = 'alert("a");'
b.execute_script(js)

How to solve this? Thanks in advance!

解决方案

Well, this problem is actually quite classic in GUI programming and so it is the solution. Direct actions (e.g. press a button) as well as indirect actions (webkit browser finishing loading a page) need always to fire up actions (functions) that sit on different processes or different threads.

In this case you can use the "load-finished" event given by the webkit object b.

Once the web page has completed loading the associated function will fire up executing your JS code. This is what the code looks like:

def load_finished(webview, frame):
    js = 'alert("a");'
    b.execute_script(js)

b = webkit.WebView()
b.connect("load-finished", load_finished)

这篇关于Python GTK + webkit - 在gtk.main()之后插入JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆