Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
Resource icon

PyQt5 - Python 3.12 Music Player - Part 1

In this part we are going to take the MVC (Modal, View, Controller) approach.
  • Modal - Handles all data manipulations
  • View - Handles the display
  • Controller - Handles the communications between Modal and View
To install PyQt6
open a terminal and do pip install pyqt6 pip install mutagen

To get a basic window

Python:
# Do the imports
import sys
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget, QGridLayout, QLabel,
                             QFrame)
from PyQt5.QtCore import Qt
class Modal:
    ''' Class will handle data manipulations '''
    pass
class Window(QMainWindow):
    ''' Class will handle the display '''
    def __init__(self):
        QMainWindow.__init__(self)
        self.setWindowTitle('My Music Player')

        # Sets a fixed size for the window
        self.setFixedSize(1280,720)

        # Create a main container - will hold all other widgets and layouts
        container = QGridLayout()

        # Create a header label
        header = QLabel('My Music Player')
        header.setFrameShape(QFrame.Shape.Box)
        header.setFrameShadow(QFrame.Shadow.Raised)
        header.setLineWidth(3)

        # Just like in css - set color and background color and the like. Is a bit limited.
        header.setStyleSheet('font-size: 48px; color: gold; \
                             font-weight: bold; font-family: comic sans ms; \
                             background-color: seagreen; padding: 10px;')
       
        # Aligning the text to the center
        header.setAlignment(Qt.AlignmentFlag.AlignCenter)

        # section for adding widgets to main container
        container.addWidget(header, 0, 0, 1, 3, Qt.AlignmentFlag.AlignTop)
        # Sets main layout
        widget = QWidget()
        widget.setLayout(container)
        self.setCentralWidget(widget)
class Controller:
    def __init__(self, modal, window):
        self.modal = modal
        self.window = window

if __name__ == '__main__':
    app = QApplication([])
    controller = Controller(Modal(), Window())
    controller.window.show()
    sys.exit(app.exec())

The Classes we use from PyQ5 are:
  • QApplication
  • QMainWindow - subclassed
  • QGrid Layout - Like it sounds. Allows us to place our widgets using a grid
  • QLabel - Allows to create labels
  • Qt - Allows help with aligning text and widgets
  • QFrame - put a border around the header label
We have create a basic window and header for our app
Back
Top Bottom