【问题标题】:How can i press a button in headerbar and do things in window in vala我如何在标题栏中按下按钮并在 vala 的窗口中执行操作
【发布时间】:2025-12-01 07:00:02
【问题描述】:

如何在标题栏中制作按钮并在我尝试过的窗口中执行操作:

public class headerbar : Gtk.HeaderBar {
    construct {
        title = "Quiz";
        subtitle = "You can solve this!";
        show_close_button = true;
        Gtk.Button button = new Gtk.Button.with_label ("Quit");
        button.get_style_context ().add_class ("suggested-action");
        button.set_valign (Gtk.Align.CENTER);
        button.clicked.connect (() => {
            var label = new Gtk.Label ("Hi");
            main.add (label);
            label.show ();
        });
        pack_start (button);
    }
}

【问题讨论】:

    标签: button window gtk3 vala


    【解决方案1】:

    我得到了一个样本如下,希望它可以帮助你:

    public class AppTitleBar : Gtk.HeaderBar {
        private Gtk.Button m_new_tab_button;
        private Gtk.Box m_app_title_gui_box;
        private Gtk.MenuButton m_main_menu_button;
    
        public weak Workbench workbench { get; construct; }
    
        public AppTitleBar (Workbench workbench) {
            Object ( workbench: workbench );
        }
    
        construct {
            set_show_close_button (true);
    
            this.build_title_bar_layout();
        }
    
        public void add_new_tab(Widget title_widget)
        {
            m_app_title_gui_box.pack_start(title_widget);
            title_widget.focus.connect(() => {
                this.update_tab_inactive_all();
                return true;
            });
        }
    
        public void update_tab_inactive_all()
        {
            m_app_title_gui_box.foreach ((widget) => {
                widget.set_opacity(0.3);
            });
        }
    
        public void remove_tab_widget(Widget tab_bar)
        {
            m_app_title_gui_box.remove(tab_bar);
        }
    
        private void build_title_bar_layout()
        {
            m_main_menu_button = new Gtk.MenuButton ();
            var menu_image = new Gtk.Image.from_icon_name ("open-menu-symbolic", Gtk.IconSize.BUTTON);
            m_main_menu_button.set_image (menu_image);
            m_main_menu_button.tooltip_text = ("Main Menu");
    
            // create title bar box
            m_app_title_gui_box = new Box(Orientation.HORIZONTAL, 2);
    
            // create add new tab button
            m_new_tab_button = new Gtk.Button.from_icon_name("list-add", Gtk.IconSize.BUTTON);
            m_new_tab_button.get_style_context ().add_class ("back-button");
            m_new_tab_button.valign = Gtk.Align.CENTER;
            m_new_tab_button.always_show_image = true;
            m_new_tab_button.can_focus = false;
            m_new_tab_button.action_name = WorkbenchActions.ACTION_PREFIX + WorkbenchActions.ACTION_TAB_NEW;
            m_new_tab_button.has_tooltip = true;
            m_new_tab_button.tooltip_text = "Open a new connection (Ctrl+N)";
    
            this.pack_end(m_main_menu_button);
            this.pack_start(m_app_title_gui_box);
            this.pack_start(m_new_tab_button);
        }
    }
    

    【讨论】: