从指定目录读取图像文件

时间:2019-08-23 21:50:22

标签: gtk vala

我是Vala(显然也是C)的初学者。完成一些培训后,我决定创建简单的图库应用程序。我认为这是最好的学习方式。我已经研究过Valadoc,但问题是我不知道如何管理从指定目录中读取图像文件并通过单击一个按钮一个一个地显示它们。

我创建了一些代码,该代码显示了来自指定文件的图像,并通过单击按钮对其进行了更改。在互联网上搜索并尝试了一些不合适的解决方案后,我陷入了困境。

public class GalleryApp : Gtk.Application {

    public GalleryApp () {
        Object (
            application_id: "com.github.roypen.Gallery",
            flags: ApplicationFlags.FLAGS_NONE
        );
    }

    protected override void activate () {

        var main_window = new Gtk.ApplicationWindow (this);
        main_window.default_width = 800;
        main_window.default_height = 600;
        main_window.title = "Simple Image Gallery";


        var layout = new Gtk.Grid ();
        layout.column_spacing = 10;
        layout.row_spacing = 10;
        layout.halign = Gtk.Align.CENTER;
        layout.valign = Gtk.Align.CENTER;
        var image = new Gtk.Image.from_file ("images/image_1.jpg");

        var left_button = new Gtk.Button.with_label ("<-");
        var right_button = new Gtk.Button.with_label ("->");

        layout.attach (image, 1, 0, 2, 1);
        layout.attach (left_button, 1, 1, 1, 1);
        layout.attach (right_button, 2, 1, 1, 1);

        left_button.clicked.connect (() => {
            image.file = "images/image_2.jpg";
            left_button.sensitive = true;
        });

        right_button.clicked.connect (() => {
            image.file = "images/image_1.jpg";
            right_button.sensitive = true;
        });

        main_window.add (layout);
        main_window.show_all ();

    }

    public static int main (string[] args) {
        var app = new GalleryApp ();
        return app.run (args);
    }

}

0 个答案:

没有答案
相关问题