2
我有以下代碼,並且當我按下te按鈕「hlpBtn」時我想更改標籤「標題」的值,但我遇到了麻煩。在GTK + 2中單擊按鈕後重新定義標籤
#include <gtk/gtk.h>
void button_clicked(GtkWidget *widget, gpointer data)
{
GtkWidget *title = (GtkWidget *) data;
title = gtk_label_new("DECODED!!");
}
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *table;
GtkWidget *title;
GtkWidget *wins;
GtkWidget *halign;
GtkWidget *halign2;
GtkWidget *hlpBtn;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_widget_set_size_request (window, 650, 850);
gtk_window_set_title(GTK_WINDOW(window), "Windows");
gtk_container_set_border_width(GTK_CONTAINER(window), 15);
table = gtk_table_new(6, 4, FALSE);
gtk_table_set_col_spacings(GTK_TABLE(table), 3);
gtk_table_set_row_spacing(GTK_TABLE(table), 0, 3);
title = gtk_label_new("Decrypting code...");
halign = gtk_alignment_new(0, 0, 0, 0);
gtk_container_add(GTK_CONTAINER(halign), title);
gtk_table_attach(GTK_TABLE(table), halign, 0, 1, 0, 1,
GTK_FILL, GTK_FILL, 0, 0);
halign2 = gtk_alignment_new(0, 1, 0, 0);
hlpBtn = gtk_button_new_with_label("RUN");
gtk_container_add(GTK_CONTAINER(halign2), hlpBtn);
gtk_widget_set_size_request(hlpBtn, 70, 30);
gtk_table_set_row_spacing(GTK_TABLE(table), 3, 5);
gtk_table_attach(GTK_TABLE(table), halign2, 0, 1, 4, 5,
GTK_FILL, GTK_FILL, 0, 0);
g_signal_connect(G_OBJECT(hlpBtn), "clicked",
G_CALLBACK(button_clicked), title);
gtk_container_add(GTK_CONTAINER(window), table);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), G_OBJECT(window));
gtk_widget_show_all(window);
gtk_main();
return 0;
}
我的本意是叫我按hlpBtn然後changue標題從「解密代碼...」來button_clicked的funcition「DECODED!」。
這裏有什麼問題?
謝謝。
如果來自GTK3 +的這個原則成立(GTK2文檔不說...),我想你也可以調用[gtk_widget_destroy](https://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk -widget-destroy),這會自動從其父容器中刪除它。然後用'gtk_container_add'將一個新標籤插入到該容器中。也就是說,在這種情況下沒有理由,並且在這裏設置標籤文本是正確的答案。 –
@BrianMcFarland好評。這個http://developer.gimp.org/api/2.0/gtk/GtkWidget.html#gtk-widget-destroy應該是** GTK + 2.12.9 **的有效文檔,它確認了你的建議。 –