analitics

Pages

Showing posts with label PySide. Show all posts
Showing posts with label PySide. Show all posts

Wednesday, December 5, 2018

PySide2 and PyQt versions.

A short intro about this python module named PySide2 can be found on the Wikipedia webpage.
The purpose of this tutorial is to introduce concepts about licenses and develop python programs using ergonomic design interfaces with PySide2 and PyQt versions.

PySide2 is a Python binding of the cross-platform GUI toolkit Qt, currently developed by The Qt Company under the Qt for Python project. It is one of the alternatives to the standard library package Tkinter.
...
PySide was released under the LGPL in August 2009 by Nokia,[1] the former owners of the Qt toolkit, after Nokia failed to reach an agreement with PyQt developers Riverbank Computing[7] to change its licensing terms to include LGPL as an alternative license.
...


Now about the LGPL software license:

The GNU Lesser General Public License (LGPL) is a free software license published by the Free Software Foundation (FSF). The license allows developers and companies to use and integrate software released under the LGPL into their own (even proprietary) software without being required by the terms of a strong copyleft license to release the source code of their own components.

I wrote in the past about PySide and you can find on this blog or on my website.
Let's start installing Python using pip3.6.
C:\Python364>cd Scripts

C:\Python364\Scripts>pip3.6.exe install PySide2
Collecting PySide2
  Downloading https://files.pythonhosted.org/packages/10/ba/7448ec862655c356ade2
2351ed46c9260773186c37ba0d8ceea1ef8c7515/PySide2-5.11.2-5.11.2-cp35.cp36.cp37-no
ne-win_amd64.whl (128.7MB)
    100% |████████████████████████████████| 128.7MB 44kB/s
Installing collected packages: PySide2
Successfully installed PySide2-5.11.2
The PySide2 python module is another way to connect Qt with Python modules.
You can take a look at this python module at Qt website .
The source code is more simple but this can put the development in trouble if you try to follow the PyQt way.
Let's see a simple example. I create the main window for this example.

The layout of this window named main_layout is split into two: secondary_splitter_1 and secondary_splitter_2.
Into this, I add for each area the defined class named SecondaryWindow.
The four variables for this SecondaryWindow class are named like secondary_window_1, secondary_window_2, secondary_window_3 and secondary_window_4.
Interface programming with PySide2 and PyQt keeps a classic form of statements and then structured implementations. It seems simpler to create interfaces with PySide, but the development and community interest seems to be attracted to PyQt.
In both cases, the programming speed in the python programming language is required.
Error management is simpler and more limited in PySide2. I have noticed PyQt essential changes in developing even more advanced versions (see PyQt4 and PyQt5).
Let's see the source code:
import sys
from PySide2 import QtGui, QtCore, QtWidgets
from PySide2.QtGui import *
from PySide2.QtCore import *
from PySide2.QtWidgets import *

class SecondaryWindow(QtWidgets.QWidget):

    def __init__(self, label, parent=None):
        super(SecondaryWindow, self).__init__(parent)

        self.label = QtWidgets.QLabel(label, parent=self)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setStyleSheet("QLabel {font-size:18px;color:blue}")

        self.main_layout = QtWidgets.QVBoxLayout()
        self.main_layout.addWidget(self.label)
        self.setLayout(self.main_layout)

class MainWindow(QtWidgets.QWidget):

    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        self.secondary_window_1 = SecondaryWindow("1", parent=self)
        self.secondary_window_2 = SecondaryWindow("2", parent=self)
        self.secondary_window_3 = SecondaryWindow("3", parent=self)
        self.secondary_window_4 = SecondaryWindow("4", parent=self)

        self.secondary_splitter_1 = QtWidgets.QSplitter(QtCore.Qt.Horizontal, parent=self)
        self.secondary_splitter_1.addWidget(self.secondary_window_1)
        self.secondary_splitter_1.addWidget(self.secondary_window_2)

        self.secondary_splitter_2 = QtWidgets.QSplitter(QtCore.Qt.Horizontal, parent=self)
        self.secondary_splitter_2.addWidget(self.secondary_window_3)
        self.secondary_splitter_2.addWidget(self.secondary_window_4)

        self.main_splitter = QtWidgets.QSplitter(QtCore.Qt.Vertical, parent=self)
        self.main_splitter.addWidget(self.secondary_splitter_1)
        self.main_splitter.addWidget(self.secondary_splitter_2)

        self.main_layout = QtWidgets.QVBoxLayout()
        self.main_layout.addWidget(self.main_splitter)
        self.setLayout(self.main_layout)

        self.setWindowTitle("PySide2 example")
        self.resize(220, 220)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())

Thursday, May 7, 2015

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 I will tell you 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 ...



Saturday, May 2, 2015

New tutorials about PySide .

I make one new tutorial about PySide. I started with one simple tutorial with two simple examples.
I hope this help you.
You can read more about this