New-style Signal and Slot Support, PyQt - python-3.x

I'm writing a GUI application with PyQt4 (Python3). One my friend pointed out that using pyuic4 is a bad practice and referred me to uic module and Connecting Slots By Name features. He didn't have time to explain more and the references I have are rather short, I couldn't grasp the idea from them (uic module, LoadingUIFilesAtRuntime, connecting slots by name).
On StackOverflow there is at least one related question but the links to the literature are broken there.
I could follow standard tutorials and did simple GUI using pyuic, but now feel a little bit confused... Any good examples and/or references are welcome.

Firstly, using pyuic4 is certainly not "bad practice".
There are three main ways to get PyQt4 UI's into your code:
Write it all by hand yourself
Use pyuic4 to auto-generate a python module that can be imported
Use the uic package to load ui files directly at runtime
Of these, the first two are by far the most common, and most documentation, tutorials, advice, etc that you will come across will use those methods.
A good source for PyQt4 tutorials can be found in this section of the PyQt4 Wiki. However, I should probably point out that, although still relevant, many of them are quite old and so still use the old-style signals and slots.
However, the difference between the old- and new- styles is not that difficult to understand, so maybe a simple example is all that's needed.
Here's the old-style way to connect a button-click signal to a handler method (aka slot):
self.connect(self.button, QtCore.SIGNAL('clicked()'), self.handleButtonClick)
and here's the new-style way:
self.button.clicked(self.handleButtonClick)
As you can see, the new-style is much simpler and more pythonic. On the other hand, the old-style is quite similar to how signals are connected using C++ (and for this reason can still be useful in certain circumstances).
If you have problems with connecting signals when writing your GUIs, you can always ask a question here - but it's much easier to get good answers if you ask specific questions that include example code.

Related

How to integrate Pyglet with Twisted/Asyncio?

How can I integrate a Python-Pyglet game with either Twisted or Asyncio?
I was going to ask a more general question, like "How can I add networking to a Python-Pyglet game?", but when doing any kind of web-search, "all roads lead to Twisted", anyway. However, both Pyglet and Twisted have their own event-loop, and it's not clear how they can be integrated. None of the other answers I've found, have more detail than "use Twisted", either.
Twisted does have a list of libraries it integrates with, including GUI ones like WxPython, but Pyglet is not on the list. I read one article that said Pyglet was going to be integrated with Twisted - from 2008, so I'm not holding my breath.
There is a pyglet-twisted library, but it's five years old on Github. There's no version information, but the sample-code has Python-2 print statements, so I'm guessing it's out-of-date. Using Pyglet and Twisted together
Same for Asyncio, which I also looked at - it has its own event-loop. Although Pyglet docs do have some information about doing a custom event-loop, I'm not sure how well it plays with others. I'd actually prefer using Asyncio, if it's feasible to get them to play nicely together.
You could get a quick win by basing code off of pigtwist and convert Py2 to Py3 syntax. Which doesn't look like a whole lot. There's also the crochet package that runs twisted in it's own thread, which you could run the network stack on. I also found this example, which uses a coiterator approach.
Hopefully pyglet will incorperate asyncio into itself given that it's a pure Python library. Maybe file a ticket with the team?

Anjuta/Glade Tutorials or Better IDE?

