[Python] Why is the width and height being ignored at runtime?

Discussão em 'Python' iniciado por Stack, Março 29, 2025.

  1. Stack

    Stack Membro Participativo

    I have installed flet on my windows PC and I'm using Pycharm as my editor. When I run the python script in both the editor or using CMD, the width and height defined in the code below won't work. I have set my environment variables and I'm using the latest version of flet as of this writing. Here's a screenshot of what it looks like when I run it. Also, even though I have defined the window to not be resizable, I can still adjust the size of the window. I don't understand why those lines of code are being ignored at runtime. Any help would be much appreciated.

    [​IMG]

    import flet as ft


    def main(page: ft.Page):
    # Set window properties
    page.title = "My Flet App"
    page.window_width = 400 # Width in pixels
    page.window_height = 700 # Height in pixels
    page.window_resizable = False # Fix the window size
    #page.bgcolor = ft.Colors.WHITE # Use the updated Colors enum
    page.update() # Apply settings immediately

    # --- Define pages (content for each page) ---
    def home_page():
    return ft.Container(
    content=ft.Text("Home Page Content", size=20),
    alignment=ft.alignment.center,
    expand=True,
    )

    def daily_agenda_planner():
    return ft.Container(
    content=ft.Text("Daily Agenda Planner Content", size=20),
    alignment=ft.alignment.center,
    expand=True,
    )

    # Dictionary holding our pages
    frames = {
    "HomePage": home_page(),
    "DailyAgendaPlanner": daily_agenda_planner(),
    }

    # --- Current Page Container ---
    current_page = ft.Container(content=frames["HomePage"], expand=True)

    # Function to switch between pages
    def show_frame(page_name: str):
    current_page.content = frames[page_name]
    page.update()

    # --- Navigation Bar ---
    def create_nav_button(icon, tooltip, action):
    return ft.IconButton(
    icon=icon,
    icon_size=30,
    tooltip=tooltip,
    on_click=action,
    )

    # Navigation button actions
    def on_home_click(e):
    show_frame("HomePage")

    def on_planner_click(e):
    show_frame("DailyAgendaPlanner")

    def on_contacts_click(e):
    print("Contacts button clicked!")

    def on_map_click(e):
    print("Nav button clicked!")

    def on_refresh_click(e):
    print("Refresh button clicked!")

    # Create nav bar
    nav_bar = ft.Row(
    controls=[
    create_nav_button(ft.Icons.HOME, "Home", on_home_click),
    create_nav_button(ft.Icons.CALENDAR_TODAY, "Planner", on_planner_click),
    create_nav_button(ft.Icons.CONTACTS, "Contacts", on_contacts_click),
    create_nav_button(ft.Icons.MAP_ROUNDED, "Map", on_map_click),
    create_nav_button(ft.Icons.REFRESH, "Refresh", on_refresh_click),
    ],
    alignment=ft.MainAxisAlignment.CENTER,
    spacing=20,
    )

    # Wrap nav bar in a container for padding
    nav_bar_container = ft.Container(
    content=nav_bar,
    padding=10,
    )

    # --- Main Layout ---
    main_layout = ft.Column(
    controls=[
    current_page, # Expands to fill available space.
    nav_bar_container, # Navigation bar at the bottom.
    ],
    expand=True,
    alignment=ft.MainAxisAlignment.SPACE_BETWEEN,
    )

    page.add(main_layout)

    ft.app(target=main)

    Continue reading...

Compartilhe esta Página