我在使用GTK绘制文本时遇到了困难

时间:2019-02-01 15:52:35

标签: c gtk

我尝试键入此代码以在屏幕上显示文本,但是在编译然后打开已编译的应用程序时没有文本。为什么不做这项工作?我正在使用C和Linux,并且正在尝试使用gtk头文件。我在做什么错了?

{t, position}

我该如何进行这项工作?我希望它在编译然后打开时显示文本。我试了好东西,他们没有工作。

2 个答案:

答案 0 :(得分:1)

您忘了将文本框添加到窗口的某个实际显示位置,就像这样

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

static void helloWorld (GtkWidget *wid, GtkWidget *win) {
    GtkWidget *dialog = NULL;

    dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Hello World! My name is ****");
    gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);
}

int main (int argc, char *argv[]) {
    GtkWidget *button = NULL;
    GtkWidget *win = NULL;  
    GtkWidget *vbox = NULL;

    /* Initialize GTK+ */
    g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
    gtk_init (&argc, &argv);
    g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);

    /* Create the main window */
    win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_window_set_default_size(GTK_WINDOW (win), 400, 300);
    gtk_container_set_border_width (GTK_CONTAINER (win), 8);
    gtk_window_set_title (GTK_WINDOW (win), "The stupid GTK test");
    gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
    gtk_widget_realize (win);
    g_signal_connect (win, "destroy", gtk_main_quit, NULL);

    /* Create a vertical box with buttons*/

    vbox = gtk_vbox_new (TRUE, 6);
    gtk_container_add (GTK_CONTAINER (win), vbox);

    button = gtk_button_new_from_stock (GTK_STOCK_DIALOG_INFO);
    g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (helloWorld), (gpointer) win);
    gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);

    button = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
    g_signal_connect (button, "clicked", gtk_main_quit, NULL);
    gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0);

    GtkWidget *view;
    GtkTextBuffer *buffer;
    GtkTextIter start, end;
    PangoFontDescription *font_desc;
    GdkColor color;
    GtkTextTag *tag;

    view = gtk_text_view_new ();

    buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view));

    gtk_text_buffer_set_text (buffer, "Hello, this is some text", -1);

    /* Change default font throughout the widget */
    font_desc = pango_font_description_from_string ("Serif 15");
    gtk_widget_modify_font (view, font_desc);
    pango_font_description_free (font_desc);

    /* Change default color throughout the widget */
    gdk_color_parse ("green", &color);
    gtk_widget_modify_text (view, GTK_STATE_NORMAL, &color);

    /* Change left margin throughout the widget */
    gtk_text_view_set_left_margin (GTK_TEXT_VIEW (view), 30);

    /* Use a tag to change the color for just one part of the widget */
    tag = gtk_text_buffer_create_tag (buffer, "blue_foreground",
                            "foreground", "blue", NULL);  
    gtk_text_buffer_get_iter_at_offset (buffer, &start, 7);
    gtk_text_buffer_get_iter_at_offset (buffer, &end, 12);
    gtk_text_buffer_apply_tag (buffer, tag, &start, &end);
    /*Enter the main loop */
    gtk_widget_show_all (win);
    gtk_main ();
    return 0;
}

(必须在定义视图之后且在show_all调用之前)。 如果您查看代码,就会看到此调用是针对所有其他出现的小部件的。实际上,您的问题并不只限于绘图文本。

答案 1 :(得分:0)

您是否要使用所有这些代码只是为了绘制文本?您知道,您可以用比这简单得多的方式绘制文本。

对于文本来说,最少的代码如下:

gtk_container_add(GTK_CONTAINER(Window), gtk_label_new("Hello, World"));