问题
I am building a python GTK application and I used Glade to define the interface. (using the Glade Interface Designer).
Everything look good on Glade but when I try to create the GUI from my Python program it gives a error loading Webkit related views:
Glade - Invalid object type 'WebKitWebView'
It works fine in Glade Designer and also if I create the widgets manually, but the Builder doesn't like it.
I am building the ui like this:
GtkBuilder = Gtk.Builder.new_from_file("glade/main.glade")
GtkBuilder.connect_signals(self)
My .glade file:
<?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>
Any ideas what I am doing wrong?
Thank you.
回答1:
The widget needs to have been instantiated once before you use it, I guess it's just the case for non-GTK+ widgets.
The code below is Vala, but you get the point:
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);
}
}
}
回答2:
Instead of instantiated it like @mrzapp, you should use following code
typeof(WebKit.WebView).ensure();
Ensures that the indicated type has been registered with the type system, and its _class_init method has been run.
https://valadoc.org/gobject-2.0/GLib.Type.ensure.html
来源:https://stackoverflow.com/questions/51010820/glade-invalid-object-type-webkitwebview-when-loading-from-file-works-on-gla