In this lesson we want to learn that How to Create QPushButton in PySide6 , it is One of the most commonly used widgets in PySide6 is QLabel, which can display text and images in window or dialog. In this lesson, we will discuss how to work with the Python PySide6 QLabel widget to create labels and customize their appearance.
Creating a QPushButton
To create a QPushButton in PySide6, we first need to import the QtWidgets module.
1 |
from PySide6.QtWidgets import QPushButton |
We can then create a new QPushButton object by calling its constructor
1 |
button = QPushButton("Click me!") |
This creates a new button with the label “Click me!”.
We can add this button to our application’s layout using the addWidget() method of a layout object. For example if we have a QVBoxLayout object called layout we can add the button to it like this:
1 |
layout.addWidget(button) |
When the button is clicked, it emits a signal that we can connect to a slot to perform some action.
Connecting Signals and Slots
To perform an action when a QPushButton is clicked, we need to connect its clicked signal to a slot. A slot is a function that performs some action when it is called. We can define a slot function in our code like this:
1 2 |
def on_button_click(): print("Button was clicked!") |
We can then connect the button’s clicked signal to this slot using the connect() method:
1 |
button.clicked.connect(on_button_click) |
Now, when the button is clicked, the on_button_click() function will be called, and the message “Button was clicked!” will be printed to the console.
Setting Button Properties
We can also set various properties of a QPushButton to change its appearance or behavior. For example, we can change the label of the button using the setText() method:
1 |
button.setText("New label") |
We can set the size of the button using the setFixedSize() method:
1 |
button.setFixedSize(100, 50) |
We can disable the button using the setEnabled() method:
1 |
button.setEnabled(False) |
These are just a few of the many properties that we can set for a QPushButton.
This is the complete code Python PySide6 QPushButton
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import sys from PySide6.QtWidgets import QApplication,QWidget , QVBoxLayout, QPushButton class MainWindow(QWidget): def __init__(self): super().__init__() self.setWindowTitle("QPushButton Example") self.setGeometry(100, 100, 300, 200) layout = QVBoxLayout() # Create a new QPushButton and add it to the layout self.button = QPushButton("Click me!") layout.addWidget(self.button) # Connect the button's clicked signal to a slot self.button.clicked.connect(self.on_button_click) self.setLayout(layout ) def on_button_click(self): # Change the button's label and disable it self.button.setText("Button was clicked!") self.button.setEnabled(False) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec()) |
in this example we create a new MainWindow object that contains a single QPushButton. When the button is clicked, the on_button_click() slot is called, which changes the button’s label to “Button was clicked!” and disables it.
Run the code and this will be the result.