I am trying to decide on a technology for developing a desktop application that can interface with the serial port. I have looked into python and it looks like a console based app would not be difficult - http://pyserial.sourceforge.net/ , and I have also looked at PyQt, which would fulfill the GUI portion of my project.
But is it possible to include 3rd party modules like PySerial in PyQt?
PyQt and pyserial play nice together (in fact, I'm using them in one of my apps.) The examples on the pyserial website are mostly simple console-like examples, but there's no reason you cannot take data from any of the pyserial objects and use them in PyQt. You could, for instance, take data that you received over a serial port and push it into a QByteArray or NumPy array or anything similar as fits your fancy.
A couple of caveats: if you use Serial.readline(), it is blocking until it gets a newline (\n). This could be bad for your GUI. If you must use readline() instead of read(), I recommend putting your pyserial related activities in a separate thread. How you do this is up to you, but I'd recommend using Qt's built in threading. You can then do data conversions to Qt types and such inside the thread. More info on threading in pyqt.
Edit: almost forgot. If anyone cares, you can use PyQt to write console apps too. Just use QCoreApplication instead of QApplication. Not quite sure why one would do that in python, however, unless you were really fond of Qt's data types... in this case you could use PyQt and pyserial to write a pure console app :D
Related
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
I want to create an GUI application, that would communicate with Asterisk server and provide functions, such as call forwarding, originating calls, etc.
I wanted to use Kivy (Python GUI framework), but there here is so many different tools (AGI, AMI, FastAGI) and libraries (Pyst2, StarPy, etc.) to manage asterisk, that i don't even know where to start.
I have already written some code (using Pyst2 asterisk manager) but I have a feeling, that this is not the best solution, as said application should be able to have multiple instances open simultaneously and AMI would be too messy for that purpose.
Could someone give me some advice or suggestions what tools would be best to use in this case?
You can start from nice book, like ORelly "The future of telephony".
There is no way do something really valuable without know of asterisk dialplan. All that technology used for different things.
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
I'm writing an application that sends files over network, I want to develop a custom protocol to not limit myself in term on feature richness (http wouldn't be appropriate, the nearest thing is the bittorrent protocol maybe).
I've tried with twisted, I've built a good app but there's a bug in twisted that makes my GUI blocking, so I've to switch to another framework/strategy.
What do you suggest? Using raw sockets and using gtk mainloop (there are select-like functions in the toolkit) is too much difficult?
It's viable running two mainloops in different threads?
Asking for suggestions
Disclaimer: I have little experience with network applications.
That being said, the raw sockets isn't terribly difficult to wrap your head around/use, especially if you're not too worried about optimization. That takes more thought, of course. But using GTK and raw sockets should be fairly straightforward. Especially since you've used the twisted framework, which IIRC, just abstracts some of the more nitty-gritty details of socket managing.
Two threads: one for the GUI, one for sending/receiving data. Tkinter would be a perfectly fine toolkit for this. You don't need twisted or any other external libraries or toolkits -- what comes out of the box is sufficient to get the job done.
If your application is somewhat similar to bittorrent, why not check the source code of Deluge http://deluge-torrent.org/ and build from it? It is written in Python, it does use the bittorrent protocol and it does have a GTK user interface.
As an alternative to twisted and whatever GUI library you seem to be using, how about trying PyQt? It provides a GUI and non-blocking sockets all in the same event loop. That way you don't have to worry about interoperability issues, which seem to be the issue you are facing.
Hope this helps!
I have a problem with threads in pygtk. My application consist of a program that downloads pictures off the internet and then displays it with pygtk. The problem is that in order to do this and keep the GUI responsive, I need to use threads.
So I got into a callback after the user clicked on the button "Download pictures" and I call the method to download the pictures that is within that same class.
thread.start_new_thread(self.images_download, (path,pages)
This won't work. The only way I get my program to get into the thread is by using
gtk.threads_init()
Before starting any thread. Now it downloads the pictures but the GUI remains unresponsive.
I googled this and I tried putting gtk.threads_enter and gtk.threads_leave around the threads but it just doesn't work.
Your question is a bit vague, and without a reference to your actual code it's hard to speculate what you're doing wrong.
So I'll give you some pointers to read, then speculate wildly based on experience.
First of all, you seem to think that you can only keep the GUI responsive by using threads. This is not true. You can also write your code asynchronously, and do everything in a single-threaded application. Twisted is built on this programming model. I recently made a blog post that explains how I created an asynchronous task interface, and example runners both for CLI and GTK+. You can look at those examples to see how tasks can be implemented asynchronously, and the UI still gets updated.
Second, if you prefer to use threads for some reason, you will need to understand the GTK+ threading model a little.
You should start by reading The PyGTK FAQ entry on the subject, and you might find this blog post easy to understand too.
Now, on to speculation. I am guessing that you are trying to update your GTK UI from the thread, and not handling the locking properly. If this is the case, you are better off for now deferring all your UI updates you want to do from threads to the main thread by using gobject.idle_add() This way, all UI calls will be made from the main thread. It is an easier mental model to follow in your programming.
Once you feel you really understand the threading and locking models, you could consider updating the UI from your threads, but it's easy to miss a threads_enter()/threads_leave()
You can use gtk.gdk.threads_init() in order to allow any thread modify the UI with the respecting gtk.gdk.threads_enter() and gtk.gdk.theads_leave() lock, but, the problem with this is that doesn't work well on windows. I have tested it on Linux and performs quite well, but I had no luck making this to work over win32.
=== Edit ===
I have been browsing about this, you could make use of the gobject.io_add_watch to check if there is something in your socket, grab it and then update the GUI. check my post about this:
Sockets (and some other files) and PyGTK without threads.