PyQt5 pixels

Thu 12 October 2017 by Little Captain

pixels

You can paint in a PyQt5 window using the QPainter widget. This widget supports adding pixels (dots) inside of the widget, unlike the other widgets.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QMainWindow, QLabel
from PyQt5.QtGui import QPainter, QColor, QPen
from PyQt5.QtGui import QIcon
from PyQt5 …
read more

PyQt5 image

Thu 12 October 2017 by Little Captain

image

PyQt5 (and Qt) support images by default. An image can be loaded using the QPixmap class.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QIcon, QPixmap

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 image - pythonspot.com'
        self.left = 10
        self …
read more

PyQt5 file dialog

Thu 12 October 2017 by Little Captain

file dialog

PyQt5 supports (native) file dialogs: open file, open files and save file. By calling the functions included in PyQt5 you get the default file dialog, you don’t have to recreate these dialogs from scratch.

Importing QFileDialog is required.

The methods used are QFileDialog.getOpenFileName(), QFileDialog.getOpenFileNames(), QFileDialog …

read more

PyQT5 input dialog

Thu 12 October 2017 by Little Captain

input dialog

PyQt5 supports several input dialogs, to use them import QInputDialog

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit
from PyQt5.QtGui import QIcon


class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 input dialogs - pythonspot.com'
        self.left = 10
        self.top = 10
        self.width …
read more

PyQt5 grid layout

Thu 12 October 2017 by Little Captain

grid layout

PyQt5 supports a grid layout, which is named QGridLayout. Widgets can be added to a grid in both the horizontal and vertical direction.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QGroupBox, QDialog, QVBoxLayout, \
    QGridLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot


class …
read more

PyQt5 horizontal layout

Thu 12 October 2017 by Little Captain

horizontal layout

A window can contain widgets (buttons, text field, image and others). Widgets are frequently added to a layout. A horizontal layout can be used to (dynamically) add widgets in horizontal direction.

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QGroupBox, QDialog, QVBoxLayout
from PyQt5.QtGui import …
read more

PyQt5 tabs

Thu 12 October 2017 by Little Captain

tabs

PyQt5 has a widget to create tabs known as QTabWidget. The QTabWidget can contain tabs (QWidgets), which have widgets on them such as labels, buttons, images etcetera.

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QPushButton, QWidget, QAction, QTabWidget, QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import …
read more

PyQt5 table

Thu 12 October 2017 by Little Captain

table

To add a table, you will need to import QTableWidget and QTableWidgetItem

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QAction, QTableWidget, QTableWidgetItem, QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot


class App(QWidget):
    def __init__(self):
        super().__init__()
        self.title = 'PyQt5 table - pythonspot.com …
read more

PyQt5 menu

Thu 12 October 2017 by Little Captain

menu

To create a menu for a PyQt5 program we need to use a QMainWindow. The top menu can be created with the method menuBar(). Sub menus are added with addMenu(name)

import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QPushButton, QAction
from PyQt5.QtGui import QIcon
from PyQt5 …
read more

PyQt5 absolute position

Thu 12 October 2017 by Little Captain

absolute position

PyQt5 supports several layout methods such as grid layouts, horzontal layous and absolute positioning. The layout you should pick depends on your preference and type of application.

Absolute positioning gives you total control over the widget positions but you have to explicitly define every widget location.

Widgets can …

read more