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 Comments

  1. Reply
    sanibel island resorts September 17, 2011

    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. Reply
    mide kanseri belirtileri September 19, 2011

    I was suggested this web site by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my trouble. You are wonderful! Thanks!

  3. Reply

    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!

  4. Reply
    zona hastalığı September 20, 2011

    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!

  5. Reply
    İndirimli Tatil September 20, 2011

    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.

  6. Reply
    Turkey Property September 20, 2011

    I was recommended this web site by my cousin. I’m not sure whether this post is written by him as nobody else know such detailed about my problem. You are incredible! Thanks!

  7. Reply
    bayan escort izmir September 20, 2011

    Heya i am for the first time here. I found this board and I find It truly useful & it helped me out a lot. I hope to give something back and help others like you aided me.

  8. Reply
    hd film izle September 20, 2011

    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.

  9. Reply
    izmir hatay escort September 20, 2011

    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.

  10. Reply
    6pm coupon code September 22, 2011

    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?

  11. Reply
    magna rx September 22, 2011

    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.

  12. Reply
    Film indir September 22, 2011

    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.

  13. Reply
    kuzey güney izle September 23, 2011

    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?

  14. Reply
    mynet September 23, 2011

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

  15. Reply
    Robyn Pullara September 23, 2011

    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.

  16. Reply
    ikinci el oto September 24, 2011

    Excellent blog here! Also your web site loads up fast! What web host are you using? Can I get your affiliate link to your host? I wish my web site loaded up as quickly as yours lol

  17. Reply
    Nicolas Hockenbury September 24, 2011

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

  18. Reply
    Mixed Martial Arts DVD September 24, 2011

    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.

  19. Reply
    tek link September 24, 2011

    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.

  20. Reply
    Online Film İzle September 24, 2011

    This is very interesting, You are a very skilled blogger. I have joined your feed and look forward to seeking more of your wonderful post. Also, I’ve shared your site in my social networks!

  21. Reply
    gelinlik September 24, 2011

    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.

  22. Reply
    gratis annonser September 24, 2011

    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.

  23. Reply
    combofix September 25, 2011

    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

  24. Reply
    Rich Garett September 25, 2011

    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.

  25. Reply
    lee min ho September 26, 2011

    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!!

  26. Reply
    cheap adobe software September 26, 2011

    You are my role models. Thanks for the article

  27. Reply
    Beat Generator September 27, 2011

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

  28. Reply
    porno izle September 27, 2011

    Wow, incredible blog layout! How long have you been blogging for? you made blogging look easy. The overall look of your web site is fantastic, as well as the content!

  29. Reply
    systemic enzymes September 27, 2011

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

  30. Reply
    Ankara escort September 27, 2011

    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!

  31. Reply
    çizgi film September 28, 2011

    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.

  32. Reply
    seoer September 29, 2011

    I really wish I hadn’t noticed this as I genuinely appreciate now!

  33. Reply
    Sommer Persia September 29, 2011

    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:

  34. Reply
    Real Estate Lewes Delaware September 29, 2011

    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.

  35. Reply
    search engine optimisation September 29, 2011

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

  36. Reply
    Real Estate Lewes Delaware September 29, 2011

    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.

  37. Reply
    video September 30, 2011

    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.

  38. Reply
    uggs bailey button bomber September 30, 2011

    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!

  39. Reply
    100percentwinners October 1, 2011

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

  40. Reply
    Real Estate Lewes Delaware October 3, 2011

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

  41. Reply
    Ycvddf October 3, 2011

    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!…

  42. Reply
    Tattoo Font Generator October 3, 2011

    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

  43. Reply
    Hwa Gameros October 7, 2011

    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.

  44. Reply
    Marin Bagnall October 8, 2011

    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.

  45. Reply
    Ranjith June 4, 2012

    I,need source code

  46. Reply
    ibtihaj December 14, 2012

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

Leave a Reply