How to create a GUI(Graphical User Interface) using C programming Language.. (part 4)

Hi,  after a long time I was able to find a time to write the blog, because these days I’m very busy with my exam and it will end 29th  September. So until that I doubt I’d be able to write much. So sorry for that my all friends who read this.

Related articles..

1) How to create a GUI(Graphical User Interface) using C programming Language..

2) How to create a GUI(Graphical User Interface) using C programming Language.. (part 2)

3) How to create a GUI(Graphical User Interface) using C programming Language.. (part 3)

And this article focus on  how to use a Text Entry with button action signals.  And the purpose of this program is display the string in the label,which user enter in the text entry.

Contents…

1) Open a Code Blocks projects

2) Open a Glade Project

3) Set properties for the components

4) Code the C Gtk project

5) Run the project

Components

1) Window= mainWindow

2) Label= displayLabel

3) Text Entry= textEntry

4) Button= displayButton

5) Button= exitButton

Set properties

  • For the main Window

General–> Name= mainWindow
General–>Resizable= No
Common–>Height request = 400
Common–>Width request=200

  • For Display Label

General–>Name=displayLabel

General–>Label=Display

  • For Display Button

General–>Name=displayButton

General–>Label=Display

Signals–>Clicked=on_displayButton_clicked

  • For Exit Button

General–>Name=exitButton

General–>Label=Exit

Signals–>Clicked=on_exitButton_clicked

  • For Text Entry

General–>Name=textEntry

Then save it as TextEntry.glade in libglade format in the CodeBlocks project folder.

In this C project I divided the whole project into three files.

1) main.c

2) callback.c

3) callback.h

The source code of main.c 

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

/*
Author : Gihan De Silva
gihansblog.com

Purpose: This program displays the string in the label,which user enter in the text entry.
*/
GladeXML *xml;
GtkWidget *widget;

int main(int argc, char *argv[])
{

gtk_init(&argc, &argv);

/*import glade file*/
xml = glade_xml_new(“TextEntry.glade”, NULL, NULL);

/* get a widget (useful if you want to change something) */
widget = glade_xml_get_widget(xml, “mainWindow”);

/* connect signal handlers */
glade_xml_signal_autoconnect(xml);

/*show widget*/
gtk_widget_show (widget);

gtk_main();

return 0;
}

The source code of callback.c

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

GladeXML *xml;
GtkWidget *display;
GtkWidget *textValue;

gchar *d_string;

G_MODULE_EXPORT void on_displayButton_clicked(GtkButton *button,gpointer *data)
{

/* Pull the widgets out of the tree */
display= glade_xml_get_widget(xml, “displayLabel”);

textValue= glade_xml_get_widget(xml, “textEntry”);

/* Get the string value form the Entry widgets */
d_string=gtk_entry_get_text(GTK_ENTRY(textValue));

gtk_label_set_text(GTK_LABEL(display),d_string);
}

G_MODULE_EXPORT void on_exitButton_clicked(GtkButton *button,gpointer *data)
{
gtk_main_quit();
}

The source code of callback.h

G_MODULE_EXPORT void on_displayButton_clicked(GtkButton *button,gpointer *data);
G_MODULE_EXPORT void on_exitButton_clicked(GtkButton *button,gpointer *data);

Now run the gtk project and it will look like this.


And when you hit the Display button, it will display the content in the text entry..

Display Button Code Explained…

Here I’m not going to explain all the code because I already have explained them in previous articles. If you haven’t read previous article, you’d better to read them first. But I’m going to explain the specific things related to this article.

GladeXML *xml;
GtkWidget *display;
GtkWidget *textValue;

gchar *d_string;

G_MODULE_EXPORT void on_displayButton_clicked(GtkButton *button,gpointer *data)
{
/* Pull the widgets out of the tree */
display= glade_xml_get_widget(xml, “displayLabel”);

textValue= glade_xml_get_widget(xml, “textEntry”);

In these two lines, the program will take glade widgets into Gtk widgets.

/* Get the string value form the Entry widgets */
d_string=gtk_entry_get_text(GTK_ENTRY(textValue));

We can’t directly use GtkWidgets to set gtk label, because it needs gchar* type data. So in here we convert to into gchar* .

gtk_label_set_text(GTK_LABEL(display),d_string);

Now with above line we can set it to the label.

}

Ok that’s all for today :D . If you want, you can DOWNLOAD my CodeBlock project here!. In next post I will show you how to create a simple Calculator using Gtk and Glade.

Thank you

Gihan De Silva