I am attempting to develop a GUI application for Tails. I'm doing the initial development on Debian 8 since development directly in Tails can be a pain.
I started out using Anjuta, but the documentation is essentially non-existent. The Anjuta website has nothing at all about how Glade is integrated or how to use it. I can't even track down documentation on how to change the main window title. The only tutorial I found has you start a project and build it using the default files that are generated for a GTKmm project.
Is there a good book or online tutorial out there for doing GUI development in Anjuta?
This is maybe not a complete answer, but it's too large to put in as a comment. I use Anjuta fairly regularly, but I share your feeling about the missing documentation (which is, by the way, not unique for Anjuta). I appreciate Anjuta (and Glade) very much, so don't take the following as criticisms on either program.
I would recommend you consider using PyGTK for GUI creation. It is a lot more productive. You can design the GUI in Glade - exactly the same way you would do for C/C++ - and then implement the code in Python, which you can also edit and manage from Anjuta. There are plenty of code examples, for example on the nullege code search engine.
About the work flow in Anjuta (for C/C++). It is based mainly on the Autotools system, so you should really read up a little on make, Makefile, and related tools. Though in principle Anjuta manages this, you will, sooner or later hit a problem, and some knowledge about Autotools will help you a long way (also this tutorial or this one. This slide series is interesting - probably because it is more graphical. There are even some video tutorials, like this one.).
There is no real necessity to use Glade from inside Anjuta. In fact, Glade has passed a long process distancing itself from 'code generation'. It now only contains an XML generator, which can be called separately. I find the screen space left for Glade inside Anjuta insufficient for comfortable work anyway.
So, in conclusion: If you mainly need a GUI, consider Python + Gtk. If you do need C or C++, Anjuta is a great IDE, but look at Gtk Development examples (like this one). Following those, the use of Anjuta should be a lot clearer.
EDIT:
Very useful answer. I have some underlying legacy code that has to be
C++. Is there a way to mix Python and C++ in Anjuta, or do you know of
any guideposts or tutorials for such?
You can open a C++ project in Anjuta - maybe even import you legacy code directly as a Makefile project. You can also add new files to your C/C++ project and create them as Python files. I've never tried to do that though, and I'm not sure how Anjuta would treat them, for example, in the Makefile(s). I don't have large projects mixing languages at the moment, but for small projects, I like 'Geany', because it doesn't get in the way. You do have to maintain the Makefiles manually.

Contributing to unmaintained reactor in Twisted or maintain separate implementation?

Future Edit
As of twisted 15.5.0 and kivy 1.9.1, the kivy implementation works on python3. I also use AMP, which is not yet in the release but was merged 2 months ago and will be in the next twisted release.
Introduction
I'm developing an app using Kivy and plan to extend it using multi-processing and, more importantly Twisted. The app has a master component and a slave which should be controllable from various masters, including over a network (hence twisted).
Kivy has it's own twisted support code (in kivy/support.py called instal_twisted_reactor) which works in python2 but doesn't in python3 due to the unported status of the _threadedselect reactor.
I've hacked a port of twisted/internet/_threadedselect.py to python3 which is sufficient to run the kivy example code (one server GUI and one client GUI sending text). Normally I'd then go for a pull request upstream, but there's a few more factors in play:-
_threadedselect.py is, as best as I can tell, unmaintained in twisted, and in fact was last even mentioned on their mailing list back in 2006 (as a candidate for removal)
The twisted requirements for python3 porting include appropriate unit tests, and I don't have to knowledge (networking wise especially) to write appropriate unit tests for this reactor
I do not know whether any other project (besides kivy) even uses _threadedselect.py
Main Question
Given the above, is it advisable to put in the effort to get _treadedselect.py patched upstream (and henced ported to python3)? Or would it be more efficient to maintain a separate implementation in my own project (or optionally contributed to kivy's project).
Disclaimer: I'm (obviously) not a maintainer for either project
Non-essential details:
Kivy isn't the most popular UI base in python, hence it doesn't have a twisted maintained reactor (a la GTK, QT, TK etc.), but its the best fit for what I want to do. I've considered tornado etc. but that's really a matter for another question.
Also, the modifications needed are (so far) really minor. queue to Queue, zope interface decorators, except using as rather than comma, and using the function next rather than using a generator's next method.
Thank you for your interest in contributing to the Twisted ecosystem. I hope I can clear up your confusion so you can move forward.
First of all, Kivy is a very important Python library, and one we at the Twisted project definitely care about (I'm super happy to hear you're using them together!) so you are mistaken if you believe that Kivy doesn't have a reactor simply because it "isn't the most popular UI base in python"; the only reason it doesn't have a reactor in Twisted is that nobody contributed one.
There are a couple of possible ways forward, but they depend on some technical details you haven't covered in your question, and one SO answer is probably not enough to resolve the conflict; you will probably want to join the Twisted mailing list and discuss this there.
Regardless of the Python 3 port, _threadedselect is a private API, with no compatibility contract, and Kivy should not be using it. So in order to move forward, this will need to change. However, it is unlikely to be removed, as it is internally the basis of the wxpython reactor, which is a public API distributed with Twisted, and will eventually need to be ported to python 3 itself to complete the port.
The first way forward would be to improve _threadedselect's test coverage and make it a public API again. This strikes me as not necessarily the best move, because getting the thread interaction correct is very tricky and I would not want to encourage people - even people dealing with low-level enough implementation details to want to implement a reactor - to do it that way if any other options were available. However, if _threadedselect had a valid use-case within Kivy, and an advocate willing to do some maintenance work, this is definitely plausible.
The second way forward would be to create a reactor based on an internal socket-monitoring API within Kivy. Is there one? Most of the UIs which Kivy is a wrapper over (CoreFoundation, X11, Windows) do have socket-monitoring of some kind built in. Can Kivy tap into that, possibly? This is less error-prone than threading, if you can manage it, but sometimes it's impossible; I don't see any directly exposed APIs in Kivy for this. If you could develop such a thing it could be within Twisted or within Kivy depending on your preference.
I hope to hear from you in a more discussion-oriented forum soon :)

