2012-04-21 57 views
1

我有這樣的代碼:GTK網格中添加兩個小部件

/* Descrizione: GUI Client FTP sviluppato come progetto per il corso di Reti di Calcolatori (laurea SSRI presso DTI Crema) 
* Sviluppatori: Paolo Stivanin, Filippo Roncari, Stefano Agostini. 
* Anno: 2012 
* Licenza: GNU AGPL v3 <http://www.gnu.org/licenses/agpl-3.0.html> 
* Sito web: <https://github.com/polslinux/FTPUtils> */ 

#include <gtk/gtk.h> 

/* When "connect" button is clicked a message will appear */ 
static void print_hello (GtkWidget *widget, gpointer data) 
{ 
    g_print ("Connect button clicked\n"); 
} 

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

/* Declare widgets */ 
GtkWidget *window; 
GtkWidget *grid; 
GtkWidget *button; 
GtkWidget *u_name; 
GtkWidget *h_name; 
GtkWidget *pass; 
GtkWidget *label_user; 
GtkWidget *label_host; 
GtkWidget *label_pass; 

/*This is called in all GTK applications. Arguments are parsed from the command line and are returned to the application.*/ 
gtk_init (&argc, &argv); 

/* create a new window, set its title and put it on center */ 
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); /* window is NOT resizable */ 

/* Here we construct the container that is going pack our buttons */ 
grid = gtk_grid_new(); 
gtk_grid_set_row_spacing (GTK_GRID (grid), 3); 

/* Pack the container in the window */ 
gtk_container_add (GTK_CONTAINER (window), grid); 

/* Add labels */ 
label_user = gtk_label_new("Username "); 
label_host = gtk_label_new("Hostname "); 
label_pass = gtk_label_new("Password "); 

/* Add hostname entry and label */ 
h_name = gtk_entry_new(); 
gtk_entry_set_placeholder_text (GTK_ENTRY (h_name), "Hostname"); /* imposto il testo trasparente che scompare quando clicco o scrivo */ 
gtk_grid_attach (GTK_GRID (grid), label_host, 0, 0, 1, 1); 
gtk_grid_attach (GTK_GRID (grid), h_name, 1, 0, 1, 1); 

/* Add username entry and label */ 
u_name = gtk_entry_new(); 
gtk_entry_set_placeholder_text (GTK_ENTRY (u_name), "Username"); 
gtk_grid_attach (GTK_GRID (grid), label_user, 0, 1, 1, 1); 
gtk_grid_attach (GTK_GRID (grid), u_name, 1, 1, 2, 1); 

/* Add password entry and label (visibility set to 0 = not visible */ 
pass = gtk_entry_new(); 
gtk_entry_set_placeholder_text (GTK_ENTRY (pass), "Password"); 
gtk_grid_attach (GTK_GRID (grid), label_pass, 0, 2, 1, 1); 
gtk_entry_set_visibility (GTK_ENTRY (pass), 0); 
gtk_grid_attach (GTK_GRID (grid), pass, 1, 2, 1, 1); 

/* Add connect button */ 
button = gtk_button_new_with_label ("Connect"); 
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL); 
gtk_grid_attach (GTK_GRID (grid), button, 0, 3, 2, 1); 

/* Add quit button */ 
button = gtk_button_new_with_label ("Quit"); 
g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL); 
gtk_grid_attach (GTK_GRID (grid), button, 0, 4, 2, 1); 

/* Now that we are done packing our widgets, we show them all in one go, by calling gtk_widget_show_all() on the window.*/ 
gtk_widget_show_all (window); 

/* All GTK applications must have a gtk_main(). Control ends here and waits for an event to occur (like a key press or a mouse event), 
* until gtk_main_quit() is called. */ 
gtk_main(); 

return 0; 
} 

,我想添加更多的小工具......我該怎麼辦呢? 例如,我想添加一個菜單欄(有關於例如菜單),但如果我這樣做:

gtk_container_add (GTK_CONTAINER (window), menubar); 

編譯器告訴我,我不能添加超過1個構件到窗口...

我在Ubuntu 12.04上開發GTK + v3.4.1的gui!

+0

您能否縮小問題範圍,以便我們不必一直讀取代碼轉儲?這將使人們能夠更快更輕鬆地幫助你。 – ptomato 2012-04-21 19:20:52

回答

2

您必須將第二個小部件添加到網格,而不是窗口。該窗口只能包含一個小部件,在這種情況下是網格。網格可以包含多個小部件。

+0

感謝您的幫助...我寫了這個: 'gtk_grid_attach(GTK_GRID(grid),menub,1,1,1,1); menub = gtk_menu_bar_new(); file_item = gtk_menu_item_new_with_label(「File」); file_menu = gtk_menu_new(); gtk_menu_item_set_submenu(GTK_MENU_ITEM(file_item),file_menu);' 但在執行時,我得到一個錯誤: 'gtk_grid_attach:斷言'GTK_IS_WIDGET(孩子)」 failed' – polslinux 2012-04-22 08:33:07

+0

看起來你正在創建的菜單欄小工具嘗試後附上它。先創建它,然後附加它。 – ptomato 2012-04-22 09:22:13

+0

我已更新代碼和問題描述... PS:感謝您的幫助:) – polslinux 2012-04-22 10:02:43