在Linux(基本OS)上的VALA中带有图标,文本和两个按钮的简单对话框

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Linux(基本OS)上的VALA中带有图标,文本和两个按钮的简单对话框相关的知识,希望对你有一定的参考价值。

最近两天,我试图找出如何在Linux上的Vala(elementaryOS JUNO)中使用左侧的图标,右侧的文本和下方的两个按钮来实现标准的对话框布局。也无法为此找到任何模板。这是我的代码,随时可以对其进行修改。

/*
 * The Button widget is commonly found in programs and used to launch processes
 * and operations.
*/

using Gtk;

public class XScreensaverControl : Window

    private Button button;

    public XScreensaverControl()
    
        this.title = "Control XScreensaver";
        this.destroy.connect(Gtk.main_quit);

        //Box with icon and text in horizontal layout
        var hbox = new Box(Gtk.Orientation.HORIZONTAL, 20);
        hbox.set_spacing(5);
        this.add(hbox);
        var image = new Gtk.Image.from_icon_name ("dialog-question", Gtk.IconSize.DIALOG);
        image.halign = Gtk.Align.START;
        hbox.add(image);
        var label1 = new Label(null);
        hbox.add(label1);
        label1.set_markup("What do you want to do with <b>XScreensaver</b>?");
        hbox.margin = 12;

        //Box with buttons in horizontal layout
        var hbox2 = new Box(Gtk.Orientation.HORIZONTAL, 20);
        button = new Button();
        button.set_label("Turn Off");
        button.get_style_context ().add_class (Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION);
        button.clicked.connect(off_button_clicked);
        hbox2.add(button);
        button = new Button();
        button.set_label("Turn On");
        button.get_style_context ().add_class (Gtk.STYLE_CLASS_SUGGESTED_ACTION);
        button.clicked.connect(on_button_clicked);
        hbox2.add(button);
        hbox2.halign = Gtk.Align.END;
        resizable = false;

        //Vertical box container for two horizontal boxes
        var vbox = new Box(Gtk.Orientation.VERTICAL, 1);
        vbox.set_spacing(1);
        vbox.add(hbox2);
        hbox.add(vbox);

    

    private void off_button_clicked(Button button)
    
        var label = button.get_label();
        stdout.printf("%s clicked\n", label);
                // Non blocking - does not wait for process to finish
        Process.spawn_command_line_async ("xscreensaver-command -exit");
    

    private void on_button_clicked(Button button)
    
        var label = button.get_label();
        stdout.printf("%s clicked\n", label);
                // Non blocking - does not wait for process to finish
        Process.spawn_command_line_async ("xscreensaver -nosplash");
    


    public static int main(string[] args)
    
        Gtk.init(ref args);

        var window = new XScreensaverControl();
        window.show_all();

        Gtk.main();

        return 0;
    


目前,它会产生以下结果:enter image description here

但是我想得到这样的结果(在photopea.com中创建的模型):enter image description here

经过两个下午的思考后,我成为一名业余爱好者,如何实现这种布局。

答案
using Gtk;

public class XScreensaverControl : Window

    private Button button1 = new Button.with_label("Turn Off");
    private Button button2 = new Button.with_label("Turn On");

    public XScreensaverControl()
    
        this.title = "Control XScreensaver";
        this.destroy.connect(Gtk.main_quit);

        //Box with icon and text in horizontal layout
        var hbox = new Box(Gtk.Orientation.HORIZONTAL, 20);
        hbox.set_spacing(5);
        var image = new Gtk.Image.from_icon_name ("dialog-question", Gtk.IconSize.DIALOG);
        image.halign = Gtk.Align.START;
        hbox.add(image);
        var label1 = new Label(null);
        hbox.add(label1);
        label1.set_markup("What do you want to do with <b>XScreensaver</b>?");
        hbox.margin = 12;

        //Box with buttons in horizontal layout
        var hbox2 = new Box(Gtk.Orientation.HORIZONTAL, 20);

        button1.get_style_context ().add_class (Gtk.STYLE_CLASS_DESTRUCTIVE_ACTION);
        button1.clicked.connect(off_button_clicked);
        hbox2.add(button1);
        button2.get_style_context ().add_class (Gtk.STYLE_CLASS_SUGGESTED_ACTION);
        button2.clicked.connect(on_button_clicked);
        hbox2.add(button2);
        hbox2.halign = Gtk.Align.END;
        resizable = false;

        //Vertical box container for two horizontal boxes
        var vbox = new Box(Gtk.Orientation.VERTICAL, 1);
        vbox.set_spacing(1);
        vbox.add(hbox);
        vbox.add(hbox2);
        this.add(vbox);
    

    private void off_button_clicked(Button button)
    
        var label = button.get_label();
        stdout.printf("%s clicked\n", label);
                // Non blocking - does not wait for process to finish
        Process.spawn_command_line_async ("xscreensaver-command -exit");
    

    private void on_button_clicked(Button button)
    
        var label = button.get_label();
        stdout.printf("%s clicked\n", label);
                // Non blocking - does not wait for process to finish
        Process.spawn_command_line_async ("xscreensaver -nosplash");
    


    public static int main(string[] args)
    
        Gtk.init(ref args);
        var window = new XScreensaverControl();
        window.show_all();
        Gtk.main();
        return 0;
    


以上是关于在Linux(基本OS)上的VALA中带有图标,文本和两个按钮的简单对话框的主要内容,如果未能解决你的问题,请参考以下文章

Mac OS X >= 10.6 上的 Finder 文件图标标记(图标覆盖)

linux里的文件夹上有个锁头的图标,表示啥

Mac OS X 上的 OpenGL 纹理损坏

如何在 OS X 上的 Qt 应用程序中设置应用程序图标,足以分发?

项目 SAPUI5 中带有文件夹 src 的自定义图标

Linux基本命令及获取帮助小结1