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

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.lryc.cn/news/519401.html

相关文章:

  • 使用wav2vec 2.0进行音位分类任务的研究总结
  • 25/1/11 嵌入式笔记<esp32> 初入esp32
  • 基于SMT32U575RIT单片机-中断练习
  • 在Django的Serializer的列表数据中剔除指定元素
  • 我喜欢的数学题
  • Redis解决热key问题
  • 【git】-2 分支管理
  • Win11+WLS Ubuntu 鸿蒙开发环境搭建(二)
  • Meilisearch ASP.Net Core API 功能demo
  • 程序员独立开发竞品分析:确定网站使用什么建站系统
  • selenium+pyqt5自动化工具总结
  • docker GPU安装
  • hutool糊涂工具通过注解设置excel宽度
  • Three.js教程015:全面讲解Three.js的UV与应用
  • IOS界面传值-OC
  • 阿里mod_asr3.0集成webrtc静音算法
  • [Git] git pull --rebase / git rebase origin/master
  • Leetcode​​​​​​​3270:求出数字答案
  • 第十一章 施工监测
  • Python爬虫应用领域
  • 软件架构考试基础知识 002:进程的状态与其切换
  • 新车月交付突破2万辆!小鹏汽车“激活”智驾之困待解
  • VideoPlayer插件的功能和用法
  • .NET体系架构
  • QT中引入OpenCV库总结(qmake方式和cmake方式)
  • matlab系列专栏-快捷键速查手册
  • 对于 NestJS + TypeORM 查询构造器分页功能的简单二次封装
  • Kafka消息队列出现消息堆积如何解决
  • LeetCode hot100-100
  • Vue.js:现代前端开发的灵活框架