analitics

Pages

Monday, November 19, 2018

Using Google Classroom API with python.

Today I read and test Google API with the Python programming language.
You can find a good example here.
The example shows how to read with Google Classroom API, v1.
This example can be used as a default example if you want to use googlescopes.
The Google documentation tells us:
This document lists the OAuth 2.0 scopes that you might need to request to access Google APIs, depending on the level of access you need. Sensitive scopes require review by Google and have a sensitive indicator on the Google Cloud Platform (GCP) Console's OAuth consent screen configuration page. Many scopes overlap, so it's best to use a scope that isn't sensitive. For information about each method's scope requirements, see the individual API documentation.
The script use credentials.json file created by the Google project in the folder with the python script.
The script creates automatically when the authorization flow completes a token.json file.
The control of the project can be used with the Google Cloud Console.
The result of the example script can be seen in the next image:

Friday, November 16, 2018

Python Qt5 : setStyleSheet example.

Today I will show you how to style the PyQt5 widgets and create a good looking application interface.
The main goal of this tutorial is to see where you can use the style issue.
I used just one edit and one button to have a simple example.
The result of my example is this:

The example start with a simple application with QPushButton, QLineEdit.
Is more simple to use a class for the button because we need to create a different style for each action: enterEvent or leaveEvent and so on.
You can see I used QFont to change the font from button.
This class is named Push_button and will be used like any QPushButton from default PyQt5 examples.
We can do this for any widget and change it with setStyleSheet.
Another part of the code is for QLineEdit.
This can be changed easily with setStyleSheet, first with the default of this and make other changes when you need.
The source code has an QGridLayout to help us to align the widgets.
Let's see the source code:
from PyQt5 import QtWidgets, QtGui, QtCore
from PyQt5.QtCore import pyqtSignal

font_button = QtGui.QFont()
font_button.setFamily("Corbel")
font_button.setPointSize(10)
font_button.setWeight(100)

class Push_button(QtWidgets.QPushButton):
    
    def __init__(self, parent=None):
        super(Push_button, self).__init__(parent)
        self.setMouseTracking(True)
        self.setStyleSheet("margin: 1px; padding: 10px; \
      background-color: \
                           rgba(255,255,0,255); \
                           color: rgba(0,0,0,255); \
                           border-style: solid; \
                           border-radius: 4px; border-width: 3px; \
                           border-color: rgba(0,0,0,255);")

    def enterEvent(self, event):
        if self.isEnabled() is True:
            self.setStyleSheet("margin: 10px; padding: 10px; \
          background-color: \
                               rgba(255,255,0,255); \
                               color: rgba(0,0,10,255); \
                               border-style: solid; \
                               border-radius: 8px; \
                               border-width: 1px; \
                               border-color: \
                               rgba(0,0,100,255);")
        if self.isEnabled() is False:
            self.setStyleSheet("margin: 10px; padding: 10px; \
          background-color: \
                               rgba(255,255,0,255); \
                               color: rgba(0,0,10,255); \
                               border-style: solid; \
                               border-radius: 8px; \
                               border-width: 1px; \
                               border-color: \
                               rgba(0,0,100,255);")

    def leaveEvent(self, event):
        self.setStyleSheet("margin: 10px; padding: 10px; \
                           background-color: rgba(0,0,0,100); \
                           color: rgba(0,0,255,255); \
                           border-style: solid; \
                           border-radius: 8px; border-width: 1px; \
                           border-color: rgba(0,50,100,255);")


class QthreadApp(QtWidgets.QWidget):
    sig = pyqtSignal(str)
    def __init__(self, parent=None):
        QtWidgets.QWidget.__init__(self, parent)
        self.setWindowTitle("PyQt5 style application")
        self.setWindowIcon(QtGui.QIcon("icon.png"))
        self.setMinimumWidth(resolution.width() / 3)
        self.setMinimumHeight(resolution.height() / 2)
        self.setStyleSheet("QWidget { \
                           background-color: rgba(0,0,100,250);} \
                           QScrollBar:horizontal {width: 1px; \
                           height: 1px; \
                           background-color: rgba(0,100,255,0);} \
                           QScrollBar:vertical {width: 1px; \
                           height: 10px; \
                           background-color: rgba(0,41,59,255);}")
        self.linef = QtWidgets.QLineEdit(self)
        self.linef.setPlaceholderText("Input text ...")
        self.linef.setStyleSheet("margin: 10px; padding: 10px; \
                                 background-color: \
                                 rgba(0,0,0,255);\
                                 color: rgba(255,0,0,255); \
                                 border-style: solid; \
                                 border-radius: 15px; \
                                 border-width: 1px; \
                                 border-color: \
                                 rgba(255,255,255,255);")
        self.my_button = Push_button(self)
        self.my_button.setText("Blue")
        self.my_button.setFixedWidth(72)
        self.my_button.setFont(font_button)
        self.my_grid = QtWidgets.QGridLayout()
        self.my_grid.addWidget(self.linef, 0, 0, 1, 220)
        self.my_grid.addWidget(self.my_button, 0, 220, 1, 1)
        self.my_grid.setContentsMargins(8, 8, 8, 8)
        self.setLayout(self.my_grid)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    desktop = QtWidgets.QApplication.desktop()
    resolution = desktop.availableGeometry()
    myapp = QthreadApp()
    myapp.setWindowOpacity(0.95)
    myapp.show()
    myapp.move(resolution.center() - myapp.rect().center())
    sys.exit(app.exec_())
