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

typedef struct {
    const gchar *host;
} example;

void b_clicked (GtkButton *c_button, example *test){
    g_print("Hostname: %s\n", test->host);
}

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

GtkWidget *window;
GtkWidget *grid;
GtkWidget *c_button;
GtkWidget *q_button;
GtkWidget *label_host;
GtkWidget *h_name;

gtk_init (&argc, &argv);

window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "FTP Client");
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_window_set_resizable(GTK_WINDOW(window), FALSE);

grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), grid);
gtk_grid_set_row_spacing (GTK_GRID (grid), 3);

label_host = gtk_label_new("Hostname");

example test;
h_name = gtk_entry_new();
test.host = gtk_entry_get_text(GTK_ENTRY (h_name));
gtk_entry_set_placeholder_text (GTK_ENTRY (h_name), "Hostname");
c_button = gtk_button_new_with_label ("Connect");
g_signal_connect (c_button, "clicked", G_CALLBACK (b_clicked), (gpointer*)&test);
q_button = gtk_button_new_with_label ("Quit");
g_signal_connect (q_button, "clicked", G_CALLBACK (gtk_main_quit), NULL);

gtk_grid_attach (GTK_GRID (grid), label_host, 0, 0, 1, 1);
gtk_grid_attach (GTK_GRID (grid), h_name, 1, 0, 1, 1);
gtk_grid_attach (GTK_GRID (grid), c_button, 0, 3, 2, 1);
gtk_grid_attach (GTK_GRID (grid), q_button, 0, 4, 2, 1);

gtk_widget_show_all (window);

gtk_main ();

return 0;
}

这有什么问题??
我没有错误也没有警告,但是在终端上这个程序什么也没写:(
如果我写:

test.host="trying something"

它可以工作,但使用 gtk_entry_get_text 它什么也不显示 :(
我不明白......为什么它不适用于 gtk_entry_get_text?

4

1 回答 1

4

您需要了解 GTK 是一个事件驱动的工具包(与许多其他工具包一样)。您需要与事件交互。但在您运行 gtk_main 之前它不会检查事件。因此,您的问题是您正在使用 读取主机名test.host = gtk_entry_get_text(GTK_ENTRY (h_name)),但当时该小部件尚未显示,您甚至没有在其中输入任何内容!所以你基本上只是从中得到一个空字符串,这就是你点击“连接”按钮时显示的内容。

一种方法是让您的指针指向结构中的小部件。这样,您可以从 b_clicked 回调内部调用 gtk_entry_get_text。这样,您获得的值就是当时文本输入小部件内部的

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

typedef struct {
        GtkWidget *host;
} example;

void b_clicked (GtkButton *c_button, example *test){
        g_print("Hostname: %s\n", gtk_entry_get_text (GTK_ENTRY(test->host)));
}

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

        GtkWidget *window;
        GtkWidget *grid;
        GtkWidget *c_button;
        GtkWidget *q_button;
        GtkWidget *label_host;
        GtkWidget *h_name;

        gtk_init (&argc, &argv);

        window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        gtk_window_set_title (GTK_WINDOW (window), "FTP Client");
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
        g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
        gtk_container_set_border_width (GTK_CONTAINER (window), 10);
        gtk_window_set_resizable(GTK_WINDOW(window), FALSE);

        grid = gtk_grid_new ();
        gtk_container_add (GTK_CONTAINER (window), grid);
        gtk_grid_set_row_spacing (GTK_GRID (grid), 3);

        label_host = gtk_label_new("Hostname");

        example test;
        h_name = gtk_entry_new();
        test.host = h_name;
        gtk_entry_set_placeholder_text (GTK_ENTRY (h_name), "Hostname");
        c_button = gtk_button_new_with_label ("Connect");
        g_signal_connect (c_button, "clicked", G_CALLBACK (b_clicked), &test);
        q_button = gtk_button_new_with_label ("Quit");
        g_signal_connect (q_button, "clicked", G_CALLBACK (gtk_main_quit), NULL);

        gtk_grid_attach (GTK_GRID (grid), label_host, 0, 0, 1, 1);
        gtk_grid_attach (GTK_GRID (grid), h_name, 1, 0, 1, 1);
        gtk_grid_attach (GTK_GRID (grid), c_button, 0, 3, 2, 1);
        gtk_grid_attach (GTK_GRID (grid), q_button, 0, 4, 2, 1);

        gtk_widget_show_all (window);

        gtk_main ();

        return 0;
}

另一种更好的方法是在不修改结构的情况下要求在文本更改时得到通知。为此,请使用“ changed ”信号,因为 GtkEntry 实现了 GtkEditable 接口。请参阅“ GtkEntry 文本更改信号”。

另请注意这(gpointer*)&test是错误的。test 是一个结构体,&test 是一个结构体的地址。gpointer 是一个 void *,即。已经是指针,gpointer *指向指针的指针也是如此,这不是 &test 是什么。所以就写吧&test

于 2012-04-23T13:12:46.557 回答