百度360必应搜狗淘宝本站头条
当前位置:网站首页 > IT知识 > 正文

Flet 界面设计1:用Column、Row和Container控件设计“计算器”

liuian 2025-02-27 15:51 7 浏览

为回应网友和粉丝咨询,本期使用 Flet 几个常用的“布局”控件,设计一个漂亮的“计算器”界面,从这个例子中,就可以掌握常用的 界面 设计技巧了。

一个真实的应用,会设计多个界面,各界面有相同的部分,这就需要界面复用了,这个下期再讲。今天主要分享 Column、Row和Container 这3个控件,用他们就足够设计一个漂亮的“计算器”了。下面,循序渐进地设计和优化界面:

循序渐进第一步:不用任何布局控件,直接用最原始的 page.add(),看看是什么样子

import flet as ft

def main(page: ft.Page):
    page.title = "我的计算器" # 定义标题
    result = ft.Text(value="0") # 计算结果存在着“文本”控件中

    # 一口气添加计算器所有按钮
    page.add(
        result,
        ft.ElevatedButton(text="AC"),
        ft.ElevatedButton(text="+/-"),
        ft.ElevatedButton(text="%"),
        ft.ElevatedButton(text="/"),
        ft.ElevatedButton(text="7"),
        ft.ElevatedButton(text="8"),
        ft.ElevatedButton(text="9"),
        ft.ElevatedButton(text="*"),
        ft.ElevatedButton(text="4"),
        ft.ElevatedButton(text="5"),
        ft.ElevatedButton(text="6"),
        ft.ElevatedButton(text="-"),
        ft.ElevatedButton(text="1"),
        ft.ElevatedButton(text="2"),
        ft.ElevatedButton(text="3"),
        ft.ElevatedButton(text="+"),
        ft.ElevatedButton(text="0"),
        ft.ElevatedButton(text="."),
        ft.ElevatedButton(text="="),
    )

ft.app(target=main)

按vscode的F5键,运行看效果如下:

直接用 page.add(),元素都在1列中,界面很丑很不合理,这显然不是我们需要的。但这也说明一个问题:就是 Flet 界面布局,默认是一个 “列”,记住这个特性。

循序渐进第二步:使用“行”布局控件Row,把以上按钮,分布在多个“行”中间。当然,控件Row 也需要 add 到根控件 page 中。Row 控件 用数组容纳子控件,务必用数组 [ ]

import flet as ft

def main(page: ft.Page):
    page.title = "我的计算器" # 定义标题
    result = ft.Text(value="0") # 计算结果存在着“文本”控件中

    # 一口气添加多个 Row 控件
    page.add(
        # Row 控件 用数组容纳子控件,务必用数组 [ ] 
        # 把计算结果单独放在一行中
        ft.Row(controls=[result,]), 
        # 把 AC、+/-、%、/   这4个按钮,放在单独一行中
        ft.Row(controls=[
                ft.ElevatedButton(text="AC"),
                ft.ElevatedButton(text="+/-"),
                ft.ElevatedButton(text="%"),
                ft.ElevatedButton(text="/"),
            ]
        ),
        # 如法炮制,把其他按钮也分组到各自单独的行中,如下:
        ft.Row(
            controls=[
                ft.ElevatedButton(text="7"),
                ft.ElevatedButton(text="8"),
                ft.ElevatedButton(text="9"),
                ft.ElevatedButton(text="*"),
            ]
        ),
        ft.Row(
            controls=[
                ft.ElevatedButton(text="4"),
                ft.ElevatedButton(text="5"),
                ft.ElevatedButton(text="6"),
                ft.ElevatedButton(text="-"),
            ]
        ),
        ft.Row(
            controls=[
                ft.ElevatedButton(text="1"),
                ft.ElevatedButton(text="2"),
                ft.ElevatedButton(text="3"),
                ft.ElevatedButton(text="+"),
            ]
        ),
        ft.Row(
             controls=[
                ft.ElevatedButton(text="0"),
                ft.ElevatedButton(text="."),
                ft.ElevatedButton(text="="),
            ]
        ),
    )

