Skip to main content

[GTK] Example of using GtkBox with label and button


[1]Source code



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <gtk/gtk.h>

void on_window_closed (GtkWidget *window, gpointer data)
{
    gtk_main_quit();
}

static void destroy(GtkWidget *widget, gpointer data)
{
    gtk_main_quit();
}

static void button_clicked(GtkWidget *button, gpointer data)
{
    g_print("Button clicked\n");
}

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

    GtkWidget *window, *label, *box, *button ;

    gtk_init(&argc,&argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW (window), "Test GTK");
    gtk_window_set_default_size (GTK_WINDOW (window), 500, 200);

#if 1 //Use gtk3: change 0->1, Use gtk2: keep it is 0

    //Using gtk3
    box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);
#else

    //Using gtk2
    box = gtk_vbox_new(GTK_ORIENTATION_VERTICAL, 5);
#endif

    label = gtk_label_new("Label name");
    button = gtk_button_new_with_label ("CLOSE");
    g_signal_connect(GTK_BUTTON(button), "clicked", G_CALLBACK(button_clicked), NULL);
    g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
    g_signal_connect(window, "destroy", G_CALLBACK(destroy), NULL);

    gtk_box_pack_start(GTK_BOX (box), label, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX (box), button, FALSE, FALSE, 0);
    gtk_container_add(GTK_CONTAINER (window), box);

    gtk_widget_show_all(window);

    gtk_main();

    return 0;
}

[2]How to build

Open terminal and type below command
For gtk2
g++ test1.cpp -o hello-1 `pkg-config --cflags --libs gtk+-2.0`

For gtk3
g++ test1.cpp -o hello-1 `pkg-config --cflags --libs gtk+-3.0`

[3]Error may happened !

Problem
test1.cpp: In function ‘int main(int, char**)’:
test1.cpp:32:50: error: ‘gtk_box_new’ was not declared in this scope

     box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);


1
2
3
4
g++ test1.cpp -o hello-1 `pkg-config --cflags --libs gtk+-2.0`
test1.cpp: In function ‘int main(int, char**)’:
test1.cpp:32:50: error: ‘gtk_box_new’ was not declared in this scope
     box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5);

Root cause:
source code is mismatched with gtk version.
Remember, gtk2 is not support API gtk_box_new()

Enjoy.

Keyword: gtk2, gtk3, GtkBox, GtkLabel, GtkButton,
Reference: http://xlpiao.blogspot.com/2016/02/gtk3-example.html

Comments

Popular posts from this blog

[STM32] How to configure Timer 1, Channel 3 is PWM 1kHz, duty cycle 20% to control BLDC motor

 To configure Timer1 for a 1 kHz PWM signal with a 20% duty cycle on an STM32H7S3L8 microcontroller, follow these steps: 1. Understand the Timer Parameters PWM Frequency : 1 kHz → Period = 1 ms. Duty Cycle : 20% → ON time = 0.2 ms. The timer’s clock frequency is derived from the APB clock (e.g., TIMCLK). Let’s assume you know the APB clock frequency. The Timer prescaler and auto-reload register (ARR) define the PWM frequency. 2. Compute Timer Parameters Formula: PWM Frequency = Timer Clock (Prescaler + 1) * (ARR + 1) \text{PWM Frequency} = \frac{\text{Timer Clock}}{\text{(Prescaler + 1) * (ARR + 1)}} PWM Frequency = (Prescaler + 1) * (ARR + 1) Timer Clock ​ For 1 kHz PWM: A R R = Timer Clock PWM Frequency ∗ ( Prescaler + 1) − 1 ARR = \frac{\text{Timer Clock}}{\text{PWM Frequency} * (\text{Prescaler + 1)}} - 1 A RR = PWM Frequency ∗ ( Prescaler + 1) Timer Clock ​ −...

How to use ChatGPT to get your resume shortlisted?

How to use ChatGPT to get your resume shortlisted? Core steps: Chat GPT -> Resume Creator -> LinkedIn(Copy the job description to ChatGPT) -> Add your personal information -> Copy output from ChatGPT to Instaresume.io to make the template -> Goto SkillSyncer to check ATS(Applicant Tracking Software) score, point out the missing keywords. Detail ☑️In my pursuit of job #opportunities, I encountered a familiar challenge - my resume seemingly disappeared into oblivion, yielding no responses despite my diverse skill set and numerous applications. ☑️As I delved into my research, I uncovered the existence of ATS software, the automated gatekeeper of #resumes, which swiftly filtered out those lacking relevant keywords. ☑️The outcome? Not just one #company, but over a dozen organizations recognized the potential in my resume, resulting in multiple shortlists and promising #job prospects! 💻If you want to supercharge your resume and unlock countless opportunities, don't miss o...

[Mentorship] First day at work - question for your co-workers (casual and friendly tone)

Summary Learn essential tips to make a positive first impression on your first day at a new job, ensuring success and connection with colleagues. Highlights⏰ Be punctual: Arrive early to show commitment. ( 15 minutes before your shift ) 🚫 Avoid gossip: Maintain professionalism and integrity. ❌ Don’t ask for time off: Show dedication to your new role. 🤝 Firm handshake: A confident greeting sets a great tone. 📚 Ask questions: Engage with your new role actively. 🗂️ Organize your workspace: Keep your area tidy for respect. ☕ Accept social invites: Build rapport with co-workers. Key Insights ⏳ First Impressions Matter: Your colleagues will form opinions quickly, so be on your best behavior right from the start. 🌟 Professional Appearance: Dressing smartly conveys seriousness about your role and can positively influence how others perceive you. 💬 Communication is Key: A warm greeting and engaging dialogue can help break the ice and make you more approachable. 📝 Active Learning: Asking ...