我如何在标题栏中按下按钮并在 vala 的窗口中执行操作

Posted

技术标签:

【中文标题】我如何在标题栏中按下按钮并在 vala 的窗口中执行操作【英文标题】:How can i press a button in headerbar and do things in window in vala 【发布时间】:2019-12-21 20:16:35 【问题描述】:

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

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);
    

【问题讨论】:

【参考方案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);
    

【讨论】:

以上是关于我如何在标题栏中按下按钮并在 vala 的窗口中执行操作的主要内容,如果未能解决你的问题,请参考以下文章

在 Python Tkinter 中按下按钮后如何清除窗口?

在iOS中按下后退按钮时如何创建确认弹出窗口?

在导航栏中按下后退按钮时刷新 RootViewController 的内容

在.NET MVC中按下提交按钮后如何在屏幕顶部显示成功弹出窗口?

如何在python tkinter中按下按钮之前使窗口状态空闲?

在父活动上按下后退按钮