Thursday, November 25, 2021

Python Qt5 - QtWebEngineWidgets and YouTube Video.

In this tutorial I will show you how to play a video from Youtube using PyQt5 and this standard URL type:
I used this version of python, version 3.9.6.
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
The source code is simple:
import time

import sys

from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile
from PyQt5.QtWebEngineWidgets import QWebEnginePage, QWebEngineSettings
from PyQt5.QtWidgets import QApplication

if __name__ == '__main__':

    app = QApplication(sys.argv)

    webview = QWebEngineView()
    profile = QWebEngineProfile("my_profile", webview)
    webpage = QWebEnginePage(profile, webview)
    webpage.settings().setAttribute(QWebEngineSettings.PlaybackRequiresUserGesture, False)


The song is: SO EMOTIONAL- Olivia Rodrigo - Traitor | Allie Sherlock cover, see it on youtube.

Wednesday, November 17, 2021

Python 3.7.11 : My colab tutorials - part 021.

This is a simple notebook tutorial about how can test and get info from GPU on colab online tool.
This tutorial can be found on my GitHub account.

Tuesday, November 16, 2021

Python 3.7.11 : My colab tutorials - part 020.

The tutorial I created is a test and use of the Selenium WebDriver python package for to automate web browser interaction from Python.
This tutorial can be found on my GitHub account.

Friday, November 5, 2021

Python Qt5 - Drag and drop examples 001.

This tutorial si about drag and drop with PyQt5 and Python 3.9.6.
The drag and drop feature is very intuitive for the user.
The widgets should respond to the drag and drop events in order to store the data dragged into them. 
  • DragEnterEvent provides an event which is sent to the target widget as dragging action enters it.
  • DragMoveEvent is used when the drag and drop action is in progress.
  • DragLeaveEvent is generated as the drag and drop action leaves the widget.
  • DropEvent, on the other hand, occurs when the drop is completed. The event’s proposed action can be accepted or rejected conditionally.
Let's see two example, first is a drag and drop for one button:
import sys
from PyQt5.QtCore import Qt, QMimeData
from PyQt5.QtGui import QDrag
from PyQt5.QtWidgets import QPushButton, QWidget, QApplication

class Button(QPushButton):

    def __init__(self, title, parent):
        super().__init__(title, parent)

    def mouseMoveEvent(self, e):

        if e.buttons() != Qt.RightButton:

        mimeData = QMimeData()

        drag = QDrag(self)
        drag.setHotSpot(e.pos() - self.rect().topLeft())
        dropAction = drag.exec_(Qt.MoveAction)

    def mousePressEvent(self, e):
        if e.button() == Qt.LeftButton:

class Example(QWidget):

    def __init__(self):


    def initUI(self):


        self.button = Button('Button', self)
        self.button.move(100, 65)

        self.setWindowTitle('Drag and drop with mouse - right click to move Button!')
        self.setGeometry(300, 300, 550, 450)

    def dragEnterEvent(self, e):

    def dropEvent(self, e):
        position = e.pos()


def main():
    app = QApplication(sys.argv)
    ex = Example()

if __name__ == '__main__':
... this source code is for a drag and drop for a image file:
import sys, os
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap

class ImageLabel(QLabel):
    def __init__(self):

        self.setText('\n\n Drop Image Here \n\n')
                border: 3px dashed #bbb

    def setPixmap(self, image):

class AppDemo(QWidget):
    def __init__(self):
        self.setWindowTitle('Drag and drop one image to this window!')
        self.setGeometry(300, 300, 550, 450)


        mainLayout = QVBoxLayout()

        self.photoViewer = ImageLabel()


    def dragEnterEvent(self, event):
        if event.mimeData().hasImage:

    def dragMoveEvent(self, event):
        if event.mimeData().hasImage:

    def dropEvent(self, event):
        if event.mimeData().hasImage:
            file_path = event.mimeData().urls()[0].toLocalFile()


    def set_image(self, file_path):

app = QApplication(sys.argv)
demo = AppDemo()

Saturday, October 30, 2021

Python 3.7.11 : My colab tutorials - part 019.

The tutorial I created is a test and use Probabilistic Graphical Models for the most basic problem the coin problem with the pgmpy python module.
This tutorial can be found on my GitHub account.

Sunday, October 10, 2021

News: The new python version 3.10.0.

Almost six days ago, the new version of python was released, version 3.10.0, see this.
Its installation on windows operating systems is done in the same way as the old installations, with the same steps and the same settings.
After installation, I turned it on and tested some of the new features.
Python 3.10.0 (tags/v3.10.0:b494f59, Oct  4 2021, 19:00:18) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
Python 3.10 comes with precise and constructive error messages like:
SyntaxError: '{' was never closed
>>> foo(a, b for b in range(5), c)
    foo(a, b for b in range(5), c)
SyntaxError: Generator expression must be parenthesized
>>> {a, b for (a, b) in zip("a", "b")}
    {a, b for (a, b) in zip("a", "b")}
SyntaxError: did you forget parentheses around the comprehension target?
SyntaxError: expected ':'
SyntaxError: invalid syntax. Perhaps you forgot a comma?
SyntaxError: ':' expected after dictionary key
SyntaxError: expected 'except' or 'finally' block
SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
IndentationError: expected an indented block after 'if' statement in line ...
>>> import collections
>>> collections.namedtoplo
AttributeError: module 'collections' has no attribute 'namedtoplo'. Did you mean: 'namedtuple'?
>>> a = 0
>>> aa
NameError: name 'aa' is not defined. Did you mean: 'a'?
PEP 634: Structural Pattern Matching Structural 
... the pattern matching is a comprehensive addition to the Python language. 
They tell us: Pattern matching enables programs to extract information from complex data types, branch on the structure of data, and apply specific actions based on different forms of data.
PEP 604: New Type Union Operator as X|Y 
PEP 613: Explicit Type Aliases 
PEP 647: User-Defined Type Guards 
PEP 612: Parameter Specification Variables
You can see more on the official webpage.