GTK 中带有标签的文本框+程序

发布于 2024-09-25 18:39:40 字数 1575 浏览 5 评论 0原文

我是 GTK+ 编程的新手。我编写了一个简单的 GTK+ 程序,其中在窗口中显示标签和文本框,标签应该位于文本框的左侧,并且我应该能够指定文本框的水平长度。下面是到目前为止我的代码,程序运行良好,但我无法将标签与文本框的左侧对齐,也无法设置文本框的水平长度。

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

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

int main (int argc, char *argv[])
{
  GtkWidget *window,*table,*label,*entry;
  gtk_init(&argc, &argv);
  void initialize_window(GtkWidget *);

  //Create the main window
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  initialize_window(window);
  gtk_widget_show(window);

   /* Create a 1x2 table */
   table = gtk_table_new (1, 2, TRUE);
   gtk_container_add (GTK_CONTAINER (window), table);
   gtk_widget_show (table);


  /* create a new label. */
  label = gtk_label_new ("Enter some text: ");
  gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
  gtk_table_attach_defaults (GTK_TABLE (table),label, 0, 1, 0, 1);
  gtk_widget_show (label);

  //create a text box
  entry = gtk_entry_new ();
  gtk_entry_set_max_length (GTK_ENTRY (entry),0);
  gtk_table_attach_defaults (GTK_TABLE (table),entry, 0, 1, 0, 1);
  gtk_widget_show (entry); 



  gtk_main ();
  return 0;
}



void initialize_window(GtkWidget *window)
{
  gtk_window_set_title(GTK_WINDOW(window),"My Window"); //Set window title
  gtk_window_set_default_size (GTK_WINDOW (window), 400, 200); //Set default size for the window
  g_signal_connect (window, "destroy",G_CALLBACK (destroy), NULL); //End application when close button clicked

}

我该如何解决这个问题?

请帮忙 谢谢。

I am new to GTK+ programming.I wrote a simple GTK+ program where i display a label and a textbox in a window, the label should be to the left of the textbox and i should be able to specify the horizontal length of the textbox. Below is my code so far,the program runs fine but im unable to align the label to the left of the textbox and also set the textbox horizontal length.

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

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

int main (int argc, char *argv[])
{
  GtkWidget *window,*table,*label,*entry;
  gtk_init(&argc, &argv);
  void initialize_window(GtkWidget *);

  //Create the main window
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  initialize_window(window);
  gtk_widget_show(window);

   /* Create a 1x2 table */
   table = gtk_table_new (1, 2, TRUE);
   gtk_container_add (GTK_CONTAINER (window), table);
   gtk_widget_show (table);


  /* create a new label. */
  label = gtk_label_new ("Enter some text: ");
  gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
  gtk_table_attach_defaults (GTK_TABLE (table),label, 0, 1, 0, 1);
  gtk_widget_show (label);

  //create a text box
  entry = gtk_entry_new ();
  gtk_entry_set_max_length (GTK_ENTRY (entry),0);
  gtk_table_attach_defaults (GTK_TABLE (table),entry, 0, 1, 0, 1);
  gtk_widget_show (entry); 



  gtk_main ();
  return 0;
}



void initialize_window(GtkWidget *window)
{
  gtk_window_set_title(GTK_WINDOW(window),"My Window"); //Set window title
  gtk_window_set_default_size (GTK_WINDOW (window), 400, 200); //Set default size for the window
  g_signal_connect (window, "destroy",G_CALLBACK (destroy), NULL); //End application when close button clicked

}

How can i fix this problem ?

Please help
Thank You.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向地狱狂奔 2024-10-02 18:39:40

你搞乱了桌子的位置,如果你不知道它的作用,就不要设置 Aligin,这可能会有点误导。
这是工作代码(我认为这就是你想要的):

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

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



static void initialize_window(GtkWidget* window)
{
  gtk_window_set_title(GTK_WINDOW(window),"My Window"); //Set window title
  gtk_window_set_default_size (GTK_WINDOW (window), 400, 200); //Set default size for the window
  g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL); //End application when close button clicked

}

int main (int argc, char *argv[])
{
  GtkWidget *window,*table,*label,*entry;
  gtk_init(&argc, &argv);


  //Create the main window
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  initialize_window(window);


   /* Create a 1x2 table */
   table = gtk_table_new (1, 2, TRUE);
   gtk_container_add (GTK_CONTAINER (window), table);


  /* create a new label. */
  label = gtk_label_new ("Enter some text:" );
  //gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
  gtk_table_set_homogeneous(GTK_TABLE (table), TRUE);
  gtk_table_attach_defaults (GTK_TABLE (table), label, 1, 2, 0, 1);


  //create a text box
  entry = gtk_entry_new ();
  gtk_entry_set_max_length (GTK_ENTRY (entry),0);
  gtk_table_attach_defaults (GTK_TABLE (table), entry, 0, 1, 0, 1);


    gtk_widget_show_all(window);

  gtk_main ();
  return 0;
}

You messed with the table position and don't set aligin if you do not know what it does, it may be a bit missleading.
Here is working code (I think this is what you wanted):

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

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



static void initialize_window(GtkWidget* window)
{
  gtk_window_set_title(GTK_WINDOW(window),"My Window"); //Set window title
  gtk_window_set_default_size (GTK_WINDOW (window), 400, 200); //Set default size for the window
  g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL); //End application when close button clicked

}

int main (int argc, char *argv[])
{
  GtkWidget *window,*table,*label,*entry;
  gtk_init(&argc, &argv);


  //Create the main window
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  initialize_window(window);


   /* Create a 1x2 table */
   table = gtk_table_new (1, 2, TRUE);
   gtk_container_add (GTK_CONTAINER (window), table);


  /* create a new label. */
  label = gtk_label_new ("Enter some text:" );
  //gtk_misc_set_alignment (GTK_MISC (label), 0, 0);
  gtk_table_set_homogeneous(GTK_TABLE (table), TRUE);
  gtk_table_attach_defaults (GTK_TABLE (table), label, 1, 2, 0, 1);


  //create a text box
  entry = gtk_entry_new ();
  gtk_entry_set_max_length (GTK_ENTRY (entry),0);
  gtk_table_attach_defaults (GTK_TABLE (table), entry, 0, 1, 0, 1);


    gtk_widget_show_all(window);

  gtk_main ();
  return 0;
}
随梦而飞# 2024-10-02 18:39:40

对于对齐,可以使用 hbox 进行水平排列,或使用 vbox 进行垂直排列。

For alignment one can use hbox for horizontal arrangement or vbox for vertical arrangement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文