
Toga: A Python native, OS native GUI toolkit - samf
http://pybee.org/toga/
======
WoodenChair
This project appears to be quite early stage (many missing APIs, no stable
Windows support, poor documentation, etc) but very promising. If it had
integration with a GUI designer and was feature complete, I'd start using it
on projects.

I've explored the options for Python GUI toolkits quite extensively. They're a
real challenge to get running conveniently (by conveniently, I mean a simple
PIP install away with no external C/C++ library dependencies) on OS X with
native looks, and that is a nice advantage of this one IMHO.

~~~
andreasvc
"No external C/C++ library dependencies" is simply impossible. Given that the
host OS is not implemented in Python, some kind of bridge to its native
libraries is necessary for a GUI library. Such libraries can be either
compiled during the pip installation, or required as an external dependency
installed using the package manager of the OS, Either way it's never going to
be hassle free due to all the different OSes and package managers.

~~~
TazeTSchnitzel
>"No external C/C++ library dependencies" is simply impossible. Given that the
host OS is not implemented in Python, some kind of bridge to its native
libraries is necessary for a GUI library.

Not true. Python provides facilities for dealing with dynamic link libraries
from native Python code. These can be used to interface with the OS directly,
without necessitating writing a C/C++ wrapper.

~~~
andreasvc
OK then it's a terminological confusion, because I would say that gives you a
dependency on said "dynamic link libraries". Whether you need to write a C/C++
wrapper only determines if the dependency is in runtime or also compile time.

~~~
TazeTSchnitzel
You have zero dependencies aside from the OS.

~~~
andreasvc
On Windows or Mac this may be true, but with Linux the GTK or Qt libraries are
not part of the OS, but are used in desktop environments which may or may not
be installed, and there are different version etc. so things are more messy
and it's definitely a dependency which has to be fulfilled (at least at
runtime).

~~~
techdragon
From the conversations I had with the creator of Toga at PyCon AU 2014 and
before its release, this is understood, and the current dev was towards 'it
works on ubuntu', and more will come. The system is extensible so down the
track the issue will be more "which do i pick" not "i can't find a way to
render" ... since nothing stops this using some kind of text mode terminal UI
rendering library, or a raw frame buffer one, its very awesome.

------
malkia
Another toolkit that uses Python, but with Qt (and previously also wxWidgets)
is enaml - it has a cassowary solver, a Visual Studio docking system, and very
interesting approach to connecting that to visual elements.