ft.app(target=main)

按vscode的F5键,运行看效果如下:

用 “行”控件Row,对按钮进行分行呈现后,美观多了,但还是不够漂亮。

循序渐进第三步:用 “列”控件Column 和 “容器”控件Container,进一步美化界面。

因为Flet的“容器”控件Container,每个只能装饰一个控件,为了可以整体装饰所有按钮,我们需要把上面的各个行,放在一个“列”控件Column中,然后Container就可以对这个Column进行装饰了。

这里用到了控件的几个通用属性:color 定义颜色,size定义文字大小,bgcolor定义背景色;特别用到了 expand 属性,每个控件expand 值的比例分配容器内的剩余空间,确保容器里面的子控件,自动适应大小。

import flet as ft

def main(page: ft.Page):

    page.title = "我的计算器" # 定义标题
    page.window_height = 380 # 定义窗口尺寸
    page.window_width  = 450

    # 计算结果存在着“文本”控件中。color定义颜色,size定义文本大小
    result = ft.Text(value="0",color='#ffffff',size=20) 

    page.add(
        ft.Container( 
            bgcolor="#000000", # 容器背景色为黑色
            width= 400, # 容器的宽、高
            height= 300,
            border_radius= ft.border_radius.all(20), # 设置容器为圆角
            padding=20, # 设置容器内边距
            # 容器的内容,一个 Column 控件
            content = ft.Column(controls= [ 
                # Column 控件的子元素,多个 Row
                # 把计算结果单独放在一行中
                ft.Row(controls=[result,]), 
                # 把 AC、+/-、%、/   这4个按钮,放在单独一行中
                ft.Row(controls=[
                        # 多个子控件,设置 expand 属性,每个控件expand 值的比例分配容器内的剩余空间
                        ft.ElevatedButton(text="AC", expand=1),
                        ft.ElevatedButton(text="+/-", expand=1),
                        ft.ElevatedButton(text="%", expand=1),
                        ft.ElevatedButton(text="/", expand=1),
                    ]
                ),
                # 如法炮制,把其他按钮也分组到各自单独的行中,如下:
                ft.Row(
                    controls=[
                        ft.ElevatedButton(text="7", expand=1),
                        ft.ElevatedButton(text="8", expand=1),
                        ft.ElevatedButton(text="9", expand=1),
                        ft.ElevatedButton(text="*", expand=1),
                    ]
                ),
                ft.Row(
                    controls=[
                        ft.ElevatedButton(text="4", expand=1),
                        ft.ElevatedButton(text="5", expand=1),
                        ft.ElevatedButton(text="6", expand=1),
                        ft.ElevatedButton(text="-", expand=1),
                    ]
                ),
                ft.Row(
                    controls=[
                        ft.ElevatedButton(text="1", expand=1),
                        ft.ElevatedButton(text="2", expand=1),
                        ft.ElevatedButton(text="3", expand=1),
                        ft.ElevatedButton(text="+", expand=1),
                    ]
                ),
                ft.Row(
                    controls=[
                        ft.ElevatedButton(text="0", expand=1),
                        ft.ElevatedButton(text=".", expand=1),
                        ft.ElevatedButton(text="=", expand=2), # 等号 = 的expand=2,放大一倍
                    ]
                ),
            ]
        )
    ))

ft.app(target=main)

按vscode的F5键,运行看效果如下:

循序渐进第四步:继续用控件的文本、颜色、背景色等属性,进一步美化。我们把 加减乘除等运算符,背景色设为黄色,把数字设置为暗灰色。

import flet as ft

