Sonntag, 15. Juni 2014

PyQt Rapid GUI Programming with Python and Spyder [Part 1]

When i first started to work with PyQT, i had a hard time finding all the web resources i needed to program my GUI´s.
In the next weeks (or months - time is limited...) i will show you how to get started.

The most important thing, and the hardest thing for me in the beginning was to get the GUI- running in my IDE: The Event Loop of the Main Window

I am working with Python for about 1,5 year now and I like the programming language.
For the examples here I am using Spyder coming with WinPython.

The good thing using Spyder is that it comes with QtDesigner.

Let us get started. Some of the things described here will work in your Python IDE as well (PyCharm, pyzo... )

Designing you gui is pretty simple using the QtDesigner:


i build a simple Main Window here. What i do when i take a new object from the left Object-ribbon in QtDesigner is changing the object name you will later use to access the object in your program.
Buttons emit signals and you have to open slots to functions, so an event like "clicking" can lead to a computation or anything else you can imagine in Python.

When you have created a Window like i did. You Save i in a folder where you have all your Project .py files and things you will need for this project.

A problem people have in the beginning is how to convert this .ui file to a .py file so you can load it up in your IDE (Spyder).

Well there are couple of things you have to be aware of. the easiest way to get the job done is the following:
1. Go to your Winpython installation Main Folder
2. There should be a cmd prompt file you have to open "WinPython Command Prompt"

3. You first have to "cd" to the folder where your .ui (you saved it with QtDesigner) lies.
4. The next command is the following (depends how you named your .ui file - without quotation marks):
"pyuic4 -o Simple_Plot_Window.py Simple_Plot_Window.ui"
5. You .py file should appear in the same folder as the .ui file

Usually " ...\WinPython-64bit-3.3.2.3\python-3.3.2.amd64" is where you should place the pyuic file.
I do not remember if this is the case by default. If not you can search for the file and place it there.

If an error appears you can write a comment under this article.

If you want to run the event loop you have to use the following code:
Save it in a .py file like "gui_simple_plot".After hitting "run" your Gui should appear.
import sys
from PyQt4 import QtGui, QtCore, Qt
from Simple_Plot_Window import Ui_Dialog as Dlg

class MeinDialog(QtGui.QDialog, Dlg):
def __init__(self):
QtGui.QDialog.__init__(self)
self.setupUi(self)


app = QtGui.QApplication(sys.argv)
main = MeinDialog()
main.show()
sys.exit(app.exec_())


At this point obviously the GUI does nothing. In the upcoming posts i will show you, how to build your "Calculation Script" that calls methods to plot math functions using the GUI.


2 Kommentare:

  1. Dieser Kommentar wurde vom Autor entfernt.

    AntwortenLöschen
  2. Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of
    Surya Informatics

    AntwortenLöschen