Markup-based GUI for python - python

I want to get myself into programming some serious GUI based applications, but when I look at things like Swing/SWT from Java, I can't help but HATE programming a GUI interface by creating "widget" objects and populating them and calling methods on them.
I think GUI design should be done in a separate text-based file in some markup format, which is read and rendered (e.g. HTML), so that the design of the interface is not tightly coupled with the rest of the code.
I've seen HTMLayout and I love the idea, but so far it seems be only in C++.
I'm looking for a python library (or even a WIP project) for doing markup-based gui.
UPDATE
The reason I can't accept QT's xml is the same reason I hate the programatic approach; you're assembling each widget separately, and specifying each property of it on a separate line. It doesn't provide any advantage over doing it the programatic way.

You can try Mozilla's XUL. It supports Python via XPCOM.
See this project: pyxpcomext
XUL isn't compiled, it is packaged and loaded at runtime. Firefox and many other great applications use it, but most of them use Javascript for scripting instead of Python. There are one or 2 using Python though.

How about wxPython? I'm just now beginning to work with it, but there's a tool -- XRC Resource Editor -- that allows you to assemble your GUI, which is then written to an XML file. As I understand it, your Python application loads the XML file, rather than having a whole bunch of GUI-layout code mixed in with your Python code.

You should look into Qt, which you can use from Python using the excellent PyQt interface (why they didn't name it QtPy --- cutiepie, get it ? --- I will never understand).
With Qt, you can have the choice of constructing your GUI's programmatically (which you don't want), or using XML markup. This XML file can either be compiled to code beforehand, or loaded with a short command. The latter is the normal way to work using PyQt.
Qt is versatile, high-quality, cross-platform, and you're likely using it already without knowing it. The official Skype client application is written in Qt if I remember correctly.
Edit: Just adding some links so the OP get get some feel for it ...
Short intro to Qt programming with C++ -- notice the use of the Qt Designer, which outputs .ui files, which is an XML format which I remember was also quite easy to work with by hand. PyQt programming is very similar, except for being in a much easier programming language of course :-)
Info about PyQt on the Python wiki
Online book on PyQt programming

If you choose a language like Tcl or Python and Tk for your application development it becomes fairly trivial to write your own DSL for describing the interface. You can, for instance, write a DSL that lets you create menus like this:
menubar {
File => {
Open => cmd.open
Save => cmd.save
Exit => cmd.exit
}
Edit => {
Cut => cmd.cut
Copy => cmd.copy
Paste => cmd.paste
}
}
... and your main GUI forms like this:
form PropertiesForm {
Font: [fontchooser]
Foreground: [foregroundChooser]
Background: [backgroundChooser]
}
form NewUserForm {
username [_____________________]
[] administrator
enable the following features:
() feature 1
() feature 2
() feature 3
}
notebook {
Properties => PropertiesForm
New User => NewUserForm
}
... and so on. Tcl really excels at letting you write DSLs like this. Note that this capability isn't built in to Tcl per se, but the language makes DSLs trivial. Some of this type of thing exists on the Tcler's wiki, for example there's code to create menus similar to what I described at Menus Made Easy.
I think, though, that after a while you'll find it really, really hard to make professional grade UIs in this manner.

If you use GTK, you can use Glade, which is an XML file.

It's XML, not Python, but look at Open Laszlo

windows?
you can use the WinForms editor in Visual Studio and then talk to the assembly from IronPython.

Related

I want to create an extensible app via plugins with C++ and Python

