Click here to Skip to main content
15,867,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i doing sum of two number when show the final answer printf in terminal is ok. but i want to show the final answer in (gtk's label) i having a lot of problem. i had tried to refer the website or other people forum. most of them are too complicated and still error when compile it. does someone can help or giving the solution or correction of these code that i show? please don't add button function in gtk. i had want to try transfer printf sensor value to gtk after all.

how to get just "total = 23" display in gtk?
if someone don't understand can try running 2 code above.

..................................................................................................................................................................

// sample addition of 2 number

#include <stdio.h>

int main()
{
  int a, b, c;

  a=3;
  b=20;
  c = a + b;
   
  printf("Sum of numbers = %d\n", c);

return 0;

}


.................................................................................
.................................................................................

// sample gtk
#include <gtk/gtk.h>

void main(int argc, char *argv[])
{
   gtk_init (&argc, &argv);
   GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);

   gtk_window_set_default_size(GTK_WINDOW(win), 250, 150);

   GtkWidget *lbl = gtk_label_new ("100 C");
   gtk_container_add (GTK_CONTAINER (win), lbl);

   gtk_widget_show_all (win);

   gtk_main();

}




..................................................................................................................................................................

problem when trying replaced gtk label with printF value

#include <gtk/gtk.h>
#include <stdio.h>
#include <string.h>


int main(int argc, char *argv[])
{
  
   int a, b, c;

   a = 3;
   b = 20;
   c = a + b;
   
   gchar* Sumvalue;
   Sumvalue = g_strdup_printf ("total = %d", c);
   
   


   gtk_init (&argc, &argv);
   GtkWidget *win = gtk_window_new (GTK_WINDOW_TOPLEVEL);

   gtk_window_set_default_size(GTK_WINDOW(win), 250, 150);
 


   GtkWidget *lbl = gtk_label_set_text(GTK_LABEL(*Sumvalue));



   gtk_container_add (GTK_CONTAINER (win), lbl);

   gtk_widget_show_all (win);

   gtk_main();
   

}


What I have tried:

the above code is my last try or modify, still can't get result. having problem when compile it.


error massage, error, GTK_LABEL undrclared
Posted
Updated 12-Mar-20 4:16am
v3
Comments
OriginalGriff 12-Mar-20 2:59am    
"Compile failed" doesn't tell us much - and unless we have a Pi handy plus all the libraries it uses we can't even begin to guess what the compilation error actually is.

So edit your question, show us what the compiler error message is, and show us which line it is referring to.

Use the "Improve question" widget to edit your question and provide better information.

You need to create a label with gtk_label_new() before you can change its contents with gtk_label_set_text()

Here's a minimal sample program that puts the current time in a label:
C++
#include <time.h>
#include <gtk/gtk.h>

static const size_t BUFSIZE=30;

void end_program(GtkWidget *wid, gpointer ptr) {
	gtk_main_quit();
}


gboolean timer_func(gpointer ptr) {
	char buffer[BUFSIZE];	
	time_t now = time(NULL);
	strftime(buffer, BUFSIZE, "%H:%M:%S", localtime(&now));
	gtk_label_set_text(GTK_LABEL(ptr), buffer);
	return TRUE;
}

int main(int argc, char **argv)
{
	gtk_init(&argc, &argv);
	GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
	g_signal_connect(win, "delete_event", G_CALLBACK(end_program), NULL);
	GtkWidget *lbl = gtk_label_new("My label");
	gtk_container_add(GTK_CONTAINER(win), lbl);
	g_timeout_add_seconds(1, timer_func, lbl);
	gtk_widget_show_all(win);
	gtk_main();
	
	return 0;
}


Compile with: gcc `pkg-config --cflags --libs gtk+-2.0` -O2 -Wall -o clock_sample clock_sample.c. This works on a Raspberry Pi3+ running Raspbian Buster.

You should download the free eBook "An Introduction to C & GUI Programming[^] and go through the exercises in order from the beginning.
 
Share this answer
 
v2
The message is quite clear. You are trying to use a macro (I assume) that does not exist. Check the documentation at GtkLabel: GTK+ 3 Reference Manual[^] for the correct syntax.
 
Share this answer
 
I do not known GTK, but C provides the 'fprintf' function. If I remember correctly it will copy the sting data into a string buffer that you supply, instead of copying it to the console. Once you have the string, then just pass it to whoever needs to print it on the screen.

Do your research. There is away to determine the amount of memory required for the buffer for 'fprintf', but I am not telling.
 
Share this answer
 
Comments
Stefan_Lang 12-Mar-20 10:54am    
You probably mean sprintf. fprintf prints to a file, not a buffer.
John R. Shaw 13-Mar-20 10:42am    
You are correct :)
But if I did not introduce such mistakes, how would they learn?
Stefan_Lang 13-Mar-20 10:45am    
I'm pretty sure they're capable of such mistakes all by themselves :o)
John R. Shaw 13-Mar-20 13:17pm    
LMAO! Thanks that's the best laugh I had in weeks.

I am still trying to stop and failing!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900