TwitterFacebook

PySide – Introduction – part 004

Today I will show you how to deal with events over PySide interfaces.
The main example will be the round button – QDial named roiDial and one QLCDNumber named show_lcd.
The connection come with : valueChanged.connect
This will make all changes from roiDial and show_lcd.
If you want to deal with QDial and QLCDNumber then decorate every instance of a class-based.
Let’s see how working this example:

#import python modules 
import sys
import PySide
from PySide.QtGui import *
from PySide.QtCore import *

#make MyWidget class from QWidget
class MyWidget(QWidget):

#make default __init__
  def __init__(self):
#take __init__ from QWidget class
    QWidget.__init__(self)


#make roiDial from Qdial class with all definitions     
    self.roiDial = QDial()
    self.roiDial.setNotchesVisible(True)
    self.roiDial.setMaximum(15)
    self.roiDial.setMinimum(1)
    self.roiDial.setValue(1)

#make the Grid and add roiDial 
    self.myGridLayout = QGridLayout()
    self.myGridLayout.addWidget(self.roiDial, 0, 0)

#make show_lcd from QLCDNumber
    self.show_lcd = QLCDNumber()

# THIS - will coonnect the named roiDial with show_lcd
    self.roiDial.valueChanged.connect(self.show_lcd.display)

#add my show_lcd 
    self.myGridLayout.addWidget(self.show_lcd)

#make Layout and set all with my grid - myGridLayout
    self.setLayout(self.myGridLayout)

#add one title for this window ...
    self.setWindowTitle("... connect !")

#start the python script 
if __name__ =='__main__':

# make one simple example with exception handling.
# need to use the try - except - finally block.

    try:
        myApp = QApplication(sys.argv)
        myWidget = MyWidget()
        myWidget.show()
        myApp.exec_()
        sys.exit(0)
    except NameError:
        print("Name Error:", sys.exc_info()[1])
    except SystemExit:
        print("Closing Window...")
    except Exception:
        print(sys.exc_info()[1])
    finally:
        print(" - the next tutorial - ")

QDial_free-tutorials_004

PySide – Introduction – part 003

I make another tutorial named – PySide – Introduction – part 003.
The main reason come from my opption to share some basic actions and fix some issue when you try to learn PySide and all python submodules.
This is the default script:


import sys
import PySide
from PySide.QtGui import *
from PySide.QtCore import *

class MyWidget(QWidget):
  def __init__(self):
    QWidget.__init__(self)
    
    self.roiDial = QDial()
    self.roiDial.setNotchesVisible(True)
    self.roiDial.setMaximum(15)
    self.roiDial.setMinimum(1)
    self.roiDial.setValue(1)
 
    self.myGridLayout = QGridLayout()
    self.myGridLayout.addWidget(self.roiDial, 0, 0)

    self.setLayout(self.myGridLayout)
    self.setWindowTitle("The title of window !!!")

if __name__ =='__main__':

    myApp = QApplication(sys.argv)
    myWidget = MyWidget()
    myWidget.show()
    myApp.exec_()
    sys.exit(0)

The first part of this script come with importing python modules.
Also I have a python class named : MyWidget.
In this class I used the QDial and this will make one round slider with 15 lines, and all settings.

    self.roiDial = QDial()
    self.roiDial.setNotchesVisible(True)
    self.roiDial.setMaximum(15)
    self.roiDial.setMinimum(1)
    self.roiDial.setValue(1)

The window application need to be set with one QGridLayout. This will set one grid and the QDial will put over this grid.

    self.myGridLayout = QGridLayout()
    self.myGridLayout.addWidget(self.roiDial, 0, 0)

    self.setLayout(self.myGridLayout)

You can search over internet about QGridLayout and QDial property and methods.
Let’s see this in action.
QDial_free-tutorials_003
You can apply your custom style:

import sys
import PySide
from PySide.QtGui import *
from PySide.QtCore import *

class MyWidget(QWidget):
  def __init__(self):
    QWidget.__init__(self)
    self.roiDial = QDial()
    self.roiDial.setStyleSheet("QDial{ background-color: QLinearGradient(spread:pad, x1:0.683, y1:1, x2:1, y2:0, stop:0 rgba(103, 103, 103,255), stop:1 rgba(144, 144, 144, 255)); }")
    self.roiDial.setNotchesVisible(True)
    self.roiDial.setMaximum(9)
    self.roiDial.setMinimum(1)
    self.roiDial.setValue(3)
    self.roiDial2 = QDial()
    self.myGridLayout = QGridLayout()
    self.myGridLayout.setSpacing(10)
    self.myGridLayout.addWidget(self.roiDial, 0, 0)

    self.myGridLayout.addWidget(self.roiDial2, 0, 1)
    self.setLayout(self.myGridLayout)
    self.setGeometry(100, 100, 300, 150)
    self.setWindowTitle("A simple QDial !")

if __name__ =='__main__':
    myApp = QApplication(sys.argv)
    myWidget = MyWidget()
    myWidget.show()
    myApp.exec_()
    sys.exit(0)

QDial_free-tutorials_001
I need to put two QDial and use some simple style :