I want to create an app that will be extensible via plugins.
I know that I have 2 options.
I can create my own interpreted language and app with a built-in interpreter for this language.
I can use one of the existing languages such as Python, Lua or another scripting language.
I want to use option 2. And I know that I must create a layer for external language to enable communication between this language and my app. But I don't know how to do it. Maybe I must use interprocess communication or something like that.
Let's assume that I have an application written in C++. In the beginning, it may be even a simple console app that displays a few options. And I want to write a plugin in Python like this:
option = "additional option"
myApp.addOption(option)
And then:
I launch my app
My app loads the plugin
I see my app with this additional option displayed
I want to do this simple thing to understand how it works and then I will be able to do something more complicated.
You could start by looking at the languages' documentation(if you're new):
Python -->https://docs.python.org/3/
Lua --> https://www.lua.org/docs.html
C++ libraries can also be called in C(If you're careful enough),you could look at this too
https://www.teddy.ch/c++_library_in_c/
You should be aware that, with care, a C++ library can be called from a C program, mostly by appropriately using extern "C" to disable name mangling. On Linux, read also the C++ dlopen mini Howto.
Then you need to read the chapter Extending and embedding the Python interpreter
At last, Python is open source, so please study its source code.
I can use one of the existing languages such as Python, Lua or another scripting language.
I strongly recommend considering using GNU Guile or extending Ocaml.
And both TensorFlow or NumPy could inspire you, since they are open source libraries (coded in C and/or C++) usable from Python.

Creating a C++ Qt Gui for a Python logic

I was presented with a Python logic for which I need to create a GUI. I want to use Qt for that purpose and ideally I would like to program it in C++, without using the Qt Creator.
What are recommended ways of combining a C++ Qt GUI and a Python logic, perhaps with a controlling layer which is either in Python or C++?
Due to prior research, I'm aware of certain tools like PySide, PythonQt, SWIG, Shiboken and others (although I haven't used them so far) and that it is very simple to import *.ui files into Python, but the number of possible ways is a bit overwhelming and I could not come up with a "good solution" so far.
Ideally, I would not use Qt Designer, but create the GUI (windows, custom widgets, helper classes etc) by hand, combine them into one or few classes which I expose to a Python/C++ controlling layer (by wrappers? DLL?) which manages the communication between GUI and logic.
How can I achieve this?
A short version of this question might just be: How can I use a Qt C++ GUI with a Python logic?
You can create your GUI application using qt c++ and wrap controler logic written in a python api embedding the python api.
https://docs.python.org/2/extending/embedding.html
But if you have many methods that will be boring to do and you may have some memory leaks if not well written in th c++ side.
Another solution may be using rpc calls to a python api using a webservice (rest api/json rpc ....Etc...). Qt application is client of a python rest api.
like this you split the c code from the python one. You can do that using json rpc calls , or other apis,like jcon (https://github.com/joncol/jcon-cpp).
You will too be able to find python and QT xml rpc api able to talk together.
swig (http://swig.org) may have some capability , but it is better used in the other way : calling c func from python

BIOS like, interactive menu, for python text based game

I am writing a text based game in python and as you'd expect, it has many questions with multiple answers e.g. yes/no.
I have been been using the usual, input your answer, answering technique, but I was hoping to make it a bit more interactive in answering some options, such as the main menu. I do not want a full on GUI but is there some way to have something similar to, for example, the modern BIOS menu, where you use the arrow keys to navigate, and press enter to select?
http://www.washington.edu/lst1/help/computing_fundamentals/troubleshootingxp/img/bios.gif
Thanks for any reply's, whether it's possible or not!
P.S. I would rather not have to download any plugins etc, because I have to be able to use this at school where I cannot use them.
This is a rather open-ended question (not have to download any "plugins")? But what you want is the curses library, to which Python has an interface with its curses module. The Python docs provide a simple tutorial for using it.
There is also a library called Blessings which provides a nicer, more modern object-oriented interface to curses. I haven't tried it but it's well supported and looks pretty good.
Take a look at the Urwid library: http://excess.org/urwid/
(I know you'd like to not use an external module, but that means you'll essentially have to reimplement most of CURSES yourself, and that's a road you really don't want to take)

How to receive input from GUI, apply function, then display?

I wrote a function that receives input as a string, modifies it, and outputs a string. I want to make it so that my non-computer savvy friends can use it, by adding a graphical user interface. I want something very simple: A box where they type in the text, a button that along with hitting "Enter," submits the text, and then a place where it displays the result after my function had modified it. I just want a way to receive input and to write output to a GUI in a way that a regular person can understand. I have no experience with GUIs.
Update
In the end I used XCode to create the GUI and PyObjC to pass the data from GUI to Python in a sort of "frontend-backend" setup.
You may be interested to get a look at http://zetcode.com/
there is a bunch of tutorial about wxPython, PyGTK and PyQt
It should guide you.
Jordi
Does your program have to be python? (as tagged) Your description sounds like a very straightforward task. In that case, have you considered JavaScript + HTML? Your users wouldn't need to download and install anything new, and most people have a good grasp of how to create/use web page forms.
If you're committed to a python implementation: There are a variety of different GUI toolkits (ways of creating the graphical user interface), but which one you choose will depend on how you want your program to look, what operating system you run on, ease of programming, and a variety of factors.
The widgets you describe are fairly simple (text box and button), and you might be able to accomplish your goals using Tk / Tkinter, python's "de facto standard GUI". The advantage of this GUI toolkit is that it's bundled with python on most operating systems, hence (so long as your friends have python installed) they're ready to go. If you don't have experience building complicated installers, then you'll find that users wander off when you require them to install a dozen modules on their own. The TK script would also run on any OS.
Convenience aside, the disadvantage of Tkinter is that Tk is a fairly old and limited framework in the form commonly found with python, and it can be painful to work with for more complicated programs. (some of the online tutorials have typos like mixed case or missing quotation marks) For your task, though, the basic layout and code are pretty simple: see the TkDocs site for a demo that you can adapt.
http://www.tkdocs.com/tutorial/firstexample.html
If you are familier with C# , than you can use IronPython,
just import the Form libraries, make simple form, by putting Lable, Textbox and Button, and refer to that textbox value on Button click. you can get the WOW Gui for Python, with IronPython
Ref : IronPython

Declarative GTK

TL;DR: Is there a library for declarative UI creation using GTK? Preferrably with Python support.
I'm a Python/Django developer, most of my experience about user interfaces is from the web, where declarative, loosely coupled UI designs are standard. Recently I've had to create a GUI app using Java/Swing for a school project and ended up using SwiXML to create declarative Swing layouts for the project.
I asked myself whether there are similar possibilities to use for Python. I'm not very fond of Java, so I won't start developing Swing applications. When looking for Python GUI Toolkits, I ended up concluding that the three main players in this field are GTK, QT and Wx.
Of those 3 I'd prefer GTK, but I could not find any way to create a declarative UI with GTK. From my experience with HTML and SwiXML, I find code based UI creation a huge mess and would prefer not having to do it that way. Declarative UI is much more flexible and more loosly coupled.
Is there a library for declarative UI creation using GTK? Preferrably with Python support.
If there isn't such a thing (which I assume, as I couldn't find anything), I might end up getting started with QT, even though I don't like the default look of it under Linux. (But maybe that's customizable too, in a way so that it looks similar to GTK.)
I think what you're looking for is gtk.Builder. Basically, gtk.Builder objects can be used to load a .ui file that contains xml data that describes the widgets for the user interface and the callbacks to the events that should be exposed by the code. The .ui file can be created with glade so you don't even need to write the xml yourself.
Look at enaml. Although there is no GTK toolkit, it is the only truly declarative framework for python that I'm aware of.

Categories

Resources