How would you design a very "Pythonic" UI framework?

I have been playing with the Ruby library "shoes". Basically you can write a GUI application in the following way: do
  t = para "Not clicked!"
  button "The Label" do
    alert "You clicked the button!" # when clicked, make an alert
    t.replace "Clicked!" # ..and replace the label's text

This made me think - how would I design a similarly nice-to-use GUI framework in Python? One that doesn't have the usual tyings of basically being wrappers to a C* library (In the case of GTK, Tk, wx, QT etc etc)

Shoes takes things from web devlopment (like #f0c2f0 style colour notation, CSS layout techniques, like :margin => 10), and from ruby (extensively using blocks in sensible ways)

Python's lack of "rubyish blocks" makes a (metaphorically)-direct port impossible:

def Shoeless(
    self.t = para("Not clicked!")

    def on_click_func(self):
        alert("You clicked the button!")

    b = button("The label", click=self.on_click_func)

No where near as clean, and wouldn't be nearly as flexible, and I'm not even sure if it would be implementable.

Using decorators seems like an interesting way to map blocks of code to a specific action:

class BaseControl:
    def __init__(self):
        self.func = None

    def clicked(self, func):
        self.func = func

    def __call__(self):
        if self.func is not None:

class Button(BaseControl):

class Label(BaseControl):

# The actual applications code (that the end-user would write)
class MyApp:
    ok = Button()
    la = Label()

    def clickeryHappened():
        print "OK Clicked!"

if __name__ == '__main__':
    a = MyApp()
    a.ok() # trigger the clicked action

Basically the decorator function stores the function, then when the action occurred (say, a click) the appropriate function would be executed.

The scope of various stuff (say, the la label in the above example) could be rather complicated, but it seems doable in a fairly neat manner..

Asked by: Kellan640 | Posted: 28-01-2022

Answer 1

You could actually pull this off, but it would require using metaclasses, which are deep magic (there be dragons). If you want an intro to metaclasses, there's a series of articles from IBM which manage to introduce the ideas without melting your brain.

The source code from an ORM like SQLObject might help, too, since it uses this same kind of declarative syntax.

Answered by: Adelaide395 | Posted: 01-03-2022

Answer 2

I was never satisfied with David Mertz's articles at IBM on metaclsses so I recently wrote my own metaclass article. Enjoy.

Answered by: Adelaide925 | Posted: 01-03-2022

Answer 3

This is extremely contrived and not pythonic at all, but here's my attempt at a semi-literal translation using the new "with" statement.

with Shoes():
  t = Para("Not clicked!")
  with Button("The Label"):
    Alert("You clicked the button!")

The hardest part is dealing with the fact that python will not give us anonymous functions with more than one statement in them. To get around that, we could create a list of commands and run through those...

Anyway, here's the backend code I ran this with:

context = None

class Nestable(object):
  def __init__(self,caption=None):
    self.caption = caption
    self.things = []

    global context
    if context:

  def __enter__(self):
    global context
    self.parent = context
    context = self

  def __exit__(self, type, value, traceback):
    global context
    context = self.parent

  def add(self,thing):
    print "Adding a %s to %s" % (thing,self)

  def __str__(self):
    return "%s(%s)" % (self.__class__.__name__, self.caption)

class Shoes(Nestable):

class Button(Nestable):

class Alert(Nestable):

class Para(Nestable):
  def replace(self,caption):

class Command(Nestable):
  def __init__(self, target, command, caption):
    self.command = command  = target

  def __str__(self):
    return "Command(%s text of %s with \"%s\")" % (self.command,, self.caption)

  def execute(self): = self.caption

Answered by: Elise389 | Posted: 01-03-2022

Answer 4

## 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!!!')

    def my_button_clicked(self, button, event):
        self.my_paragraph.text.append('And now you clicked on it, the button that is.')

    def my_paragraph_text_changed(self, text, event):
        self.button.text = 'No more clicks!'

    def my_button_text_changed(self, text, event):

## 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__':

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?

Answered by: Anna813 | Posted: 01-03-2022

Answer 5

With some Metaclass magic to keep the ordering I have the following working. I'm not sure how pythonic it is but it is good fun for creating simple things.

class w(Wndw):
  title='Hello World'
  class txt(Txt):  # either a new class
    text='Insert name here'
  lbl=Lbl(text='Hello') # or an instance
  class greet(Bbt):
    def click(self): #on_click method
      self.frame.lbl.text='Hello %s.'%self.frame.txt.text


Answered by: Edward278 | Posted: 01-03-2022

Answer 6

The only attempt to do this that I know of is Hans Nowak's Wax (which is unfortunately dead).

Answered by: Kimberly294 | Posted: 01-03-2022

Answer 7

The closest you can get to rubyish blocks is the with statement from pep343:

Answered by: Samantha474 | Posted: 01-03-2022

Answer 8

If you use PyGTK with glade and this glade wrapper, then PyGTK actually becomes somewhat pythonic. A little at least.

Basically, you create the GUI layout in Glade. You also specify event callbacks in glade. Then you write a class for your window like this:

class MyWindow(GladeWrapper):
    GladeWrapper.__init__(self, "my_glade_file.xml", "mainWindow")

    def button_click_event (self, *args):

Here, I'm assuming that I have a GTK Button somewhere called button1 and that I specified button_click_event as the clicked callback. The glade wrapper takes a lot of effort out of event mapping.

If I were to design a Pythonic GUI library, I would support something similar, to aid rapid development. The only difference is that I would ensure that the widgets have a more pythonic interface too. The current PyGTK classes seem very C to me, except that I use instead of bar(foo, ...) though I'm not sure exactly what I'd do differently. Probably allow for a Django models style declarative means of specifying widgets and events in code and allowing you to access data though iterators (where it makes sense, eg widget lists perhaps), though I haven't really thought about it.

Answered by: Victoria939 | Posted: 01-03-2022

Answer 9

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.

Answered by: Hailey547 | Posted: 01-03-2022

Answer 10

This could be an oversimplification, i don't think it would be a good idea to try to make a general purpose ui library this way. On the other hand you could use this approach (metaclasses and friends) to simplify the definition of certain classes of user interfaces for an existing ui library and depending of the application that could actually save you a significant amount of time and code lines.

Answered by: Brianna772 | Posted: 01-03-2022

Answer 11

I have this same problem. I wan to to create a wrapper around any GUI toolkit for Python that is easy to use, and inspired by Shoes, but needs to be a OOP approach (against ruby blocks).

More information in:

Anyone's welcome to join the project.

Answered by: Anna565 | Posted: 01-03-2022

Answer 12

If you really want to code UI, you could try to get something similar to django's ORM; sth like this to get a simple help browser:

class MyWindow(Window):
    class VBox:
        entry = Entry()
        bigtext = TextView()

        def on_entry_accepted(text):
            bigtext.value = eval(text).__doc__

The idea would be to interpret some containers (like windows) as simple classes, some containers (like tables, v/hboxes) recognized by object names, and simple widgets as objects.

I dont think one would have to name all containers inside a window, so some shortcuts (like old-style classes being recognized as widgets by names) would be desirable.

About the order of elements: in MyWindow above you don't have to track this (window is conceptually a one-slot container). In other containers you can try to keep track of the order assuming that each widget constructor have access to some global widget list. This is how it is done in django (AFAIK).

Few hacks here, few tweaks there... There are still few things to think of, but I believe it is possible... and usable, as long as you don't build complicated UIs.

However I am pretty happy with PyGTK+Glade. UI is just kind of data for me and it should be treated as data. There's just too much parameters to tweak (like spacing in different places) and it is better to manage that using a GUI tool. Therefore I build my UI in glade, save as xml and parse using

Answered by: Vanessa572 | Posted: 01-03-2022

Answer 13

Personally, I would try to implement JQuery like API in a GUI framework.

class MyWindow(Window):
    contents = (
        para('Hello World!'),
        button('Click Me', id='ok'),

    def __init__(self):
        self['para'].hover(self.blend_in, self.blend_out)

    def message(self):
        print 'You clicked!'

    def blend_in(self, object):
        object.background = '#333333'

    def blend_out(self, object):
        object.background = 'WindowBackground'

Answered by: Rafael904 | Posted: 01-03-2022

Answer 14

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_label=Label(text="What's your name kiddo?", bold=True, align="center"),
        my_btn=Button(text="CLICK ME!", on_click=Handler('module.file.btn_clicked')),

def btn_clicked(sender): # could easily be in a 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"),

def btn_clicked(sender): # could easily be in a 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.

Answered by: Ryan435 | Posted: 01-03-2022

Answer 15

Declarative is not necessarily more (or less) pythonic than functional IMHO. I think a layered approach would be the best (from buttom up):

  1. A native layer that accepts and returns python data types.
  2. A functional dynamic layer.
  3. One or more declarative/object-oriented layers.

Similar to Elixir + SQLAlchemy.

Answered by: Kelvin470 | Posted: 01-03-2022

Similar questions

Why does Ruby have Rails while Python has no central framework?

This is a(n) historical question, not a comparison-between-languages question: This article from 2005 talks about the lack of a single, central framework for Python. For Ruby, this framework is clearly Rails. Why, historically speaking, did this happen for Ruby but not for Python? (or did it happen, and that framework is...

Dilemma: Should I learn Seaside or a Python framework?

I know it's kinda subjective but, if you were to put yourself in my shoes which would you invest the time in learning? I want to write a web app which deals securely with relatively modest amounts of peoples private data, a few thousand records of a few Kb each but stuff that needs to be kept safe, addresses, phone numbers etc. I've done several web projects in PHP/MYSQL and have decided, handy tho...

python - What HTTP framework to use for simple but scalable app?

Closed. This question is opinion-based. It is not c...

java - Emulator Framework

Are there any good open source frameworks for developing computer system emulators? I am particularly interested in something written in Python or Java that can reduce the effort involved in developing emulators for 8-bit processors (e.g. 6502, 6510, etc.).

php - user friendly framework for personal website?

Closed. This question does not meet Stack Overflow guid...

Python IRC client: write from scratch or write plugin for existing framework?

For our company I'd like to have a Python based IRC bot which checks whether the websites of our clients are still up and running. More specific: I want to list a number of URL which should be visited every, say, 15 minutes. If it fails, the URL should be checked again after 5 minutes. If retrieving the URL still doesn't result in an HTTP status code 200, it should echo the failing URL in the channel so we can investigate ...

Python web framework with low barrier to entry

I am looking for a LAMPish/WAMPish experience. Something very transparent. Write the script, hit F5 and see the results. Very little, if any abstraction. SQLAlchemy and (maybe) some simple templating engine will be used. I need simple access to the environment - similar to the PHP way. Something like the COOKIE, SESSION, POST, GET objects. I don't want to write a middleware layer just to get some we...

Python Video Framework

I'm looking for a Python framework that will enable me to play video as well as draw on that video (for labeling purposes). I've tried Pyglet, but this doesn't seem to work particularly well - when drawing on an existing video, there is flicker (even with double buffering and all of that good stuff), and there doesn't seem to be a way to get the frame index in the video during the per-frame callback (only elapsed...

python web framework large project

I need your advices to choose a Python Web Framework for developing a large project: Database (Postgresql)will have at least 500 tables, most of them with a composite primary key, lots of constraints, indexes & queries. About 1,500 views for starting. The project belongs to the financial area. Alwasy new requirements are coming. Will a ORM be helpful?

Chat server with Twisted framework in python can't receive data from flash client

I've develop a chat server using Twisted framework in Python. It works fine with a Telnet client. But when I use my flash client problem appear... (the flash client work find with my old php chat server, I rewrote the server in python to gain performance) The connexion is establish between the flash client and the twisted server: XMLSocket .onConnect return TRUE. So it's not a problem of permission wit...

Still can't find your answer? Check out these communities...

PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python