我想用GTade在GTK应用程序左侧制作一个标签栏。我希望像这样获得可调整大小的标签栏区域:
但我无法找到Notebook小部件的Resize选项。
有人知道我怎么能用GLade做到这一点或有任何想法吗?
答案 0 :(得分:1)
GtkNotebook的标签区域不是用户可调整大小的。但是有一些解决方法。解决方案是从GtkNotebook中删除选项卡并使用按钮模拟选项卡栏。这样你就可以将它们分开并将它们包含在GtkPaned中。
遵循此方法的简单glade文件(名为myui.glade
):
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.19.0 -->
<interface>
<requires lib="gtk+" version="3.16"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="default_width">440</property>
<property name="default_height">250</property>
<child>
<object class="GtkPaned" id="paned1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkButton" id="page1">
<property name="label" translatable="yes">Page 1</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkButton" id="page2">
<property name="label" translatable="yes">Page 2</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkButton" id="page3">
<property name="label" translatable="yes">Page 3</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
</object>
<packing>
<property name="resize">False</property>
<property name="shrink">True</property>
</packing>
</child>
<child>
<object class="GtkNotebook" id="notebook1">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="show_tabs">False</property>
<child>
<object class="GtkLabel" id="label4">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">This is Page 1</property>
</object>
</child>
<child type="tab">
<object class="GtkLabel" id="label1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">page 1</property>
</object>
<packing>
<property name="tab_fill">False</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label6">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">This is Page 2</property>
</object>
<packing>
<property name="position">1</property>
</packing>
</child>
<child type="tab">
<object class="GtkLabel" id="label2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">page 2</property>
</object>
<packing>
<property name="position">1</property>
<property name="tab_fill">False</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="label5">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">This is Page 3</property>
</object>
<packing>
<property name="position">2</property>
</packing>
</child>
<child type="tab">
<object class="GtkLabel" id="label3">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">page 3</property>
</object>
<packing>
<property name="position">2</property>
<property name="tab_fill">False</property>
</packing>
</child>
</object>
<packing>
<property name="resize">True</property>
<property name="shrink">True</property>
</packing>
</child>
</object>
</child>
</object>
</interface>
要在GtkNotebook页面之间切换,我们必须处理按钮点击并切换到相应的页面。
行为的简单实现可以是(名为main.c
):
#include <gtk/gtk.h>
void on_page1_button_clicked (GtkButton *button, gpointer user_data) {
GtkNotebook *notebook = GTK_NOTEBOOK(user_data);
gtk_notebook_set_current_page(notebook, 0);
}
void on_page2_button_clicked (GtkButton *button, gpointer user_data) {
GtkNotebook *notebook = GTK_NOTEBOOK(user_data);
gtk_notebook_set_current_page(notebook, 1);
}
void on_page3_button_clicked (GtkButton *button, gpointer user_data) {
GtkNotebook *notebook = GTK_NOTEBOOK(user_data);
gtk_notebook_set_current_page(notebook, 2);
}
int main(int argc, char *argv[]) {
GtkWidget *page1;
GtkWidget *page2;
GtkWidget *page3;
GtkWidget *window;
GtkWidget *notebook;
GtkBuilder *builder;
gtk_init (&argc,&argv);
builder = gtk_builder_new_from_file("myui.glade");
page1 = GTK_WIDGET(gtk_builder_get_object(builder, "page1"));
page2 = GTK_WIDGET(gtk_builder_get_object(builder, "page2"));
page3 = GTK_WIDGET(gtk_builder_get_object(builder, "page3"));
window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
notebook = GTK_WIDGET(gtk_builder_get_object(builder, "notebook1"));
g_signal_connect (page1, "clicked", G_CALLBACK(on_page1_button_clicked), notebook);
g_signal_connect (page2, "clicked", G_CALLBACK(on_page2_button_clicked), notebook);
g_signal_connect (page3, "clicked", G_CALLBACK(on_page3_button_clicked), notebook);
g_signal_connect (window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(GTK_WIDGET(window));
gtk_main();
return 0;
}
编译:
gcc -o notebook main.c `pkg-config --cflags --libs gtk+-3.0`
结果: