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

后端 未结 15 1801
日久生厌
日久生厌 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:44

    Maybe not as slick as the Ruby version, but how about something like this:

    from Boots import App, Para, Button, alert
    
    def Shoeless(App):
        t = Para(text = 'Not Clicked')
        b = Button(label = 'The label')
    
        def on_b_clicked(self):
            alert('You clicked the button!')
            self.t.text = 'Clicked!'
    

    Like Justin said, to implement this you would need to use a custom metaclass on class App, and a bunch of properties on Para and Button. This actually wouldn't be too hard.

    The problem you run into next is: how do you keep track of the order that things appear in the class definition? In Python 2.x, there is no way to know if t should be above b or the other way around, since you receive the contents of the class definition as a python dict.

    However, in Python 3.0 metaclasses are being changed in a couple of (minor) ways. One of them is the __prepare__ method, which allows you to supply your own custom dictionary-like object to be used instead -- this means you'll be able to track the order in which items are defined, and position them accordingly in the window.

提交回复
热议问题