gpt4 book ai didi

python - gtk-builder-错误-夸克 : invalid object type 'WebKitWebView'

转载 作者:行者123 更新时间:2023-12-01 00:03:50 28 4
gpt4 key购买 nike

尝试通过 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>

有一个类似的问题here ,但老实说我并没有像答案所说的那样“明白重点”。

最佳答案

Question: gtk-builder-error-quark: invalid object type 'WebKitWebView'

这似乎是 Gtk.Builder 无法解决的范围和命名空间问题。

找到了这个简单的解决方案,将所需的类 WebKit2.WebViewWebKit2.Settings 引入模块命名空间

Note: You don't have to change anything within Glade, place WebKit2 objects as usual.

更改导入来源

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

关于python - gtk-builder-错误-夸克 : invalid object type 'WebKitWebView' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60126579/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com