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

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

    ## All you need is this class:
    
    class MainWindow(Window):
        my_button = Button('Click Me')
        my_paragraph = Text('This is the text you wish to place')
        my_alert = AlertBox('What what what!!!')
    
        @my_button.clicked
        def my_button_clicked(self, button, event):
            self.my_paragraph.text.append('And now you clicked on it, the button that is.')
    
        @my_paragraph.text.changed
        def my_paragraph_text_changed(self, text, event):
            self.button.text = 'No more clicks!'
    
        @my_button.text.changed
        def my_button_text_changed(self, text, event):
            self.my_alert.show()
    
    
    ## The Style class is automatically gnerated by the framework
    ## but you can override it by defining it in the class:
    ##
    ##      class MainWindow(Window):
    ##          class Style:
    ##              my_blah = {'style-info': 'value'}
    ##
    ## or like you see below:
    
    class Style:
        my_button = {
            'background-color': '#ccc',
            'font-size': '14px'}
        my_paragraph = {
            'background-color': '#fff',
            'color': '#000',
            'font-size': '14px',
            'border': '1px solid black',
            'border-radius': '3px'}
    
    MainWindow.Style = Style
    
    ## The layout class is automatically generated
    ## by the framework but you can override it by defining it
    ## in the class, same as the Style class above, or by
    ## defining it like this:
    
    class MainLayout(Layout):
        def __init__(self, style):
            # It takes the custom or automatically generated style class upon instantiation
            style.window.pack(HBox().pack(style.my_paragraph, style.my_button))
    
    MainWindow.Layout = MainLayout
    
    if __name__ == '__main__':
        run(App(main=MainWindow))
    

    It would be relatively easy to do in python with a bit of that metaclass python magic know how. Which I have. And a knowledge of PyGTK. Which I also have. Gets ideas?

提交回复
热议问题