While we can build simple applications without using classes using PySide6, But in big applications and Massive coding systems We should use Classes But why?
- To understand why do we need classes in PySide6 We should first see the Python code First
from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit
import sys
class MainWindow(QWidget):
def __init__(self):
super().__init__()
button1 = QPushButton("Button 1")
input = QLineEdit()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
Before talking about why do we need Classes for PySide6
Let's Explain the code first line by line
The imports
first thing we make the imports we do need: from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QLineEdit
The QApplication Is the simply the application we will make, Like empty app on the RAM it do nothing but it's on the RAM if it's alone
And the QWidget Is the Blank screen That will be placed on the Empty Application in the RAM
The QPushButton Is like any button we are saying in any app Like the Subscribe button on YouTube or like Post button on Twitter
QLineEdit is the input bar, Like the input bar of ChatGPT where you put on it your prompt or like The input bar in WhatsApp Where you type any thing on it to send it to your friends
The class
And finally The thing You clicked on the post for
First thing we define the class
- How can we define it?
- Why do we need to define it?
- Why do even we want it?
- Who created it? (NOOO IAM JUST KIDDING)
We can simply define the class in python by just typing class That's it just class then the name of it For Example MainWindow and then a little semi-colon : OR EVEN WE GIVE IT A Parents
And Why do we need to define it, For simply use it BRILLIANT RIGHT?
And we want the classes in PySide6 for give it a parents QWidget or even QMainWindow, And we will explain both of them right now but before it Let's explain first what does parents means
Imagine a superhero became to old and will die soon But he need to make last thing in his life which is giving all of his superpowers to his son, And to do this he need to say: I am giving all of my powers and knowledge and memory and intelligence and every single thing in me to my son, And he can do every single thing using my superpowers and nothing ever can stop him.
That's is literally the same thing in parent's when you want to give all of an module, Library knowledge, functions, or even another classes to an class you simply need to make a parentheses () sticking to the name of the class so it be: class MainWindow() And inside those parentheses , you need to write the library name which is in our case QWidget And as we said QWidget is an literally Blank paper, But do not underestimate this blank sheet of paper. Because in it's the main thing for everything, yeah it's Blank but not dust, In using this Blank thing we give it as a parent for our class so our class become the little new superhero who toke everything from his dad, so when we make self.setWindowTitle() We can give to our class a title, But what what is self? And what is even def __init__() and also what is super()._init__()??!!!
We will understand everything right now
the def __init__(self): is a special method that Python automatically calls whenever you create an object from the class (WE will explain objects soon), It's called the constructor it's run automatically everything inside the class automatically and the self inside the __init__() is simply referring to the class it self (And BTW you can name it anything you want, But the important thing is to make it at index 1 or the first parameter inside def __init__(self), So simply self, Means that the class say: ME YEAH IT'S ME WHO WANT TO BE HIS TITLE IS SIMPLE WINDOW by simply making self.setWindowTitle("SIMPLE WINDOW"), and it's the same thing that goes on the size of the window, and it's icon and every single thing
And do not thing self power stops their, No and never there is a lot of another powers, If we talked about it, it's will take days
And lastly super().__init__() This one is simple as death, it's just saying: MY PARENT (QWidget) WANT TO MAKE THINGS TO SET HIS SELF UP, that's it so simple but so important because without it the PySide6 will raise an error because the PARENT should set his self up Before saying: I am giving all of my powers and knowledge and memory and intelligence and every single thing in me to my son, And he can do every single thing using my superpowers and nothing ever can stop him.
And now the question Is half answered
And the another half is simple objects what is object? it's an varible taking your class name for example:
object1 = MainWindow()
This is the object and that object holding the class and the class holds the code of your window, sometimes backend, or even your entire application,
IF NOTHING IS CLEAR YOU CAN COMMENT BELOW AND I WILL RESPOND AS QUICKLY AS POSSIBLE
And BTW who created the classes in python is called: Guido van Rossum
Top comments (0)