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

Comments

  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!

    ReplyDelete
  2. Ya I,ve answered the same question earlier, anyway Yes it a wordpress blog and I use Freshy theme :-) (http://theme.wordpress.com/themes/freshy/) Only thing I’ve done to the theme is changing the header. If you need any help regarding changing your blog page from Blogger to WordPress read my article on that : http://gihansblog.wordpress.com/2011/06/24/convert-your-blogger-bolg-into-wordpress-or-wordpress-blog-into-blogger/ :D

    ReplyDelete
  3. My brother recommended I might like this website. He was entirely right. This post actually made my day. You cann't imagine just how much time I had spent for this information! Thanks!

    ReplyDelete
  4. 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!

    ReplyDelete
  5. Hey there, You've done a fantastic job. I’ll definitely digg it and personally recommend to my friends. I am sure they'll be benefited from this web site.

    ReplyDelete
  6. F*ckin' tremendous issues here. I am very glad to see your post. Thank you a lot and i am having a look ahead to touch you. Will you please drop me a e-mail?

    ReplyDelete
  7. 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.

    ReplyDelete
  8. 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.

    ReplyDelete
  9. Thank you for the auspicious writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you! However, how can we communicate?

    ReplyDelete
  10. 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..

    ReplyDelete
  11. 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.

    ReplyDelete
  12. Excuse, I can help nothing. But it is assured, that you will find the correct decision. Do not despair.

    ReplyDelete
  13. Do you mind if I post your article on my Information Blog? I would think this article suits my topic perfectly. Uhmmm, thanks for posting this.

    ReplyDelete
  14. 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

    ReplyDelete
  15. 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.

    ReplyDelete
  16. 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!!

    ReplyDelete
  17. You are my role models. Thanks for the article

    ReplyDelete
  18. I love your wordpress web template, exactly where did you download it through?

    ReplyDelete
  19. Thanks, I've recently been seeking for info about this subject for ages and yours is the best I have located so far.

    ReplyDelete
  20. 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!

    ReplyDelete
  21. I really wish I hadn't noticed this as I genuinely appreciate now!

    ReplyDelete
  22. 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:

    ReplyDelete
  23. As shortly as we rescued this site we went upon reddit to share a small of a adore with them. "Love a small traffic that thou hast learned, as well as be calm therewith." by Marcus Aurelius Antoninus.

    ReplyDelete
  24. Thank you, I've recently been hunting for information about this subject for ages and yours is the best I have found so far.

    ReplyDelete
  25. As soon as I found this internet site I went on reddit to share some of the love with them. "A sect or party is an elegant incognito devised to save a man from the vexation of thinking." by Ralph Waldo Emerson.

    ReplyDelete
  26. I was looking at some of your blog posts on this site and I conceive this site is rattling informative! Keep posting.

    ReplyDelete
  27. I dugg some of you post as I cogitated they were very useful handy

    ReplyDelete
  28. 2011...

    Excellent website. A lot of useful info here. I am sending it to several friends ans also sharing in delicious. And certainly, thanks for your effort!...

    ReplyDelete
  29. 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.

    ReplyDelete
  30. Valuable information. Fortunate me I found your site accidentally, and I am shocked why this twist of fate did not happened earlier! I bookmarked it.

    ReplyDelete
  31. I,need source code

    ReplyDelete
  32. thanks my dear brother i am first semester in IT field so i am very satisfied see your website i read simple c.

    ReplyDelete
  33. Hello !! Im getting an fatal error like gtk/gtk.h & glade.h not found..

    ReplyDelete

Post a Comment

Popular posts from this blog

Joomla 1.7 installation with XAMPP problem solved

Switching to a third party Domain Name System (DNS)?

A shell script for Renaming large number of files in Linux