我有一个Raspberry Pi3板,我有一些传感器。我想制作一个GUI,最短的是gtk +和C ++。例如,我得到了连续的心电图数据。那个给我数据的函数是无限循环的。我想实时更新GTK标签。有人可以解决这个问题吗? 这段代码:
#include <gtk/gtk.h>
#include "eHealth.h"
#include <stdio.h>
#include <stdlib.h>
//g++ -lpthread -lrt a.cpp arduPi.o eHealth.o -o a `pkg-config gtkmm-2.4 --cflags --libs`
char c[256];
float ECG;
int i=0;
float looop(){
//here is the infinite loop
while(1){
ECG=eHealth.getECG();
}
return ECG;
}
static void button_clicked10(GtkWidget *widget, gpointer data){
gtk_label_set_text(GTK_LABEL(data), "u clicked10");
}
static void button_clicked11(GtkWidget *widget, gpointer data){
gtk_label_set_text(GTK_LABEL(data), "u clicked11");
}
int main(int argc, char* argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window, *label, *button, *table, *label10;
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "delete-event", G_CALLBACK(gtk_main_quit), NULL);
table = gtk_table_new(8, 3, 10); //rows, columns, honogenous
button = gtk_button_new_with_mnemonic("_Button");
ECG=eHealth.getECG();
sprintf(c,"%f", looop());
//in this label I want to print the ECG data
label = gtk_label_new(c); ///(!!!)
gtk_table_attach(GTK_TABLE(table), label, 0,1,0,1, GTK_FILL, GTK_FILL, 0,0);
gtk_table_attach(GTK_TABLE(table), button, 1,2,0,1, GTK_FILL, GTK_FILL, 0,0);
button = gtk_button_new_with_mnemonic("_Button 2");
label = gtk_label_new("Hello World 2");
gtk_table_attach(GTK_TABLE(table), label, 0,1,1,2, GTK_FILL, GTK_FILL, 0,0);
gtk_table_attach(GTK_TABLE(table), button, 1,2,1,2, GTK_FILL, GTK_FILL, 0,0);
button = gtk_button_new_with_mnemonic("_Button 3");
label = gtk_label_new("Hello World 3");
gtk_table_attach(GTK_TABLE(table), label, 0,1,2,3, GTK_FILL, GTK_FILL, 0,0);
gtk_table_attach(GTK_TABLE(table), button, 1,2,2,3, GTK_FILL, GTK_FILL, 0,0);
button = gtk_button_new_with_mnemonic("_Button 4");
label = gtk_label_new("Hello World 4");
gtk_table_attach(GTK_TABLE(table), label, 0,1,3,4, GTK_FILL, GTK_FILL, 0,0);
gtk_table_attach(GTK_TABLE(table), button, 1,2,3,4, GTK_FILL, GTK_FILL, 0,0);
button = gtk_button_new_with_mnemonic("_Button 5");
label = gtk_label_new("Hello World 5");
gtk_table_attach(GTK_TABLE(table), label, 2,3,0,1, GTK_FILL, GTK_FILL, 0,0);
gtk_table_attach(GTK_TABLE(table), button, 3,4,0,1, GTK_FILL, GTK_FILL, 0,0);
button = gtk_button_new_with_mnemonic("_Button 6");
label = gtk_label_new("Hello World 6");
gtk_table_attach(GTK_TABLE(table), label, 2,3,1,2, GTK_FILL, GTK_FILL, 0,0);
gtk_table_attach(GTK_TABLE(table), button, 3,4,1,2, GTK_FILL, GTK_FILL, 0,0);
button = gtk_button_new_with_mnemonic("_Button 7");
label10 = gtk_label_new("Hello World 7");
gtk_table_attach(GTK_TABLE(table), label10, 2,3,2,3, GTK_FILL, GTK_FILL, 0,0);
gtk_table_attach(GTK_TABLE(table), button, 3,4,2,3, GTK_FILL, GTK_FILL, 0,0);
g_signal_connect(button, "clicked", G_CALLBACK(button_clicked10), (gpointer)label10);
button = gtk_button_new_with_mnemonic("_Button 8");
label = gtk_label_new("Hello World 8");
gtk_table_attach(GTK_TABLE(table), label, 2,3,3,4, GTK_FILL, GTK_FILL, 0,0);
gtk_table_attach(GTK_TABLE(table), button, 3,4,3,4, GTK_FILL, GTK_FILL, 0,0);
button = gtk_button_new_with_mnemonic("_Button 9");
gtk_table_attach(GTK_TABLE(table), button, 0,4,4,5, GTK_FILL, GTK_FILL, 0,0);
button = gtk_button_new_with_mnemonic("_Button 10");
gtk_table_attach(GTK_TABLE(table), button, 0,4,5,6, GTK_FILL, GTK_FILL, 0,0);
button = gtk_button_new_with_mnemonic("_Button 11");
gtk_table_attach(GTK_TABLE(table), button, 0,4,6,7, GTK_FILL, GTK_FILL, 0,0);
g_signal_connect(button, "clicked", G_CALLBACK(button_clicked11), (gpointer)label);
button = gtk_button_new_with_mnemonic("_Button 10");
gtk_table_attach(GTK_TABLE(table), button, 0,4,7,8, GTK_FILL, GTK_FILL, 0,0);
g_signal_connect(button, "clicked", G_CALLBACK(button_clicked10), (gpointer)label);
gtk_container_add(GTK_CONTAINER(window), table);
gtk_widget_set_size_request(window, 500, 500);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
答案 0 :(得分:2)
GTK +及其底层库(如GLib)需要运行自己的主事件循环。任何改变GTK + / GLib内容的代码都需要返回,以便GTK + / GLib可以处理您要求他们做的事情。如果您使用自己的while
或其他循环阻止它们,则无法执行任何操作。
我不会因为它的基本内容而进一步纠缠于此;例如,见:
您可以使用GLib超时源解决您的问题,以按照所需的时间表运行更新标签的功能。该页面上有g_timeout_add()
,g_timeout_add_seconds()
和其他文档。
这是一个玩具示例:
#include <gtk/gtk.h>
static gboolean
on_timeout (gpointer user_data)
{
static unsigned f_times = 0;
GtkLabel *label = GTK_LABEL (user_data);
++f_times;
gchar *text = g_strdup_printf ("I have been updated %u times", f_times);
gtk_label_set_label (label, text);
g_free (text);
return G_SOURCE_CONTINUE; /* or G_SOURCE_REMOVE when you want to stop */
}
int
main (int argc,
char **argv)
{
gtk_init (&argc, &argv);
GtkWidget *label = gtk_label_new ("not updated yet...");
g_timeout_add (1000 /* milliseconds */, on_timeout, label);
GtkWidget *window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_container_add (GTK_CONTAINER (window), label);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
其他一些观点:
GtkTable
在GTK + 3中已弃用,在GTK + 4中已弃用;你应该使用GtkGrid
代替。请参阅Migrating from other containers to GtkGrid gtkmm
,即GTK +的官方C ++绑定,但您已编写所有代码以直接调用C库。这似乎有点奇怪。 gtkmm
很棒,所以我可以推荐它,但是如果你不想要它,那么你应该通过使用C和链接GTK +来陈述你的意图。但显然,我的代码假设你真的想要C。