Windows平台下如何修改Flet窗口大小并设置为不可调整?
修改Flet应用窗口大小并设置不可调整(Windows系统)
在Flet中,你可以直接通过操作page对象的窗口属性来实现固定窗口尺寸、禁用用户调整的需求,具体实现如下:
核心属性说明
page.window_width/page.window_height:直接设置窗口的固定宽度与高度page.window_resizable:将该属性设为False,即可禁止用户拖动窗口边缘调整大小
代码示例(基于计算器场景修改)
import flet as ft def main(page: ft.Page): # 基础窗口配置 page.title = "Flet计算器" # 设置固定窗口大小 page.window_width = 340 page.window_height = 460 # 禁用窗口大小调整功能 page.window_resizable = False # 计算器核心逻辑(示例) result_display = ft.Text(value="0", text_align=ft.TextAlign.RIGHT, size=28) def on_button_click(e): btn_text = e.control.text if btn_text == "=": try: result_display.value = str(eval(result_display.value)) except: result_display.value = "错误" elif btn_text == "C": result_display.value = "0" else: result_display.value = result_display.value + btn_text if result_display.value != "0" else btn_text page.update() # 构建计算器按钮布局 button_rows = [ ["7", "8", "9", "/"], ["4", "5", "6", "*"], ["1", "2", "3", "-"], ["0", ".", "=", "+"], ["C"] ] for row in button_rows: page.add(ft.Row( controls=[ft.ElevatedButton(text=txt, on_click=on_button_click, expand=True) for txt in row], expand=True )) page.add(result_display) page.update() if __name__ == "__main__": ft.app(target=main)
注意事项
- 窗口属性的修改需要在
page.update()前完成,或者修改后调用page.update()让配置生效 - 示例中的宽高数值可根据你的计算器UI布局自行调整
内容的提问来源于stack exchange,提问作者Crapicus
相关产品推荐
相关产品推荐

