我正在编写一个多线程的 Lxpanel 插件(使用 C 和 GTK2)并且我正在尝试在单独的线程中更改 GUI(例如,添加和删除小部件),但它不起作用 - 当我试图添加一个标签有一个空白空间。
//includes
typedef struct {
GtkWidget *l1;
GtkWidget *l2;
} Plug;
/*thread function*/
gboolean test_func(Plugin *plugin){
Plug *priv=plugin->priv;
gtk_container_remove(GTK_CONTAINER(plugin->pwid), priv->l1);
gtk_container_add(GTK_CONTAINER(plugin->pwid), priv->l2);
return FALSE;
}
/*called when plugin is added*/
static int test_constructor(Plugin *plugin, char **fp){
Plug *priv=g_new0(Plug, 1);
plugin->pwid=gtk_event_box_new();
plugin->priv=priv;
priv->l1=gtk_label_new("l1");
priv->l2=gtk_label_new("l2");
g_thread_new(
"test_thread", (GThreadFunc)test_func, plugin
);
//g_idle_add((GSourceFunc)test_func, plugin);
//test_func(plugin);
gtk_container_add(GTK_CONTAINER(plugin->pwid), priv->l1);
gtk_widget_set_size_request(plugin->pwid, 30, 25);
gtk_widget_set_has_window(plugin->pwid, FALSE);
gtk_widget_show_all(plugin->pwid);
return 1;
}
/*called when plugin is removed*/
static void test_destructor(Plugin *plugin){
Plug *priv=plugin->priv;
gtk_widget_destroy(priv->l1);
gtk_widget_destroy(priv->l2);
g_free(priv);
}
/*settings*/
PluginClass test_plugin_class = {
PLUGINCLASS_VERSIONING,
type : "test",
name : N_("test"),
version: "1.0",
description : N_("test"),
one_per_system : TRUE,
expand_available : FALSE,
constructor : test_constructor,
destructor : test_destructor,
config : NULL,
save : NULL
};
我也试过 g_idle_add - 它也不起作用。当我在函数中添加 gdk_threads_enter() 和 gdk_threads__leave() 时,test_func
lxpanel 就会挂起。我究竟做错了什么?
非常感谢您的帮助。