def main(page: ft.Page):

    page.title = "我的计算器" # 定义标题
    page.window_height = 380 # 定义窗口尺寸
    page.window_width  = 450

    # 计算结果存在着“文本”控件中。color定义颜色,size定义文本大小
    result = ft.Text(value="0",color='#ffffff',size=20) 

    page.add(
        ft.Container( 
            bgcolor="#000000", # 容器背景色为黑色
            width= 400, # 容器的宽、高
            height= 300,
            border_radius= ft.border_radius.all(20), # 设置容器为圆角
            padding=20, # 设置容器内边距
            # 容器的内容,一个 Column 控件
            content = ft.Column(controls= [ 
                # Column 控件的子元素,多个 Row
                # 把计算结果单独放在一行中
                ft.Row(controls=[result,]), 
                # 把 AC、+/-、%、/ 这4个按钮,放在单独一行中
                ft.Row(controls=[
                        # 多个子控件,设置 expand 属性,每个控件expand 值的比例分配容器内的剩余空间
                        ft.ElevatedButton(text="AC", expand=1),
                        ft.ElevatedButton(text="+/-", expand=1),
                        ft.ElevatedButton(text="%", expand=1),
                        ft.ElevatedButton(text="/", expand=1, bgcolor="#ffcc00", color="#ffffff"),
                    ]
                ),
                # 如法炮制,把其他按钮也分组到各自单独的行中,如下:
                ft.Row(
                    controls=[
                        ft.ElevatedButton(text="7", expand=1, bgcolor="#999999", color="#ffffff"),
                        ft.ElevatedButton(text="8", expand=1, bgcolor="#999999", color="#ffffff"),
                        ft.ElevatedButton(text="9", expand=1, bgcolor="#999999", color="#ffffff"),
                        ft.ElevatedButton(text="*", expand=1, bgcolor="#ffcc00", color="#ffffff"),
                    ]
                ),
                ft.Row(
                    controls=[
                        ft.ElevatedButton(text="4", expand=1, bgcolor="#999999", color="#ffffff"),
                        ft.ElevatedButton(text="5", expand=1, bgcolor="#999999", color="#ffffff"),
                        ft.ElevatedButton(text="6", expand=1, bgcolor="#999999", color="#ffffff"),
                        ft.ElevatedButton(text="-", expand=1, bgcolor="#ffcc00", color="#ffffff"),
                    ]
                ),
                ft.Row(
                    controls=[
                        ft.ElevatedButton(text="1", expand=1, bgcolor="#999999", color="#ffffff"),
                        ft.ElevatedButton(text="2", expand=1, bgcolor="#999999", color="#ffffff"),
                        ft.ElevatedButton(text="3", expand=1, bgcolor="#999999", color="#ffffff"),
                        ft.ElevatedButton(text="+", expand=1, bgcolor="#ffcc00", color="#ffffff"),
                    ]
                ),
                ft.Row(
                    controls=[
                        ft.ElevatedButton(text="0", expand=1, bgcolor="#999999", color="#ffffff"),
                        ft.ElevatedButton(text=".", expand=1, bgcolor="#999999", color="#ffffff"),
                        # 等号 = 的 expand=2,放大一倍
                        ft.ElevatedButton(text="=", expand=2, bgcolor="#ffcc00", color="#ffffff"), 
                    ]
                ),
            ]
        )
    ))

ft.app(target=main)

按vscode的F5键,运行看效果如下:

至此,计算器界面就算设计完毕了。还算漂亮吧?

不过有个问题:每个按钮,都在单独设置属性,而且代码重复多;那可否重用呢?当然可以。 下期我们分享如何“UI控件复用”,对“我的计算器”进行改造。

#python# #Flet #计算器# #界面设计#

相关推荐

打开新世界,教你用RooCode+Copliot+Mcp打造一个自己的Manus

本文耗时两天打造,想要一遍走通需要花点时间,建议找个专注的时间开搞!这不仅是个免费使用claude3.5的方案,也是一个超级智能体方案,绝对值得一试!最近Manus真是赚足了眼球,然而我还是没有邀请码...

