RustGUI学习(iced/iced_aw)之扩展小部件(十七):如何使用colorpicker部件来选择颜色?

注:此文适合于对rust有一些了解的朋友
iced是一个跨平台的GUI库,用于为rust语言程序构建UI界面。
在这里插入图片描述
前言:
本系列是iced的小部件应用介绍系列,主要介绍iced、iced_aw两个库中涉及的各种小部件的使用及实例演示。

系列博客链接
1、RustGUI学习(iced)之小部件(一):如何使用按钮button和文本标签text部件?

2、RustGUI学习(iced)之小部件(二):如何使用滑动条slider部件?

3、RustGUI学习(iced)之小部件(三):如何使用下拉列表pick_list?

4、RustGUI学习(iced)之小部件(四):如何使用

  • 7
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,以下是用 Rust 语言Iced 库实现的简单计算器: ```rust use iced::{button, Button, Column, Element, Sandbox, Settings, Text}; pub struct Calculator { first_operand: String, second_operand: String, operator: Operator, result: String, calculate_button_state: button::State, } #[derive(Debug, Clone, Copy)] pub enum Operator { Add, Subtract, Multiply, Divide, } impl Default for Calculator { fn default() -> Self { Self { first_operand: String::new(), second_operand: String::new(), operator: Operator::Add, result: String::new(), calculate_button_state: button::State::new(), } } } #[derive(Debug, Clone)] pub enum Message { FirstOperandChanged(String), SecondOperandChanged(String), OperatorChanged(Operator), CalculateButtonPressed, } impl Sandbox for Calculator { type Message = Message; fn new() -> Self { Self::default() } fn title(&self) -> String { String::from("Calculator") } fn update(&mut self, message: Message) { match message { Message::FirstOperandChanged(value) => { self.first_operand = value; } Message::SecondOperandChanged(value) => { self.second_operand = value; } Message::OperatorChanged(operator) => { self.operator = operator; } Message::CalculateButtonPressed => { let first_operand = self.first_operand.parse::<f32>().unwrap_or_default(); let second_operand = self.second_operand.parse::<f32>().unwrap_or_default(); let result = match self.operator { Operator::Add => first_operand + second_operand, Operator::Subtract => first_operand - second_operand, Operator::Multiply => first_operand * second_operand, Operator::Divide => first_operand / second_operand, }; self.result = result.to_string(); } } } fn view(&mut self) -> Element<Message> { let first_operand_input = Text::new("First operand:").size(20); let second_operand_input = Text::new("Second operand:").size(20); let calculate_button = Button::new( &mut self.calculate_button_state, Text::new("Calculate").size(20), ) .on_press(Message::CalculateButtonPressed); let result_text = Text::new(&self.result).size(20); Column::new() .push(first_operand_input) .push( Text::new(&self.first_operand) .size(20) .padding(10) .width(iced::Length::Units(200)) .border_width(1) .border_radius(5), ) .push(second_operand_input) .push( Text::new(&self.second_operand) .size(20) .padding(10) .width(iced::Length::Units(200)) .border_width(1) .border_radius(5), ) .push( Column::new() .push( Button::new( &mut button::State::new(), Text::new("Add").size(20), ) .on_press(Message::OperatorChanged(Operator::Add)), ) .push( Button::new( &mut button::State::new(), Text::new("Subtract").size(20), ) .on_press(Message::OperatorChanged(Operator::Subtract)), ) .push( Button::new( &mut button::State::new(), Text::new("Multiply").size(20), ) .on_press(Message::OperatorChanged(Operator::Multiply)), ) .push( Button::new( &mut button::State::new(), Text::new("Divide").size(20), ) .on_press(Message::OperatorChanged(Operator::Divide)), ), ) .push(calculate_button) .push(result_text) .spacing(20) .padding(20) .into() } } fn main() -> iced::Result { Calculator::run(Settings::default()) } ``` 这个计算器允许用户输入两个操作数和选择一个操作符,然后计算结果。请在 Rust 环境中运行这个代码,然后使用键盘或鼠标输入操作数和选择操作符,最后按下“Calculate”按钮计算结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机构师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值