self.roiDial.setStyleSheet

I will come also with another tutorial about setStyleSheet.

PySide – Introduction – part 002

First, PySide itself is licensed under LGPLv2.1, most of the examples are licensed under GPLv2.
After importing PySide then you can see all PySide python submodule:

>>> import PySide
>>> from PySide import *
>>> dir(PySide)
['QtCore', 'QtGui', 'QtNetwork', 'QtOpenGL', 'QtScript', 'QtSql', 'QtSvg', 'QtTe
st', 'QtWebKit', '__all__', '__builtins__', '__doc__', '__file__', '__name__', '
__package__', '__path__', '__version__', '__version_info__', '_setupQtDirectorie
s', '_utils']

Also this can help you to see all about Qt components and versions using by PySide:
Let’s how can do it:

# prints PySide version
>>> print PySide.__version__
1.2.2
# gets a tuple with each version component
>>> print PySide.__version_info__
(1, 2, 2, 'final', 0)
# prints the Qt version used to compile PySide
>>> print PySide.QtCore.__version__
4.8.5
# gets a tuple with each version components of Qt used to compile PySide
>>> print PySide.QtCore.__version_info__
(4, 8, 5)

Something about PySide submodules:
QtCore – core non-GUI functionality;
QtGui – extends QtCore with GUI functionality;
QtNetwork – offers classes that allow you to write TCP/IP clients and servers;
QtOpenGL – offers classes that make it easy to use OpenGL in Qt applications;
QtScript – provides classes for making Qt applications scriptable;
QtSql – helps you provide seamless database integration to your Qt applications;
QtSvg – provides classes for displaying the contents of SVG files;
QtWebkit – provides a Web browser engine;
The QtTest submodule can be used to test your PySide script code.
The structure PySide application can be see under my first tutorial.
About this PySide application most of this can be created with classes
To make one GUI ( ) just import QtGUI with your class …
This source code can be used also you can get some errors:

>>> import sys
...
>>> from PySide import QtGui
...
>>> app = QtGui.QApplication(sys.argv)

You need sys python module also the application can have this option : sys.argv .
You can searching and used all your widgets.
For example :

>>> import sys
>>> import PySide
>>> from PySide import *
>>> myapp = QtGui.QApplication(sys.argv)
>>> mywidgets = QtGui.QWidget()
>>> mywidgets.show()

The title of your application will be python.
You can set this title bellow show method , with :

mywidgets.setWindowTitle('my app title ')

About my class Example from pyside-introduction-part-001 then will told how to make one simple class example.
First I used this class named Example with this methods:

class Example(QtGui.QWidget):
...
#defaul init class method 
__init__(self)
...
#make and show the window application
initUI(self)
...
#my method to put the window to the desktop screen
center(self)
...
#method to deal with events and also close the application
closeEvent(self, event)
...

Into __init___ I used super() method.
The main reason I used this it’s :
-super() method lets you avoid referring to the base class explicitly and let you to use multiple inheritance.
The main function come with some source code:
Make the application :

app = QtGui.QApplication(sys.argv)

and also put my widgets under this with this python code:

ex = Example()

About events I will make another tutorial …

News: OpenBSD 5.7 released May 1, 2015

You can order a CDROM from our ordering system.

Also , you can see more about this here.

PySide – Introduction – part 001

PySide requires Python 2.6 or later and Qt 4.6 or better so I used python3.4 .
Can you read more about PySide from pypi.python.org/pypi/PySide:
PySide is the Python Qt bindings project, providing access the complete Qt 4.8 framework as well as to generator tools for rapidly generating bindings for any C++ libraries.
You can use pip3.4 to install this module:

C:\Python34\Scripts>pip3.4.exe install pyside
Collecting pyside
  Downloading PySide-1.2.2-cp34-none-win_amd64.whl (43.3MB)
    100% |################################| 43.3MB 4.2kB/s
Installing collected packages: pyside
Successfully installed pyside-1.2.2

Now I can some simple examples to see how to deal with this python module.
The code example is very simplistic if you know something about Qt. Even you can deal with this tutorial.
pyside

import sys
from PySide import QtGui

app = QtGui.QApplication(sys.argv)

wid = QtGui.QWidget()
wid.resize(250, 150)
wid.setWindowTitle('Simple')
wid.show()

sys.exit(app.exec_())

The next example will show you a message box with two buttons.
pyside

# -*- coding: utf-8 -*-
import sys
from PySide import QtGui

class Example(QtGui.QWidget):
    
    def __init__(self):
        super(Example, self).__init__()
        
        self.initUI()
        
    def initUI(self):               
        
        self.resize(250, 150)
        self.center()
        
        self.setWindowTitle('Center')    
        self.show()
        
    def center(self):
        
        qr = self.frameGeometry()
        cp = QtGui.QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())

    def closeEvent(self, event):
        
        reply = QtGui.QMessageBox.question(self, 'Message',
            "Are you sure to quit?", QtGui.QMessageBox.Yes | 
            QtGui.QMessageBox.No, QtGui.QMessageBox.No)

        if reply == QtGui.QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()         
        
def main():
    
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()