Git仓库(git仓库有哪些)

#Git仓库使用方法流程详解##一、环境搭建与基础配置###1.1安装与初始化-**安装Git**:官网下载安装包,默认配置安装-**配置全局信息**:```bashgitconfig...

idea版的cursor:Windsurf Wave 7(ideawalk)

在企业环境中,VisualStudioCode和JetBrains系列是最常用的开发工具,覆盖了全球绝大多数开发者。这两类IDE各有优势,但JetBrains系列凭借其针对特定语言和企业场景的深度...

Ai 编辑器 Cursor 零基础教程:推箱子小游戏实战演练

最近Ai火的同时,Ai编辑器Cursor同样火了一把。今天我们就白漂一下Cursor,使用免费版本搞一个零基础教程,并实战演练一个“网页版的推箱子小游戏”。通过这篇文章,让你真正了解cursor是什么...

ChatGPT深度集成于苹果Mac软件 编码能力得到提升

【CNMO科技消息】近日,OpenAI发布了针对MacOS的桌面应用程序,并宣布了一系列与各类应用程序的互操作性功能,标志着ChatGPT正在从聊天机器人向AI智能体工具进化。此次发布的MacOS桌面...

日常开发中常用的git操作命令和使用技巧

日常开发中常用的git操作命令,从配置、初始化本地仓库到提交代码的常用git操作命令使用git前的配置刚使用git,先要在电脑上安装好git,接着我们需要配置一下帐户信息:用户名和邮箱。#设置用户名...

Trae IDE 如何与 GitHub 无缝对接?

TraeIDE内置了GitHub集成功能,让开发者可以直接在IDE里管理代码仓库和版本控制。1.直接从GitHub克隆项目如果你想把GitHub上的代码拉到本地,Trae提供了...

China's diplomacy to further provide strong support for country's modernization: FM

BEIJING,March7(Xinhua)--ChineseForeignMinisterWangYisaidFridaythatChina'sdiplomacywil...

三十分钟入门基础Go(Java小子版)(java入门级教程)

前言Go语言定义Go(又称Golang)是Google的RobertGriesemer,RobPike及KenThompson开发的一种静态、强类型、编译型语言。Go语言语法与...

China will definitely take countermeasures in response to arbitrary pressure: FM

BEIJING,March7(Xinhua)--Chinawilldefinitelytakecountermeasuresinresponsetoarbitrarypre...

Go操作etcd(go操作docker实现沙箱)

Go语言操作etcd,这里推荐官方包etcd/clientv3。文档:https://pkg.go.dev/go.etcd.io/etcd/clientv3etcdv3使用gRPC进行远程过程调...

腾讯 Go 性能优化实战(腾讯游戏优化软件)

作者:trumanyan,腾讯CSIG后台开发工程师项目背景网关服务作为统一接入服务,是大部分服务的统一入口。为了避免成功瓶颈,需要对其进行尽可能地优化。因此,特别总结一下golang后台服务...

golang 之JWT实现(golang gin jwt)

什么是JSONWebToken?JSONWebToken(JWT)是一个开放标准(RFC7519),它定义了一种紧凑且自包含的方式,用于在各方之间以JSON方式安全地传输信息。由于此信息是经...

一文看懂 session 和 cookie(session cookie的区别)

-----------cookie大家应该都熟悉,比如说登录某些网站一段时间后,就要求你重新登录;再比如有的同学很喜欢玩爬虫技术,有时候网站就是可以拦截住你的爬虫,这些都和cookie有关。如果...

有望取代 java?GO 语言项目了解一下

GO语言在编程界一直让人又爱又恨,有人说“GO将统治下一个十年”,“几乎所有新的、有趣的东西都是用Go写的”;也有人说它过于死板,使用感太差。国外有Google、AWS、Cloudflar...