analitics

Pages

Showing posts with label PyQt5. Show all posts
Showing posts with label PyQt5. Show all posts

Tuesday, October 23, 2018

Python Qt5 : toolbar example.

This is a simple example with PyQt5 python module and python 3.6.4 version.
The example is about how to create a toolbar with PyQt5.
The base of this source code is the create a default window application.
I create a toolbar and I add an action to this toolbar.
The name of the toolbar is my_toolbar.
The action is named one_action.
This action is linked to a python function named action_one.
I add to my source code another function named alert.
This is good for debugging part to handle with errors and displaying alerts.
Let's see the source code:
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
import sys

class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args, **kwargs)
        self.status = QStatusBar()
        self.setStatusBar(self.status)
        my_toolbar = QToolBar("toolbar")
        my_toolbar.setIconSize(QSize(48, 48))
        self.addToolBar(my_toolbar)
        
        one_action = QAction(QIcon(), "Action one", self)        
        one_action.setStatusTip("Action one on toolbar")
        one_action.triggered.connect(self.action_one)
        my_toolbar.addAction(one_action)
        
        self.setWindowTitle("Window PyQt5 - 001")
        self.show()

    def action_one(self):
        print("Action one")

    def alert(self, s):
        """
        This handle errors and displaying alerts.
        """
        err = QErrorMessage(self)
        err.showMessage(s)

if __name__ == '__main__':

    app = QApplication(sys.argv)
    app.setApplicationName("Window PyQt5 - 001")

    window = MainWindow()
    app.exec_()

Thursday, April 26, 2018

Python Qt5 : menu example.

This simple tutorial is about PyQt5 and menu window example.
I have a similar example with Qt4 on this blog.
The main reason for this tutorial comes from the idea of simplicity and reuse the source code from PyQt4 and PyQt5.
I do not know if there are significant changes to the Qt5 base IU. However, it is good to check on the official pages. Let's look at the example with comments specific to source code lines:
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 26 17:20:02 2018

@author: catafest
"""
import sys
from PyQt5.QtWidgets import QMainWindow, QAction, qApp, QApplication, QDesktopWidget
from PyQt5.QtGui import QIcon

class Example(QMainWindow):
    #init the example class to draw the window application    
    def __init__(self):
        super().__init__()    
        self.initUI()
    #create the def center to select the center of the screen         
    def center(self):
        # geometry of the main window
        qr = self.frameGeometry()
        # center point of screen
        cp = QDesktopWidget().availableGeometry().center()
        # move rectangle's center point to screen's center point
        qr.moveCenter(cp)
        # top left of rectangle becomes top left of window centering it
        self.move(qr.topLeft())
    #create the init UI to draw the application
    def initUI(self):               
        #create the action for the exit application with shortcut and icon
        #you can add new action for File menu and any actions you need
        exitAct = QAction(QIcon('exit.png'), '&Exit', self)        
        exitAct.setShortcut('Ctrl+Q')
        exitAct.setStatusTip('Exit application')
        exitAct.triggered.connect(qApp.quit)
        #create the status bar for menu 
        self.statusBar()
        #create the menu with the text File , add the exit action 
        #you can add many items on menu with actions for each item
        menubar = self.menuBar()
        fileMenu = menubar.addMenu('&File')
        fileMenu.addAction(exitAct)
        #resize the window application 
        self.resize(640, 480)
        #draw on center of the screen 
        self.center()
        #add title on windows application 
        self.setWindowTitle('Simple menu')
        #show the application
        self.show()
        #close the UI class
        
if __name__ == '__main__':
    #create the application 
    app = QApplication(sys.argv)
    #use the UI with new  class
    ex = Example()
    #run the UI 
    sys.exit(app.exec_())
The result of this code is this:

Tuesday, March 20, 2018

Python 3.6.4 : Testing PyQt5 with Spyder I.D.E.

Today I tested the PyQt5 python module with python version 3.6.4.
The script was created and tested with Spyder I.D.E. version 3.2.8.
The PyQt5 version is:
from PyQt5.Qt import PYQT_VERSION_STR
print("PyQt version:", PYQT_VERSION_STR)
PyQt version: 5.9.2
This is the python script:
import sys
from PyQt5.QtWidgets import (QWidget, QCalendarWidget,
    QLabel, QApplication)
from PyQt5.QtCore import QDate
 
class Calendar(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()
 
    def initUI(self):
        calendar = QCalendarWidget(self)
        calendar.setGridVisible(True)
        calendar.move(0, 15)
        calendar.clicked[QDate].connect(self.showDate)
        calendar.currentPageChanged[int, int].connect(self.currentPageChanged)
 
        self.lbl = QLabel(self)
        date = calendar.selectedDate()
        self.lbl.setText(date.toString())
        self.lbl.move(0, 0)
 
        self.setGeometry(300, 300, 300, 300)
        self.setWindowTitle('Calendar')
        self.show()
 
    def showDate(self, date):
        self.lbl.setText(date.toString())
 
    def currentPageChanged(self, year, month):
        print(year, month)
 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    calendar_test = Calendar()
    sys.exit(app.exec_())
The result of this script is: