问题描述
我正在构建python GTK应用程序,并且使用Glade定义了接口. (使用Glade Interface Designer).
I am building a python GTK application and I used Glade to define the interface. (using the Glade Interface Designer).
在Glade上一切看起来都不错,但是当我尝试从Python程序创建GUI时,加载与Webkit相关的视图时会出错:
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 Designer中以及在我手动创建小部件的情况下都可以正常工作,但是Builder不喜欢它.
It works fine in Glade Designer and also if I create the widgets manually, but the Builder doesn't like it.
我正在这样构建ui:
GtkBuilder = Gtk.Builder.new_from_file("glade/main.glade")
GtkBuilder.connect_signals(self)
我的.glade文件:
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?
谢谢.
推荐答案
在使用小部件之前,必须对其进行一次实例化,我想非GTK +小部件就是这种情况.
The widget needs to have been instantiated once before you use it, I guess it's just the case for non-GTK+ widgets.
下面的代码是Vala,但您明白了:
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);
}
}
}
这篇关于Glade-从文件加载时,无效的对象类型'WebKitWebView'.在Glade Application上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!