为了账号安全,请及时绑定邮箱和手机立即绑定

求在linux系统上用GTK编写图形界面程序,要求用到窗口按钮标签容器和布局?

求在linux系统上用GTK编写图形界面程序,要求用到窗口按钮标签容器和布局?

阿波罗的战车 2019-02-08 11:07:14
求在linux系统上用GTK编写图形界面程序,要求用到窗口按钮标签容器和布局
查看完整描述

1 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

void enter_callback( GtkWidget *widget,
GtkWidget *entry )
{
const gchar *entry_text;
entry_text = gtk_entry_get_text (GTK_ENTRY (entry));
printf("Entry contents: %s\n", entry_text);
}

void entry_toggle_editable( GtkWidget *checkbutton,
GtkWidget *entry )
{
gtk_editable_set_editable (GTK_EDITABLE (entry),
GTK_TOGGLE_BUTTON (checkbutton)->active);
}

void entry_toggle_visibility( GtkWidget *checkbutton,
GtkWidget *entry )
{
gtk_entry_set_visibility (GTK_ENTRY (entry),
GTK_TOGGLE_BUTTON (checkbutton)->active);
}
/* 我们常见的回调函数 */
void callback( GtkWidget *widget,
gpointer data )
{
g_print ("Hello again - %s was pressed\n", (char *) data);
}
int main( int argc, char *argv[])
{
GtkWidget *window;

GtkWidget *ok;
GtkWidget *close;

GtkWidget *text_view;
GtkTextBuffer *buffer;

GtkWidget *vbox;
GtkWidget *hbox;

GtkWidget *halign;
GtkWidget *valign;

gtk_init(&argc, &argv);

//=============Build a window===========//
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 350, 280);
gtk_window_set_title(GTK_WINDOW(window), "Labyrinth");
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
g_signal_connect_swapped(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), G_OBJECT(window));
//=============Adjust layout============//
vbox = gtk_vbox_new(FALSE, 5);
valign = gtk_alignment_new(0, 1, 0, 0);
gtk_container_add(GTK_CONTAINER(vbox), valign);
gtk_container_add(GTK_CONTAINER(window), vbox);
hbox = gtk_hbox_new(TRUE, 3);
//=============Build tow button========//
ok = gtk_button_new_with_label("OK");
gtk_widget_set_size_request(ok, 70, 30);
gtk_container_add(GTK_CONTAINER(hbox), ok);
close = gtk_button_new_with_label("Close");
gtk_container_add(GTK_CONTAINER(hbox), close);
halign = gtk_alignment_new(1, 0, 0, 0);
close = gtk_button_new_from_stock (GTK_STOCK_CLOSE);
gtk_container_add(GTK_CONTAINER(halign), hbox);
gtk_box_pack_start(GTK_BOX(vbox), halign, FALSE, FALSE, 0);
g_signal_connect_swapped (G_OBJECT (close), "clicked",
G_CALLBACK (gtk_widget_destroy),
window);
GTK_WIDGET_SET_FLAGS (close, GTK_CAN_DEFAULT);
gtk_widget_grab_default (close);
gtk_widget_show (close);

//=============Build a TestView==========//
text_view=gtk_text_view_new();/*创建文本框构件*/
gtk_widget_set_size_request(text_view,200,200);
gtk_container_add(GTK_CONTAINER(window),vbox);
gtk_box_pack_start(GTK_BOX(vbox),text_view,FALSE,FALSE,0);
buffer=gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
gtk_widget_show(text_view);
//=============================
GtkWidget *entry;
gint tmp_pos;

entry = gtk_entry_new ();
gtk_entry_set_max_length (GTK_ENTRY (entry), 50);

g_signal_connect (G_OBJECT (entry), "OK",
G_CALLBACK (enter_callback), entry);
gtk_entry_set_text (GTK_ENTRY (entry), "hello");
tmp_pos = GTK_ENTRY (entry)->text_length;
gtk_editable_insert_text (GTK_EDITABLE (entry), " world", -1, &tmp_pos);
gtk_editable_select_region (GTK_EDITABLE (entry),
0, GTK_ENTRY (entry)->text_length);

// tmp_pos = GTK_ENTRY (entry)->text_length;
// gtk_editable_insert_text (GTK_EDITABLE (entry), " hello ", -1, &tmp_pos);
// gtk_editable_select_region (GTK_EDITABLE (entry),
// 0, GTK_ENTRY (entry)->text_length);

gtk_box_pack_start (GTK_BOX (vbox), entry, TRUE, TRUE, 0);

gtk_widget_show (entry);
gtk_widget_show_all(window);
gtk_main();
return 0;
}



查看完整回答
反对 回复 2019-03-03
  • 1 回答
  • 0 关注
  • 863 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信