当前位置: 首页 > article >正文

Rust语言使用iced实现简单GUI页面

使用cargo新建一个rust项目

cargo new gui_demo
cd gui_demo

编辑Cargo.toml文件 ,添加iced依赖

[package]
name = "gui_demo"
version = "0.1.0"
edition = "2021"

[dependencies]
iced = "0.4.2"

编辑src/main.rs文件:

use iced::{button, widget::{Button, Column, Text}, Application, Command, Element, Settings, Subscription};
use iced::executor::Default as Executor;

// 定义应用程序的状态
struct RustGuiApp {
    count: i32,
    button_state: button::State,
}

// 定义应用程序的消息类型
#[derive(Debug, Clone, Copy)]
enum Message {
    IncrementPressed,
}

impl Application for RustGuiApp {
    type Executor = Executor;
    type Message = Message;
    type Flags = ();

    fn new(_flags: ()) -> (Self, Command<Self::Message>) {
        (
            RustGuiApp {
                count: 0,
                button_state: button::State::new(),
            },
            Command::none(),
        )
    }
    
    // 设置窗口标题
    fn title(&self) -> String {
        String::from("Rust GUI Example")
    }

    fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
        if let Message::IncrementPressed = message {
            self.count += 1;
        }
        Command::none()
    }

    fn view(&mut self) -> Element<Self::Message> {
        let button = Button::new(&mut self.button_state, Text::new("Increment"))
            .on_press(Message::IncrementPressed);

        //    添加文字和一些你想加的东西
        Column::new()
            .push(Text::new("Hello, Rust GUI!").size(50))
            .push(Text::new(self.count.to_string()).size(50))
            .push(button)
            .into()
    }

    fn subscription(&self) -> Subscription<Self::Message> {
        Subscription::none()
    }
}

fn main() -> iced::Result {
    RustGuiApp::run(Settings::default())
}

最后使用命令运行

cargo run

运行效果展示,还是挺不错滴


http://www.kler.cn/a/471761.html

相关文章:

  • AF3 one_hot函数解读
  • RPC自定义协议
  • 搭建企业AI助理的创新应用与案例分析
  • 谷粒商城-高级篇完结-Sleuth+Zipkin 服务链路追踪
  • 搭建Hadoop分布式集群
  • 中学教资笔记1
  • 【微服务】6、限流 熔断
  • redis学习笔记(一)了解redis
  • 如何设置通过Visual Studio(VS)打开的C#项目工具集?
  • 书籍推荐:MySQL 是怎样运行的-从根上理解 MySQL
  • C# 之某度协议登录,JS逆向,手机号绑定,获取CK
  • Tableau数据可视化与仪表盘搭建-可视化原则及BI仪表盘搭建
  • 05容器篇(D2_集合 - D6_容器源码分析篇 - D1_ArrayList)
  • Flex布局的三个属性
  • 2025年1月4日蜻蜓q旗舰版st完整开源·包含前后端所有源文件·开源可商用可二开·优雅草科技·优雅草kir|优雅草星星|优雅草银满|优雅草undefined
  • WPF中RenderTargetBitmap问题解决
  • 服务器等保测评审计日志功能开启(auditd)和时间校准
  • 如何从串 ‘ 中国 +86‘ 中,获取到‘中国’:strip()、split()及正则表达式的使用
  • 通达信行情接口失效?C# 实现获取五档报价行情 GetSecurityQuotes
  • Ubuntu 安装 Java 1.8