PyQt5 QTabWidget exercise

py_qt5_tab.py, a simple exercise of using PyQt5 QTabWidget.
"""
PyQt5 exercise of QTabWidget
"""
import sys, platform, os
from PyQt5.QtWidgets import (QMainWindow, QApplication, QPushButton, QLabel, QWidget, QTabWidget,
                             QVBoxLayout, QGridLayout)
from PyQt5.QtCore import pyqtSlot, Qt

from PyQt5.QtGui import QPalette, QColor, QFont

class App(QMainWindow):
    
    def __init__(self):
        super().__init__()
        self.title = __file__
        self.left = 0
        self.top = 0
        self.setWindowTitle(self.title)

        self.main_widget = MyMainWidget(self)
        self.setCentralWidget(self.main_widget)

        self.show()

class MyMainWidget(QWidget):
    
    def read_f(self, file):
        with open(file, encoding='UTF-8') as reader:
            content = reader.read()
        return content
    
    def read_pretty_name(self):
        with open("/etc/os-release") as f:
            os_release = {}
            for line in f:
                k,v = line.rstrip().split("=")
                os_release[k] = v.strip('"')
        return os_release['PRETTY_NAME']
           
    @pyqtSlot()
    def on_btn1_Clicked(self):
        print("on_btn1_Clicked")
           
    def __init__(self, parent):
        super(QWidget, self).__init__(parent)
        self.layout = QVBoxLayout()
        
        # Initialize tab screen
        self.tabs = QTabWidget()
        self.tab1 = QWidget()
        self.tab2 = QWidget()

        # Add tabs
        self.tabs.addTab(self.tab1," Tab1 ")
        self.tabs.addTab(self.tab2," Tab2 ")
        
        #=== Tab Capture ===
        # Create first tab
        self.tab1.layout = QVBoxLayout()
        
        self.tab1.setAutoFillBackground(True)
        palette = self.palette()
        palette.setColor(QPalette.Window, QColor('black'))
        self.tab1.setPalette(palette)
        
        #Prepare tab1
        self.btn1 = QPushButton("Button 1")
        self.btn1.setFont(QFont("Helvetica", 16, QFont.Bold))
        self.btn1.clicked.connect(self.on_btn1_Clicked)
        
        self.tab1.layout.addWidget(self.btn1)
        self.tab1.layout.addStretch()
        
        self.tab1.setLayout(self.tab1.layout)
        
        #=== Tab Info ===
        self.tab2.layout = QVBoxLayout()
        
        infoGridLayout = QGridLayout()
        
        rowSpan = 1
        columnSpan0 = 1
        columnSpan1 = 5
        infoGridLayout.addWidget(QLabel('Python', self), 0, 0, rowSpan, columnSpan0)
        infoGridLayout.addWidget(QLabel(platform.python_version(), self), 0, 1, rowSpan, columnSpan1)
        
        infoGridLayout.addWidget(QLabel(' ', self), 1, 0, rowSpan, columnSpan0)
        infoGridLayout.addWidget(QLabel('Machine:', self), 2, 0, rowSpan, columnSpan0)
        infoGridLayout.addWidget(QLabel('Board', self), 3, 0, rowSpan, columnSpan0, Qt.AlignTop)
        board_def = "/proc/device-tree/model"
        board_info = self.read_f("/proc/device-tree/model") +"\n(" + board_def +")"
        infoGridLayout.addWidget(QLabel(board_info, self), 3, 1, rowSpan, columnSpan0)
        
        infoGridLayout.addWidget(QLabel('OS', self), 4, 0, rowSpan, columnSpan0, Qt.AlignTop)
        os_info = self.read_pretty_name() + "\n" + os.uname()[3]
        infoGridLayout.addWidget(QLabel(os_info, self), 4, 1, rowSpan, columnSpan1)
        
        self.tab2.layout.addLayout(infoGridLayout)
        self.tab2.layout.addStretch()
        
        self.tab2.setLayout(self.tab2.layout)
        
        #==================================
        # Add tabs to widget
        self.layout.addWidget(self.tabs)
        self.setLayout(self.layout)

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





in real work:
Python/PyQt5 GUI to control Raspberry Pi Camera using picamera2 lib


Comments

Popular posts from this blog

MicroPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI, using boochow/MicroPython-ST7735 library.

CameraWebServe: ESP32-S3 (arduino-esp32) + OV5640 camera module