[https://github.com/nucleic/enaml](https://github.com/nucleic/enaml)

[http://nucleic.github.io/enaml/docs/](http://nucleic.github.io/enaml/docs/)

~~~
drglitch
Having developed several advanced apps in enaml over last 2 years, i commend
enaml. It encourages MVC and provides clean observer-style bindings (Atom).

The framework is used internally by at least one fortune 50 corp for a large
app suite.

------
Camillo
The Mac screenshot I see on the home page looks about as non-native as the
average Qt app.

~~~
ColinCera
It's using native Cocoa controls, so I'm not sure why you don't think it looks
native.

~~~
i_am_ralpht
Because the layout that's presented is not native. Actual mac apps don't have
tabs and toolbars pushed together like that, and the margins are different.

wxWidgets can use the platform native controls, but the apps rarely look
native because the layouts and behaviors are non-native. Mac/GNOME/Windows all
have different ideal metrics for spacings, control placement and labeling
(OK/Cancel vs Cancel/"Do Thing") -- being native means getting all of those
details correct.

~~~
ColinCera
Fair point. The spacing and margins are totally wrong, for sure. But that's
just spacing & margins, and I assume that's easily adjusted (and they should
do that, so their demo doesn't look ugly). But nonetheless, the widgets are
native Cocoa widgets, so they not only look native, they feel and _behave_
native, something you can't say about Qt (at least not to the same degree —
although I develop with Qt, love Qt, and have zero issues with the way Qt
applications look and feel on Mac).

~~~
freakboy3742
The spacing and margins are all manually specified (or, in the case of the
example, _aren 't_ specified at all, so they default to 0). If you cared to do
so, this spacing could be manually adjusted.

Under the hood, it's 100% native widgets (i.e., on OSX, the code is actually
instantiating an NSButton, adding it to an NSView, set as content on an
NSWindow; on Ubuntu, it uses Gtk.Button etc), so the look and feel of the
widgets themselves are exactly as the platform dictates.

~~~
vsl
"100% native widgets" is the easy part and wxWidgets/wxPython is doing just
that (and always have been). Providing higher-level API is harder and again,
wx does that too.

But the devil is in the detail. The reason why many wx apps don't look very
well on OS X is because getting all the tiny details like spacing, behavior,
layout etc. right for all supported platforms, with (mostly) same code base is
_hard_ \- and it has nothing to do with the look of individual widgets.

But well, you'll learn that during the journey ;)

------
rkangel
For a project like this where there are many other potential options (wxPython
etc.) the first question I'm thinking is 'why do I care about this new one'.
I.e. What does this do better/differently/more conveniently. I assume the
answer here us the native Python aspect and not needing big compiled
libraries. Might be worth selling it a bit more on your homepage and making
that advantage clearer?

It may be that everyone else picked up on Python native quicker than I did,
I'm reading this in bed during a lie in so am a bit sleepy!

~~~
freakboy3742
Thanks for the feedback! I cover some of the "why" in the docs, but I agree
the homepage could do a better job of making the case for a new UI toolkit.

The short version:

* System native widgets, not themes

* Installable via "pip install" \- no third party or binary dependencies

* Not just naïve wrappers around widgets - capture the underlying use case and provide an API wrapper.

* Genuinely Python native. This means exploiting language specific features (like generators and context managers)

~~~
_random_
"Python native" is an oxymoron, given the language's interpreted nature.
"Python-idiomatic" would probably be a more correct choice of words. But
that's just me.

~~~
andreasvc
I think that is typically called "pythonic".

------
atanasb
This looks promising. It would be better if the documentation was a bit more
exhaustive[0]

[0][http://toga.readthedocs.org/en/latest/internals/platforms/de...](http://toga.readthedocs.org/en/latest/internals/platforms/desktop/cocoa.html).

~~~
freakboy3742
I'll completely agree. This is a project that has been built in the last 6
months of my spare time from all my other projects, and most of that time has
been spent getting things to a point where they _work_. The APIs aren't yet
stable enough to warrant me spending a bunch of time formalising them, but I
absolutely want to document them as soon as the ground stops moving :-)

------
rjn945
I was really confused when I read "the constrain() call takes _expressions_
that define the relationships you want to impose" (emphasis mine) and saw this
code:

    
    
      container.constrain(button.TOP == container.TOP + 50)
    

Python doesn't let you pass expressions! Why isn't that just getting evaluated
to true or false? Is this not vanilla Python? Is there a pre-processor?

I dove into the code and found the answer here[1]. button.TOP and
container.TOP are both Toga Attributes, which have their equality operators
redefined.

Very interesting and clever use of operator overloading.

[1]
[https://github.com/pybee/toga/blob/master/toga/constraint.py](https://github.com/pybee/toga/blob/master/toga/constraint.py)

~~~
rkangel
I am very conflicted about that trick. On the one hand it is a neat bit of
syntax to express what you want, but it goes completely against your
expectations of how Python syntax is parsed. 'Clever use of operator
overloading' historically has always meant 'confusing use of operator
overloading' \- something the C++ community took some time to learn.

Python is very good at behaving as you expect it to, despite the fact that you
can implement pretty much any magic. This is down to library design more than
it is language design.

~~~
TheLoneWolfling
At least it's less magic than puLP:

    
    
      prob += x*2 + y, "foo"
    

This sets the objective function of prob to (2x+y), and assigns it a name of
"foo".

    
    
      prob += x*2 + y > 3, "abcd"
    

That puts a constraint that 2x + y > 3, called "abcd".

On both of these the string is optional.

I'm also conflicted. On one hand, it's about the most compact syntax you can
get for something like this - when similar libraries in languages without
operator overloading resort to passing strings into functions... On the other
hand, it can be utterly incomprehensible if you haven't gone through the
documentation.

------
davidjhall

      left_container = toga.OptionContainer()
      AttributeError: 'module' object has no attribute 'OptionContainer'
    

Frustrating when things that are supposed to work out of the box, don't.
Initial search yields no help.

~~~
a_bonobo
What version of toga are you running? I'm on Ubuntu with Python 2.7.6, toga v
0.1.0 and cassowary 0.5 and your oneliner works fine.

Edit: In fact, OptionContainer is just 15 days old, so I think you've got an
old version:
[https://github.com/pybee/toga/commit/262efbfc00fae0a96e09666...](https://github.com/pybee/toga/commit/262efbfc00fae0a96e096664dbdb9b409b31413d)

~~~
Lord_DeathMatch
either than or you're on windows, where unfortunately next to nothing is
implemented

------
allegory
Very interesting. They have other tools which are rather useful looking as
well: [http://pybee.org/](http://pybee.org/) (debugger, test runner, coverage
visualiser etc)

~~~
freakboy3742
Thanks for the promotion :-)

Of those tools, the test runner (cricket) is the most mature; the debugger
isn't much more than a proof of concept. Those tools are also based in Tkinter
at the moment; I started Toga because I've started to hit the limits of
Tkinter.

The intention is to port the other PyBee tools to Toga as soon as they get
mature enough.

------
roryokane
Does anyone know of any similar GUI toolkits, for languages other than Python?
That is, cross-platform GUI toolkits that use true OS-native widgets, and
ideally, also abstract away platform-specific details such as the placement of
the Quit/Exit menu items and the order of OK/Cancel buttons. Toga is actually
the first library with those features that I’ve heard of.

~~~
ANTSANTS
I think you're looking for phoenix:
[http://byuu.org/programming/phoenix/](http://byuu.org/programming/phoenix/)

An example application using it, with the GTK backend:
[https://i.imgur.com/pW2Jl9q.png](https://i.imgur.com/pW2Jl9q.png)

and the Qt backend:
[https://i.imgur.com/z6cC3rM.png](https://i.imgur.com/z6cC3rM.png)

The author (byuu) posts here, he might be able to answer questions.

------
calpaterson
The use of constraints to let the user specify where to put things but still
allow enough flexibility to do OS-dependant things is interesting (and
possibly novel?):

[http://toga.readthedocs.org/en/latest/introduction/tutorial-...](http://toga.readthedocs.org/en/latest/introduction/tutorial-0.html)

~~~
daurnimator
Cassowary (
[http://constraints.cs.washington.edu/cassowary/](http://constraints.cs.washington.edu/cassowary/)
) is used by Apple for this.

~~~
freakboy3742
FYI, Toga uses Cassowary as well - natively through Apple's implementation on
OSX and iOS, and via a Python implementation of the algorithm
([http://pybee.org/cassowary/](http://pybee.org/cassowary/)) for GTK and
Windows.

------
ymeshesha
This toolkit seems extremely simple and user-friendly, might have to give it a
try

------
theophrastus
loutish/lazy Debian/"testing" system here:

    
    
       ~> apt-cache search toga
       toga2 - computer chess engine, calculates chess moves
    

(hm... wake me when it hits testing)

~~~
freakboy3742
Given that the project was announced publicly 1 week ago, I wouldn't expect to
see it in Debian (testing or otherwise) for a little while.

------
analog31
Android compatible?

~~~
freakboy3742
Not yet. Android support is on the todo list once I've processed all the
feedback that has come as a result of the initial release.

