0

How do I can have access to a widget dimensions (width, height) in flet (python) at runtime or from other widget in the page? I tried to access to the

widget.width

property directly but I believe it doesn't have read permissions.

Rodolfo
  • 96
  • 7

1 Answers1

0

You can set the widget's width/height. Default you can get only the NoneType value.

Example, resize the window's height to change widgets attribute. The widget's value will be moved:

import flet
from flet import Text, Page

def main(page: Page):
    label = Text('Resize window\'s height and value of this widget\'s height will be changed!')
    label.height = label.height = page.window_height * 0.4
    text = Text(label.height)
    page.add(label)
    page.add(text)
    
    def page_resize(e):
        label.height = page.window_height * 0.4
        text.value = label.height
        label.update()
        text.update()

    page.on_resize = page_resize
            
if __name__ == '__main__':
    flet.app(target=main)
LimJ
  • 89
  • 4
  • That is not the problem described. You are setting the label height and make it visible before accessing it. I just want to access the value that it has at runtime, not set it. Try accessing the "label.height" without setting it to "page.window_height * 0.4". It returns "None". – Rodolfo Nov 17 '22 at 11:58
  • "Default you can get only the NoneType value." The page.window_height only for the demonstration for the change of widget's height. – LimJ Nov 18 '22 at 12:27
  • remove the two lines in which you set label.height and try again. The label still has a height, but the property is not accessible. – Rodolfo Nov 18 '22 at 22:56