How would you design a very “Pythonic” UI framework?

后端 未结 15 2047
日久生厌
日久生厌 2021-02-02 01:29

I have been playing with the Ruby library \"shoes\". Basically you can write a GUI application in the following way:

Shoes.app do
  t = para \"Not clicked!\"
  b         


        
15条回答
  •  滥情空心
    2021-02-02 01:40

    Here's an approach that goes about GUI definitions a bit differently using class-based meta-programming rather than inheritance.

    This is largley Django/SQLAlchemy inspired in that it is heavily based on meta-programming and separates your GUI code from your "code code". I also think it should make heavy use of layout managers like Java does because when you're dropping code, no one wants to constantly tweak pixel alignment. I also think it would be cool if we could have CSS-like properties.

    Here is a rough brainstormed example that will show a column with a label on top, then a text box, then a button to click on the bottom which shows a message.

    from happygui.controls import *
    
    MAIN_WINDOW = Window(width="500px", height="350px",
        my_layout=ColumnLayout(padding="10px",
            my_label=Label(text="What's your name kiddo?", bold=True, align="center"),
            my_edit=EditBox(placeholder=""),
            my_btn=Button(text="CLICK ME!", on_click=Handler('module.file.btn_clicked')),
        ),
    )
    MAIN_WINDOW.show()
    
    def btn_clicked(sender): # could easily be in a handlers.py file
        name = MAIN_WINDOW.my_layout.my_edit.text
        # same thing: name = sender.parent.my_edit.text
        # best practice, immune to structure change: MAIN_WINDOW.find('my_edit').text
        MessageBox("Your name is '%s'" % ()).show(modal=True)
    

    One cool thing to notice is the way you can reference the input of my_edit by saying MAIN_WINDOW.my_layout.my_edit.text. In the declaration for the window, I think it's important to be able to arbitrarily name controls in the function kwargs.

    Here is the same app only using absolute positioning (the controls will appear in different places because we're not using a fancy layout manager):

    from happygui.controls import *
    
    MAIN_WINDOW = Window(width="500px", height="350px",
        my_label=Label(text="What's your name kiddo?", bold=True, align="center", x="10px", y="10px", width="300px", height="100px"),
        my_edit=EditBox(placeholder="", x="10px", y="110px", width="300px", height="100px"),
        my_btn=Button(text="CLICK ME!", on_click=Handler('module.file.btn_clicked'), x="10px", y="210px", width="300px", height="100px"),
    )
    MAIN_WINDOW.show()
    
    def btn_clicked(sender): # could easily be in a handlers.py file
        name = MAIN_WINDOW.my_edit.text
        # same thing: name = sender.parent.my_edit.text
        # best practice, immune to structure change: MAIN_WINDOW.find('my_edit').text
        MessageBox("Your name is '%s'" % ()).show(modal=True)
    

    I'm not entirely sure yet if this is a super great approach, but I definitely think it's on the right path. I don't have time to explore this idea more, but if someone took this up as a project, I would love them.

提交回复
热议问题