Rust学习与实践 (二) GTK4.0 GUI

Rust学习与实践 (二) GTK4.0 GUI

这里我使用的是blueprint这一个工具来进行UI界面的设计

一、BluePrint编译器

首先安装它的编译器

sudo apt install blueprint-compiler

二、创建项目

cargo new p01_hello_blueprint

创建结果如下图所示
在这里插入图片描述
进入到项目目录下

cd p01_hello_blueprint/

在这里插入图片描述

三、添加依赖包

cargo add gtk-blueprint

在这里插入图片描述

cargo add gtk4

在这里插入图片描述

cargo add phf

在这里插入图片描述

四、编辑src/main.blp

using Gtk 4.0;

ApplicationWindow MyAppWindow{
  default-width: 600;
  default-height: 300;
  title: _("Hello, Blueprint!");
  
  Box welcome {
    orientation: vertical;
    valign: center;
    halign: center;

    Image {
      name: "logo";
      icon-name: "re.sonny.Workbench";
      pixel-size: 196;
      margin-bottom: 30;

      styles [
        "icon-dropshadow",
      ]
    }

    Label {
      label: "Welcome to Workbench";
      margin-bottom: 30;

      styles [
        "title-1",
      ]
    }

    Box subtitle {
      orientation: vertical;
      halign: center;
      margin-bottom: 30;

      Label {
        label: "Learn and prototype with\nGNOME technologies";
        justify: center;
      }
    }

    Box {
      orientation: vertical;
      homogeneous: true;
      halign: center;

      Box {
        margin-bottom: 6;

        Image {
          icon-name: "update-symbolic";
          margin-end: 12;
          icon-size: normal;
        }

        Label {
          label: "Edit Style or UI to update the Preview";
        }
      }

      Box {
        margin-bottom: 6;

        Image {
          icon-name: "media-playback-start-symbolic";
          margin-end: 12;
          icon-size: normal;
        }

        Label {
          label: "Hit";
        }

        ShortcutsShortcut {
          accelerator: "<Control>Return";
          margin-start: 12;
        }

        Label {
          label: "to format and run Code";
        }
      }

      Box {
        margin-bottom: 6;

        Image {
          icon-name: "media-floppy-symbolic";
          margin-end: 12;
          icon-size: normal;
        }

        Label {
          label: "Changes are automatically saved and restored";
        }
      }

      Box {
        margin-bottom: 6;

        Image {
          icon-name: "library-symbolic";
          margin-end: 12;
          icon-size: normal;
        }

        Label {
          label: "Browse the Library for demos and examples";
        }
      }

      Box {
        margin-bottom: 6;

        Image {
          icon-name: "user-bookmarks-symbolic";
          margin-end: 12;
          icon-size: normal;
        }

        Label {
          label: "Checkout the Bookmarks menu to learn and get help";
        }
      }
    }
  }
}

五、编辑src/main.rs

use gtk4 as gtk;
use gtk4::Builder;
use gtk_blueprint::include_blp;
use gtk::Application;
use gtk::prelude::*;


gtk_blueprint::gen_blp_map!("");

fn main() {

    // Create app
    let application = Application::builder()
        .application_id("org.example.HelloWorld")
        .build();

    // Init app window and show it
    application.connect_activate(|app| {
        // You also can parse blueprint with Parser::parse
        // and then use it in gtk4::Builder
        let builder = Builder::new();
        let _ = builder.add_from_string(include_blp!("src/main.blp"));
        let window = builder.object::<gtk::ApplicationWindow>("MyAppWindow").unwrap();

        window.set_application(Some(app));

        window.present();
        println!("Hello World");
    });

    // Run app
    application.run();
}

六、安装系统依赖环境

sudo apt install pkg-config librust-gobject-sys-dev libgdk-pixbuf-2.0-dev libcairo2-dev libgtk-4-dev

七、编译程序

cargo build

会下载程序包
在这里插入图片描述

八、运行程序

cargo run

在这里插入图片描述
由于开发板没有接显示器,所以无法正常运行,在命令行设置一下远程服务器(你的电脑)来显示界面

export DISPLAY=192.168.8.4:0.0

再次运行,效果如下图所示
在这里插入图片描述
远程的效果有点怪怪的

UI设计可以使用Ubuntu下的Workbench
在这里插入图片描述
所见即所得的方式,不知道为什么在Windows上面看起来那么丑,甚至图片都没有显示,有钱建议配个屏幕,因为ubuntu运行起来是这样的
在这里插入图片描述

踩坑

在更新了blp文件之后,需要删除target/debug/deps/下的可执行文件,这样在运行cargo run命令时才会重新生成二进制文件(猜测是把资源文件也集成到了可执行程序里面了,查看大小由70多M)

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用 GTK-rs 库需要先安装 GTK+3,可以在 Linux 系统下通过包管理器安装,也可以在 Windows 和 macOS 下从官网下载安装程序进行安装。 安装 GTK+3 后,可以在 Rust 项目中添加 gtk 和 gdk 两个依赖: ```toml [dependencies] gtk = "0.9" gdk = "0.9" ``` 然后在 Rust 代码中引入 gtk 和 gdk 库: ```rust extern crate gtk; extern crate gdk; ``` 在 GTK-rs 中,主要有两种方式创建 GUI 窗口:Builder 和手动创建。下面分别介绍这两种方式。 ### 使用 Builder 创建 GUI 窗口 使用 Builder 可以通过 XML 文件描述 GUI 界面,然后使用 Rust 代码加载并显示窗口。以下是一个简单的例子: ```xml <?xml version="1.0" encoding="UTF-8"?> <interface> <object class="GtkWindow" id="main_window"> <property name="title">Hello, World!</property> <property name="default-width">400</property> <property name="default-height">300</property> <child> <object class="GtkLabel" id="label"> <property name="label">Hello, World!</property> </object> </child> </object> </interface> ``` 在 Rust 代码中,可以使用 gtk::Builder 加载并显示窗口: ```rust use gtk::prelude::*; use gtk::{Builder, Window}; fn main() { if let Err(err) = gtk::init() { eprintln!("Failed to initialize GTK: {}", err); return; } let glade_src = include_str!("path/to/glade/file.glade"); let builder = Builder::from_string(glade_src); let window: Window = builder.get_object("main_window").unwrap(); window.show_all(); gtk::main(); } ``` ### 手动创建 GUI 窗口 手动创建 GUI 窗口需要在 Rust 代码中定义窗口和窗口中的组件,以下是一个简单的例子: ```rust use gtk::prelude::*; use gtk::{Window, WindowType, Label}; fn main() { if let Err(err) = gtk::init() { eprintln!("Failed to initialize GTK: {}", err); return; } let window = Window::new(WindowType::Toplevel); window.set_title("Hello, World!"); window.set_default_size(400, 300); let label = Label::new(Some("Hello, World!")); window.add(&label); window.show_all(); gtk::main(); } ``` 以上代码中,创建了一个顶层窗口和一个标签组件,将标签添加到窗口中并显示窗口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值