else:
    desktop = QtWidgets.QApplication.desktop()
    resolution = desktop.availableGeometry()

Monday, November 12, 2018

Python Qt5 : QCalendarWidget example.

This tutorial is about QCalendarWidget.
Use a default application and add this widget.
You can change and get the date from the widget calendar like any widget.
The result of this source code is this result:

This is the source code for QCalendarWidget example:
import sys
from PyQt5 import *
from PyQt5.QtWidgets import QApplication, QCalendarWidget, QWidget, QLabel
from PyQt5.QtCore import *
from PyQt5.QtGui import *

class Example(QWidget):
   def __init__(self):
      super(Example, self).__init__()
      self.initUI()
   def initUI(self):
      my_calendar = QCalendarWidget(self)
      my_calendar.setGridVisible(True)
      my_calendar.move(10, 20)
      my_calendar.clicked[QDate].connect(self.show_date)
      self.my_label = QLabel(self)
      date = my_calendar.selectedDate()
      self.my_label.setText(date.toString())
      self.my_label.move(10, 220)
      self.setGeometry(100,100,320,270)
      self.setWindowTitle('Calendar')
      self.show()
   def show_date(self, date):
      self.my_label.setText(date.toString())

def main():
   app = QApplication(sys.argv)
   ex = Example()
   sys.exit(app.exec_())
if __name__ == '__main__':
   main()

Saturday, November 10, 2018

Python Qt5 : QTabWidget example.

Today I test the QTabWidget with an simple example.
The source code uses the QTabWidget and create two Tab: FirstTab and TabTwo.
I add for each tab two labels: tab_one_label_one, tab_one_label_two,tab_two_label_one and tab_two_label_two.
For layouts, I used: vboxLayout with first_layout and second_layout.
The result of my running code is this:

The rest of the source code is simple:
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QDialog, QApplication, QWidget
from PyQt5.QtWidgets import QVBoxLayout, QTabWidget, QLabel

class TabDialog(QDialog):
    def __init__(self):
        super().__init__()

        self.setWindowTitle("Tab Widget Application")
        self.setWindowIcon(QIcon("icon.png"))

        tabwidget = QTabWidget()
        tabwidget.addTab(FirstTab(), "First Tab")
        tabwidget.addTab(TabTwo(), "Second Tab")

        vboxLayout = QVBoxLayout()
        vboxLayout.addWidget(tabwidget)

        self.setLayout(vboxLayout)

class FirstTab(QWidget):
    def __init__(self):
        super().__init__()
        tab_one_label_one = QLabel("tab_one_label_one")
        tab_one_label_two = QLabel("tab_one_label_two")
        
        first_layout = QVBoxLayout()
        first_layout.addWidget(tab_one_label_one)
        first_layout.addWidget(tab_one_label_two)
        self.setLayout(first_layout)

