gtk-builder-error-quark:无效的对象类型 'WebKitWebView'
gtk-builder-error-quark: invalid object type 'WebKitWebView'
我在尝试通过 Gtk.builder()
获取 WebKitWebView
对象时遇到以下错误:
$ python3 test.py
Traceback (most recent call last):
File "test.py", line 11, in <module>
builder.add_from_file("ui.glade")
gi.repository.GLib.Error: gtk-builder-error-quark: ui.glade:31:1 Invalid object type 'WebKitWebView' (6)
这是我的 Python 代码
#!/usr/bin/env python
import gi
gi.require_version('WebKit2', '4.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import WebKit2 as Webkit
builder = Gtk.Builder()
builder.add_from_file("ui.glade")
window = builder.get_object("window")
window.set_title("Test")
webview = builder.get_object("webview")
if __name__ == "__main__":
window.show_all()
Gtk.main()
这是我的 ui.glade
文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<requires lib="webkit2gtk" version="2.12"/>
<object class="GtkWindow" id="window">
<property name="width_request">800</property>
<property name="height_request">600</property>
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="WebKitWebView" id="webview">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
有一个类似的问题,但老实说我没有"get the point"回答说的那样。
问题
存在 gtk_builder_get_type_from_name 不适用于 WebKitWebView 的错误。
The problem is it is looking for the symbol web_kit_web_view_get_type, when it needs to look for webkit_web_view_get_type. This is unfortunate. I'm not sure what to do about this.
Workaround: manually register the type before creating the GtkBuilder. Something like g_object_unref (g_object_ref_sink (webkit_web_view_new ())) should work.
见https://bugs.webkit.org/show_bug.cgi?id=175937
有多种可能的解决方法。
解决方法 1
一种解决方案是在代码中创建 Web 视图
webview = Webkit.WebView()
在ui.glade文件中,将WebKitWebView替换为GtkScrolledWindow,也许像'scrolled_window'这样的id会更合适。然后 expand
属性 应设置为“True”。
阅读 glade 文件后,您可以执行以下操作:
scrolled_window = builder.get_object("scrolled")
scrolled_window.add_with_viewport(webview)
webview.load_uri("https://www.google.com")
解决方法 2
或者,您可以在调用 Gtk.Builder():
之前调用(调用本身就足够了,您不必将其分配给变量)
Webkit.WebView()
这相当于上面提到的
调用 webkit_web_view_new().
链接错误报告中的解决方法
但是,对于 ui.glade 文件中的 WebKitWebView,expand
属性 仍应设置为 True
。
演示
在 Ubuntu 上,这两种情况看起来都像这样:
Question: gtk-builder-error-quark: invalid object type 'WebKitWebView'
这似乎是 scope and namespace
无法解决的 Gtk.Builder
问题。
找到这个简单的解决方案,将所需的 类 WebKit2.WebView
和 WebKit2.Settings
引入模块 namespace
。
Note: You don't have to change anything within Glade
, place WebKit2
objects as usual.
将 import
从
更改为
from gi.repository import WebKit2 as Webkit
到
from gi.repository.WebKit2 import WebView, Settings
如果你不想定义使用过的类一个一个,考虑这个根据要求解决方案。
import gi
gi.require_version('WebKit2', '4.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import WebKit2 as Webkit
class GtkBuilder(Gtk.Builder):
def do_get_type_from_name(self, type_name):
"""
Looks up a type by name, using the virtual function that Gtk.Builder
has for that purpose.
Parameters: type_name (str) – type name to lookup
Returns: the GObject.GType found for type_name
or GObject.TYPE_INVALID if no type was found
Return type: GObject.GType
"""
if type_name.startswith('WebKit'):
getattr(Webkit, type_name[6:])
r = Gtk.Builder.do_get_type_from_name(self, type_name)
print('GtkBuilder: => {}\t{}'.format(type_name, r))
return r
Output:
GtkBuilder: => WebKitSettings <GType WebKitSettings (4168054944)>
GtkBuilder: => GtkWindow <GType GtkWindow (4167639152)>
GtkBuilder: => GtkBox <GType GtkBox (4167624432)>
GtkBuilder: => GtkEntry <GType GtkEntry (4168904384)>
GtkBuilder: => WebKitWebView <GType WebKitWebView (4168054192)>
使用 Python 测试:3.5 - gi.__version__:3.22.0 - Glade 3.22.1
我在尝试通过 Gtk.builder()
获取 WebKitWebView
对象时遇到以下错误:
$ python3 test.py
Traceback (most recent call last):
File "test.py", line 11, in <module>
builder.add_from_file("ui.glade")
gi.repository.GLib.Error: gtk-builder-error-quark: ui.glade:31:1 Invalid object type 'WebKitWebView' (6)
这是我的 Python 代码
#!/usr/bin/env python
import gi
gi.require_version('WebKit2', '4.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import WebKit2 as Webkit
builder = Gtk.Builder()
builder.add_from_file("ui.glade")
window = builder.get_object("window")
window.set_title("Test")
webview = builder.get_object("webview")
if __name__ == "__main__":
window.show_all()
Gtk.main()
这是我的 ui.glade
文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<requires lib="webkit2gtk" version="2.12"/>
<object class="GtkWindow" id="window">
<property name="width_request">800</property>
<property name="height_request">600</property>
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can_focus">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="WebKitWebView" id="webview">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
有一个类似的问题
问题
存在 gtk_builder_get_type_from_name 不适用于 WebKitWebView 的错误。
The problem is it is looking for the symbol web_kit_web_view_get_type, when it needs to look for webkit_web_view_get_type. This is unfortunate. I'm not sure what to do about this.
Workaround: manually register the type before creating the GtkBuilder. Something like g_object_unref (g_object_ref_sink (webkit_web_view_new ())) should work.
见https://bugs.webkit.org/show_bug.cgi?id=175937
有多种可能的解决方法。
解决方法 1
一种解决方案是在代码中创建 Web 视图
webview = Webkit.WebView()
在ui.glade文件中,将WebKitWebView替换为GtkScrolledWindow,也许像'scrolled_window'这样的id会更合适。然后 expand
属性 应设置为“True”。
阅读 glade 文件后,您可以执行以下操作:
scrolled_window = builder.get_object("scrolled")
scrolled_window.add_with_viewport(webview)
webview.load_uri("https://www.google.com")
解决方法 2
或者,您可以在调用 Gtk.Builder():
之前调用(调用本身就足够了,您不必将其分配给变量)Webkit.WebView()
这相当于上面提到的 调用 webkit_web_view_new().
链接错误报告中的解决方法但是,对于 ui.glade 文件中的 WebKitWebView,expand
属性 仍应设置为 True
。
演示
在 Ubuntu 上,这两种情况看起来都像这样:
Question: gtk-builder-error-quark: invalid object type 'WebKitWebView'
这似乎是 scope and namespace
无法解决的 Gtk.Builder
问题。
找到这个简单的解决方案,将所需的 类 WebKit2.WebView
和 WebKit2.Settings
引入模块 namespace
。
Note: You don't have to change anything within
Glade
, placeWebKit2
objects as usual.
将 import
从
from gi.repository import WebKit2 as Webkit
到
from gi.repository.WebKit2 import WebView, Settings
如果你不想定义使用过的类一个一个,考虑这个根据要求解决方案。
import gi
gi.require_version('WebKit2', '4.0')
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository import WebKit2 as Webkit
class GtkBuilder(Gtk.Builder):
def do_get_type_from_name(self, type_name):
"""
Looks up a type by name, using the virtual function that Gtk.Builder
has for that purpose.
Parameters: type_name (str) – type name to lookup
Returns: the GObject.GType found for type_name
or GObject.TYPE_INVALID if no type was found
Return type: GObject.GType
"""
if type_name.startswith('WebKit'):
getattr(Webkit, type_name[6:])
r = Gtk.Builder.do_get_type_from_name(self, type_name)
print('GtkBuilder: => {}\t{}'.format(type_name, r))
return r
Output:
GtkBuilder: => WebKitSettings <GType WebKitSettings (4168054944)> GtkBuilder: => GtkWindow <GType GtkWindow (4167639152)> GtkBuilder: => GtkBox <GType GtkBox (4167624432)> GtkBuilder: => GtkEntry <GType GtkEntry (4168904384)> GtkBuilder: => WebKitWebView <GType WebKitWebView (4168054192)>
使用 Python 测试:3.5 - gi.__version__:3.22.0 - Glade 3.22.1