我正在构建python GTK应用程序,并且使用Glade定义了接口。 (使用Glade Interface Designer)。
在Glade上一切看起来都不错,但是当我尝试从Python程序创建GUI时,加载与Webkit相关的视图会出错:
Glade-无效的对象类型“ WebKitWebView”
它在Glade Designer中以及在我手动创建小部件的情况下都可以正常工作,但是Builder不喜欢它。
我正在这样构建ui:
GtkBuilder = Gtk.Builder.new_from_file("glade/main.glade")
GtkBuilder.connect_signals(self)
我的.glade文件:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.0 -->
<interface>
<requires lib="gtk+" version="3.12"/>
<requires lib="webkit2gtk" version="4.0"/>
<object class="GtkPopover" id="popover1">
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
</object>
<object class="WebKitSettings" id="wv_settings">
<property name="enable_developer_extras">True</property>
<property name="enable_dns_prefetching">True</property>
<property name="enable_webaudio">True</property>
<property name="enable_webgl">True</property>
<property name="user_agent">Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Safari/605.1.15</property>
<property name="enable_smooth_scrolling">True</property>
<property name="enable_accelerated_2d_canvas">True</property>
<property name="enable_media_stream">True</property>
<property name="enable_mediasource">True</property>
<property name="enable_encrypted_media">True</property>
</object>
<object class="GtkApplicationWindow" id="MainWindow">
<property name="can_focus">False</property>
<property name="window_position">center</property>
<property name="default_width">440</property>
<property name="default_height">250</property>
<property name="deletable">False</property>
<child>
<object class="WebKitWebView" id="webview">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="settings">wv_settings</property>
<property name="is_ephemeral">True</property>
<signal name="decide-policy" handler="on_webview_decide_policy" swapped="no"/>
<signal name="load-changed" handler="on_webview_load_changed" swapped="no"/>
<child>
<placeholder/>
</child>
</object>
</child>
<child type="titlebar">
<object class="GtkHeaderBar" id="header">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkButton" id="btn_back">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_back_btn_clicked" swapped="no"/>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-go-back</property>
</object>
</child>
</object>
</child>
<child>
<object class="GtkButton" id="btn_forward">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_btn_forward_clicked" swapped="no"/>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-go-forward</property>
</object>
</child>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn_refresh">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_btn_refresh_clicked" swapped="no"/>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-refresh</property>
</object>
</child>
</object>
<packing>
<property name="position">2</property>
</packing>
</child>
<child type="title">
<object class="GtkEntry">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="width_chars">30</property>
<property name="caps_lock_warning">False</property>
<property name="input_purpose">url</property>
<signal name="activate" handler="on_search_input" swapped="no"/>
</object>
<packing>
<property name="position">3</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn_close">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_btn_close_clicked" swapped="no"/>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-close</property>
</object>
</child>
</object>
<packing>
<property name="pack_type">end</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkButton" id="btn_settings">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<signal name="clicked" handler="on_btn_settings_clicked" swapped="no"/>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-preferences</property>
</object>
</child>
</object>
<packing>
<property name="pack_type">end</property>
<property name="position">5</property>
</packing>
</child>
<child>
<object class="GtkMenuButton" id="menu">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="popover">popover1</property>
<child>
<object class="GtkImage">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="stock">gtk-index</property>
</object>
</child>
</object>
<packing>
<property name="pack_type">end</property>
<property name="position">5</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
有什么想法我做错了吗?
谢谢。
答案 0 :(得分:0)
在使用小部件之前,必须先对其进行一次实例化,我想非GTK +小部件就是这种情况。
下面的代码是Vala,但您明白了:
namespace MyApp {
using WebKit;
[GtkTemplate (ui = "/org/gnome/MyApp/window.ui")]
public class Window : Gtk.ApplicationWindow {
public Window (Gtk.Application app) {
WebView webview = new WebView();
Object (application: app);
}
}
}
答案 1 :(得分:0)
而不是像@mrzapp一样实例化它,您应该使用以下代码
typeof(WebKit.WebView).ensure();
确保指示的类型已在类型系统中注册,并且其_class_init方法已运行。