class TabTwo(QWidget):
    def __init__(self):
        super().__init__()
        tab_two_label_one = QLabel("tab_two_label_one")
        tab_two_label_two = QLabel("tab_two_label_two")
        
        second_layout = QVBoxLayout()
        second_layout.addWidget(tab_two_label_one)
        second_layout.addWidget(tab_two_label_two)
        self.setLayout(second_layout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    tabdialog = TabDialog()
    tabdialog.show()
    app.exec()

Friday, November 9, 2018

Python Qt5 : default icons with QStyle.

This is a simple example of QStyle.
Using the QStyle can solve more issues above this example.
The QStyle can solve more problems than this example.
You can change everything in PyQt5 and Qt.
The QStyle class is an abstract base class that encapsulates the look and feel of a GUI, read here.
This is the result of a source code I used to show you how can use default icons from PyQt5.

See this source code:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *

class Widget(QWidget):
  
    def __init__(self, parent= None):
        super(Widget, self).__init__()
        icons = [
'SP_TitleBarMinButton', 
'SP_TitleBarMenuButton', 
'SP_TitleBarMaxButton', 
'SP_TitleBarCloseButton', 
'SP_TitleBarNormalButton', 
'SP_TitleBarShadeButton', 
'SP_TitleBarUnshadeButton', 
'SP_TitleBarContextHelpButton', 
'SP_MessageBoxInformation', 
'SP_MessageBoxWarning', 
'SP_MessageBoxCritical', 
'SP_MessageBoxQuestion', 
'SP_DesktopIcon', 
'SP_TrashIcon', 
'SP_ComputerIcon', 
'SP_DriveFDIcon', 
'SP_DriveHDIcon', 
'SP_DriveCDIcon', 
'SP_DriveDVDIcon', 
'SP_DriveNetIcon', 
'SP_DirHomeIcon', 
'SP_DirOpenIcon', 
'SP_DirClosedIcon', 
'SP_DirIcon', 
'SP_DirLinkIcon', 
'SP_FileIcon', 
'SP_FileLinkIcon', 
'SP_FileDialogStart', 
'SP_FileDialogEnd', 
'SP_FileDialogToParent', 
'SP_FileDialogNewFolder', 
'SP_FileDialogDetailedView', 
'SP_FileDialogInfoView', 
'SP_FileDialogContentsView', 
'SP_FileDialogListView', 
'SP_FileDialogBack', 
'SP_DockWidgetCloseButton', 
'SP_ToolBarHorizontalExtensionButton', 
'SP_ToolBarVerticalExtensionButton', 
'SP_DialogOkButton', 
'SP_DialogCancelButton', 
'SP_DialogHelpButton', 
'SP_DialogOpenButton', 
'SP_DialogSaveButton', 
'SP_DialogCloseButton', 
'SP_DialogApplyButton', 
'SP_DialogResetButton', 
'SP_DialogDiscardButton', 
'SP_DialogYesButton', 
'SP_DialogNoButton', 
'SP_ArrowUp',  
'SP_ArrowDown', 
'SP_ArrowLeft', 
'SP_ArrowRight', 
'SP_ArrowBack', 
'SP_ArrowForward', 
'SP_CommandLink', 
'SP_VistaShield', 
'SP_BrowserReload', 
'SP_BrowserStop', 
'SP_MediaPlay', 
'SP_MediaStop', 
'SP_MediaPause', 
'SP_MediaSkipForward',
'SP_MediaSkipBackward', 
'SP_MediaSeekForward',
'SP_MediaSeekBackward', 
'SP_MediaVolume', 
'SP_MediaVolumeMuted', 
'SP_CustomBase'
            ]
        Col_size = 6
         
        layout = QGridLayout()
 
        count = 0
        for i in icons:
            select_button = QPushButton(i)
            select_button.setIcon(self.style().standardIcon(getattr(QStyle, i)))
 
            layout.addWidget(select_button, count / Col_size, count % Col_size)
            count += 1
             
        self.setLayout(layout)
         
if __name__ == '__main__':
    app = QApplication(sys.argv)
             
    dialog = Widget()
    dialog.show()
             
    app.exec_()

Thursday, November 8, 2018

Python Qt5 : QFileDialog and QTextEdit example.

This tutorial is about QFileDialog and how to use it.
First you need to create a default PyQ5 application and add your method is called by my_OpenDialog.
This will be connected to one button.
My application uses the QTextEdit to show HTML and text files.
If you try to see another file this will open badly.
I set the completeSuffix just for HTML and text.
As you know this returns the complete suffix (extension) of the file.
The content of this file is put into QTextEdit widget create and named editor_text.
This is result of using QFileDialog with python:

This is the source code with the QFileDialog example:
import sys, os
from PyQt5 import QtCore, QtWidgets

class Window(QtWidgets.QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.setWindowTitle('Text document')
        self.editor_text = QtWidgets.QTextEdit(self)

        self.button_OpenDialog = QtWidgets.QPushButton('Open', self)
        self.button_OpenDialog.clicked.connect(self.my_OpenDialog)

        layout = QtWidgets.QGridLayout(self)
        layout.addWidget(self.editor_text, 0, 0, 1, 1)
        layout.addWidget(self.button_OpenDialog, 1, 0)
        #self.handleTextChanged()

    def my_OpenDialog(self):
        path = QtWidgets.QFileDialog.getOpenFileName(
            self, 'Open file', '',
            'HTML files (*.html);;Text files (*.txt)')[0]
        if path:
            file = QtCore.QFile(path)
            if file.open(QtCore.QIODevice.ReadOnly):
                stream = QtCore.QTextStream(file)
                text = stream.readAll()
                info = QtCore.QFileInfo(path)
                if info.completeSuffix() == 'html':
                    self.editor_text.setHtml(text)
                else:
                    self.editor_text.setPlainText(text)
                file.close()

if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Window()
    window.resize(640, 480)
    window.show()
    sys.exit(app.exec_())