Finding Functions Defined in a with: Block

后端 未结 2 1196
无人及你
无人及你 2021-02-01 09:54

Here\'s some code from Richard Jones\' Blog:

with gui.vertical:
    text = gui.label(\'hello!\')
    items = gui.selection([\'one\', \'two\', \'three\'])
    wit         


        
2条回答
  •  猫巷女王i
    2021-02-01 10:33

    To answer your question, yes, it's frame introspection.

    But the syntax I would create to do the same thing is

    with gui.vertical:
        text = gui.label('hello!')
        items = gui.selection(['one', 'two', 'three'])
        @gui.button('click me!')
        class button:
            def on_click():
                text.value = items.value
                text.foreground = red
    

    Here I would implement gui.button as a decorator that returns button instance given some parameters and events (though it appears to me now that button = gui.button('click me!', mybutton_onclick is fine as well).

    I would also leave gui.vertical as it is since it can be implemented without introspection. I'm not sure about its implementation, but it may involve setting gui.direction = gui.VERTICAL so that gui.label() and others use it in computing their coordinates.

    Now when I look at this, I think I'd try the syntax:

        with gui.vertical:
            text = gui.label('hello!')
            items = gui.selection(['one', 'two', 'three'])
    
            @gui.button('click me!')
            def button():
                text.value = items.value
                foreground = red
    

    (the idea being that similarly to how label is made out of text, a button is made out of text and function)

提交回复
热议问题