最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c - Gtk box, add command in Menu button - Stack Overflow

programmeradmin2浏览0评论

This example is from this video:

How can I add this exec command to button 1 (Menu)?

Exec xdotool key ctrl+m

#include <gtk/gtk.h>
int main(int argc, char **argv) {
gtk_init(&argc, &argv);

GtkWidget *window;
GtkWidget *box;
GtkWidget *button1;

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);

button1 = gtk_button_new_with_label ("Menu");
gtk_container_add(GTK_CONTAINER(window), box);
gtk_box_pack_start(GTK_BOX(box), button1, TRUE, TRUE, 0);

gtk_widget_show_all(window);
gtk_main();
return 0;
}

This example is from this video: https://www.youtube/watch?v=mKC0Z0SoHso

How can I add this exec command to button 1 (Menu)?

Exec xdotool key ctrl+m

#include <gtk/gtk.h>
int main(int argc, char **argv) {
gtk_init(&argc, &argv);

GtkWidget *window;
GtkWidget *box;
GtkWidget *button1;

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);

button1 = gtk_button_new_with_label ("Menu");
gtk_container_add(GTK_CONTAINER(window), box);
gtk_box_pack_start(GTK_BOX(box), button1, TRUE, TRUE, 0);

gtk_widget_show_all(window);
gtk_main();
return 0;
}
Share Improve this question edited Mar 18 at 22:52 President James K. Polk 42.1k29 gold badges109 silver badges145 bronze badges asked Mar 17 at 15:24 RasatPCRasatPC 476 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 2

In GTK , all user interactions and system events are treated as "signals". These signals are managed by a central event loop (Main Loop) and forwarded to corresponding callback functions.

So if a Gtk button is pressed, a signal "clicked" is triggered, which can then be collected and further processed in the program.

The script below shows how this can be achieved, which I have taken from the following website.

https://docs.gtk./gtk3/getting_started.html

#include <gtk/gtk.h>

static void
print_hello (GtkWidget *widget,
             gpointer   data)
{
  g_print ("Hello World\n");
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *button_box;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

  button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_container_add (GTK_CONTAINER (window), button_box);

  button = gtk_button_new_with_label ("Hello World");
  g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);
  g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window);
  gtk_container_add (GTK_CONTAINER (button_box), button);

  gtk_widget_show_all (window);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new (".gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

In this context, I would also like to recommend that you start your entry into GTK with this website.Even better perhaps with https://www.gtk./ . This is the current version of GTK and also includes good tutorials. The following tutorial might be recommended: https://github/ToshioCP/Gtk4-tutorial/tree/main as a second step.

Enjoy programming and trying out GTK.

You are probably trying to run a system command by clicking a GtkButton. This can be done like this --

#include <gtk/gtk.h>

int run (GtkWidget *widget, gpointer   data)
{
  /*run **** command */
  system ("ls"); //or try popen
}

int main(int argc, char **argv) {
gtk_init(&argc, &argv);

GtkWidget *window;
GtkWidget *box;
GtkWidget *button1;

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);

button1 = gtk_button_new_with_label ("Menu");
gtk_container_add(GTK_CONTAINER(window), box);
gtk_box_pack_start(GTK_BOX(box), button1, TRUE, TRUE, 0);

g_signal_connect (button1, "clicked", G_CALLBACK (run), NULL);


gtk_widget_show_all(window);
gtk_main();
return 0;
}
发布评论

评论列表(0)

  1. 暂无评论