47 thoughts on “How to create a GUI(Graphical User Interface) using C programming Language.. (part 4)

  1. Hello, I need to ask you one thing. Is this site a wordpress weblog? My organization is planning on changing our weblog from Blogger to wordpress, ya think that is practical? In addition did you set up this specific theme by yourself some how? Bless you for your assistance!

  2. You actually make it seem so easy with your presentation but I find this matter to be actually something that I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I’ll try to get the hang of it!

  3. I have been surfing online more than 3 hours today, yet I never found any interesting article like yours. It is pretty worth enough for me. In my opinion, if all web owners and bloggers made good content as you did, the internet will be a lot more useful than ever before.

  4. Its like you read my mind! You appear to know so much about this, like you wrote the book in it or something. I think that you could do with a few pics to drive the message home a little bit, but instead of that, this is fantastic blog. A fantastic read. I’ll certainly be back.

  5. Pretty section of content. I just stumbled upon your blog and in accession capital to assert that I acquire actually enjoyed account your blog posts. Any way I’ll be subscribing to your augment and even I achievement you access consistently quickly.

  6. I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored material stylish. nonetheless, you command get got an nervousness over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly very often inside case you shield this hike.

  7. hello there and thank you for your information – I’ve certainly picked up something new from right here. I did however expertise several technical issues using this web site, since I experienced to reload the web site lots of times previous to I could get it to load properly. I had been wondering if your hosting is OK? Not that I’m complaining, but sluggish loading instances times will very frequently affect your placement in google and can damage your high-quality score if advertising and marketing with Adwords. Well I’m adding this RSS to my e-mail and could look out for much more of your respective fascinating content. Ensure that you update this again soon..

  8. Wonderful page along with very easy for you to figure out justification. Exactly how can My spouse and i attempt receiving concur for you to submit element in the document inside my future news letter? Getting suitable credit history to you personally your journalist along with backlink on the blog won’t certainly be a trouble.

  9. We’re a group of volunteers and opening a new scheme in our community. Your website offered us with valuable information to work on. You’ve done a formidable job and our entire community will be grateful to you.

  10. Excellent post. I was checking constantly this blog and I’m impressed! Very helpful information particularly the last part :) I care for such information much. I was seeking this particular info for a very long time. Thank you and best of luck.

  11. Wonderful goods from you, man. I’ve understand your stuff previous to and you’re just extremely fantastic. I actually like what you’ve acquired here, certainly like what you’re saying and the way in which you say it. You make it enjoyable and you still take care of to keep it sensible. I can’t wait to read far more from you. This is really a tremendous website.

  12. Definitely believe that which you stated. Your favorite justification appeared to be on the net the easiest thing to be aware of. I say to you, I certainly get annoyed while people consider worries that they just don’t know about. You managed to hit the nail upon the top and also defined out the whole thing without having side effect , people could take a signal. Will likely be back to get more. Thanks

  13. I do consider all the ideas you have introduced in your post. They’re really convincing and can definitely work. Still, the posts are very brief for newbies. May just you please lengthen them a bit from next time? Thank you for the post.

  14. Hi, I think that I saw you visited my website so I came to “return the favor�.I am trying to find things to improve my website!I suppose its ok to use some of your ideas!!

  15. Pretty nice post. I just stumbled upon your blog and wanted to say that I’ve truly enjoyed browsing your blog posts. After all I’ll be subscribing to your feed and I hope you write again very soon!

  16. I loved as much as you’ll receive carried out right here. The sketch is tasteful, your authored subject matter stylish. nonetheless, you command get bought an edginess over that you wish be delivering the following. unwell unquestionably come more formerly again since exactly the same nearly very often inside case you shield this hike.

  17. It’s your pity you actually don’t have got a contribute link! I’d most definitely contribute to that remarkable blog site! I actually presume in the meantime i’ll happy with book-marking plus putting a person’s Rss so that you can this Bing akun. I actually glance forwards so that you can fresh improvements but will publish the following blog site by using this Twitter crew:

  18. I loved as much as you will receive carried out right here. The sketch is attractive, your authored subject matter stylish. nonetheless, you command get bought an nervousness over that you wish be delivering the following. unwell unquestionably come further formerly again as exactly the same nearly very often inside case you shield this increase.

  19. It’s a pity you don’t have a donate button! I’d certainly donate to this outstanding blog! I suppose for now i’ll settle for book-marking and adding your RSS feed to my Google account. I look forward to brand new updates and will talk about this site with my Facebook group. Talk soon!

  20. Pingback: Ycvddf
  21. Awesome blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple adjustements would really make my blog shine. Please let me know where you got your design. Thank you

  22. Excellent piece of writing and easy to fully understand story. How do I go about getting agreement to post component of the page in my upcoming newsletter? Offering proper credit to you the source and weblink to the site will not be a problem.

Leave a Reply