Haskell `ncurses` library

I would like to use a text-based UI in my Haskell program. I found some bindings for the ncurses library (see also hscurses or ncurses, which one to use?). The hscurses and nanocurses packages are just simple wrappers around the C library, while vty isn't very well documented and a bit ugly (for example mixing snake_case and CamelCase).
The ncurses library on Hackage looks much more pretty and provides API which nicely fits Haskell. The problem is that it doesn't seem to implement some crucial features, like resizing or refreshing the windows.
So my question is:
is there any other Haskell text UI library, either ncurses-based or not, which I missed?
if there isn't anyone, is it possible to extend the ncurses Haskell library to at least support window refreshing and resizing? (this should be probably consulted with the project owner, but I need the solution quickly)
EDIT:
I finally used nscurses without windows (and panels) to avoid the troubles with refreshing them. I had problems with output to bottom-right corner of a window (a very similar issue was reported for Python's ncurses binding). I solved it by not writing there :).
Have you looked at vty-ui? It has a very nice user manual with lots of examples. I believe it's essentially a wrapper around vty.
I've used nanoncurses and hscurses succesfully, my hmp3 app has a binding that was the basis for nanocurses.
No matter what you probably will want a nice high level API. hscurses does have a box abstraction at least.
You'd be fine going with hscurses.
There is another good choice for Text-based user interfaces in haskell;
Brick is written by jtdaugherty, the same person that developed vty-ui which is Deprecated now.
The API is Declarative, which is Better for Presenting a language like Haskell.
also the Documentation was great and complete.

The right language for OpenGL UI prototyping. Ditching Python

So, I got this idea that I'd try to prototype an experimental user interface using OpenGL and some physics. I know little about either of the topics, but am pretty experienced with programming languages such as C++, Java and C#. After some initial research, I decided on using Python (with Eclipse/PyDev) and Qt, both new to me, and now have four different topics to learn more or less simultaneously.
I've gotten quite far with both OpenGL and Python, but while Python and its ecosystem initially seemed perfect for the task, I've now discovered some serious drawbacks. Bad API documentation and lacking code completion (due to dynamic typing), having to import every module I use in every other module gets tedious when having one class per module, having to select the correct module to run the program, and having to wait 30 seconds for the program to start and obscure the IDE before being notified of many obvious typos and other mistakes. It gets really annoying really fast. Quite frankly, i don't get what all the fuzz is about. Lambda functions, list comprehensions etc. are nice and all, but there's certainly more important things.
So, unless anyone can resolve at least some of these annoyances, Python is out. C++ is out as well, for obvious reasons, and C# is out, mainly for lack of portability. This leaves Java and JOGL as an attractive option, but I'm also curious about Ruby and Groovy. I'd like your opinion on these and others though, to keep my from making the same mistake again.
The requirements are:
Keeping the hell out of my way.
Good code completion. Complete method signatures, including data types and parameter names.
Good OpenGL support.
Qt support is preferable.
Object Oriented
Suitable for RAD, prototyping
Cross-platform
Preferably Open-Source, but at least free.
It seems you aren't mainly having a problem with Python itself, but instead with the IDE.
"Bad API documentation"
To what API? Python itself, Qt or some other library you are using?
"lacking code completion (due to dynamic typing)"
As long as you are not doing anything magic, I find that PyDev is pretty darn good at figuring these things out. If it gets lost, you can always typehint by doing:
assert isinstance(myObj, MyClass)
Then, PyDev will provide you with code completion even if myObj comes from a dynamic context.
"having to import every module I use in every other module gets tedious when having one class per module"
Install PyDev Extensions, it has auto-import on the fly. Or collect all your imports in a separate module and do:
from mymodulewithallimports import *
"having to select the correct module to run the program"
In Eclipse, you can set up a default startup file, or just check "use last run configuration". Then you never have to select it again.
"before being notified of many obvious typos and other mistakes"
Install PyDev Extensions, it has more advanced syntax checking and will happily notify you about unused imports/variables, uninitialized variables etc.
Looking just at your list I'd recommend C++; especially because Code Completion is so important to you.
About Python: Although I have few experience with OpenGL programming with Python (used C++ for that), the Python community offers a number of interesting modules for OpenGL development: pyopengl, pyglew, pygpu; just to name a few.
BTW, your import issue can be resolved easily by importing the modules in the __init__.py files of the directory the modules are contained in and then just importing the "parent" module. This is not recommended but nonetheless possible.
I don't understand why nobody has heard of the D programing language?
THIS IS THE PERFECT SOLUTION!!!!
The only real alternative if you desire all those things is to use Java, but honestly you're being a bit picky about features. Is code completion really that important a trait? Everything else you've listed is traditionally very well regarded with Python, so I don't see the issue.
The text editor (not even an IDE) which I use lets you import API function definitions. Code completion is not a language feature, especially with OpenGL. Just type gl[Ctrl+I] and you'd get the options.
I tried using Java3D and java once. I realized Java3D is a typical Java API... lots of objects to do simple things, and because it's Java, that translates to a lot of code. I then moved to Jython in Eclipse to which cleaned up the code, leaving me with only the complexity of Java3D.
So in the end, I went in the opposite direction. One advantage this has over pure python is I can use Java with all of Eclipse's benefits like autocomplete and move it over to python when parts get unwieldy in Java.
It seems like Pydev can offer code completion for you in Eclipse.
I started off doing OpenGL programming with GL4Java, which got migrated to JOGL and you should definately give it (JOGL) a try. Java offers most of the features you require (plus Eclipse gives you the code completion) and especially for JOGL there are a lot of tutorials out there to get you started.
Consider Boo -- it has many of Python's advantages while adopting features from elsewhere as well, and its compile-time type inference (when variables are neither explicitly given a specific type or explicitly duck typed) allows the kind of autocompletion support you're asking about.
The Tao.OpenGL library exposes OpenGL to .NET apps (such as those Boo compiles), with explicit support for Mono.
(Personally, I'm mostly a Python developer when not doing C or Java, but couldn't care less about autocompletion... but hey, it's your question; also, the one-class-per-module convention seems like a ridiculous amount of pain you're putting yourself through needlessly).

Resources