Interactive PySide6 Apps: Getting Started with QPushButton

 


QPushButton: Represents a button that users can click to perform an action.

The QPushButton widget in PySide6 represents a clickable button that users can interact with to perform actions. It's one of the most commonly used widgets in GUI applications, providing a simple and intuitive way for users to trigger specific functionality.

Key Features of QPushButton

  1. Click Action: Triggers an action when clicked.
  2. Customizable Text: The text displayed on the button can be easily set and modified.
  3. Icon Support: Buttons can display icons alongside text.
  4. Signals and Slots: Emits signals like clicked to handle button interactions.
  5. Styles and Themes: Supports various styles and can be customized using style sheets.

Creating a QPushButton

To create a QPushButton in PySide6, instantiate the QPushButton class and add it to a layout. Here is a basic example:

from PySide6.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget

class ButtonExample(QWidget):
    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()

        self.button = QPushButton("Click Me")
        self.button.clicked.connect(self.on_click)

        layout.addWidget(self.button)
        self.setLayout(layout)
        self.setWindowTitle('QPushButton Example')

    def on_click(self):
        print("Button clicked!")

app = QApplication([])
window = ButtonExample()
window.show()
app.exec()

In this example, we create a simple QPushButton with the text "Click Me." When the button is clicked, it triggers the on_click method, printing "Button clicked!" to the console.

Adding Icons to QPushButton

You can add an icon to a QPushButton using the setIcon method. This is useful for enhancing the visual appeal and functionality of the button:

from PySide6.QtGui import QIcon

self.button.setIcon(QIcon("icon.png"))

This adds the specified icon to the button, displaying it alongside the button text.

Handling Signals

The QPushButton emits several signals that you can connect to custom slot functions. The most commonly used signal is clicked, which is emitted when the button is pressed and released:

self.button.clicked.connect(self.on_click)

You can also connect other signals like pressed and released to handle more specific interactions.

Customizing Appearance

The appearance of QPushButton can be customized using style sheets (QSS). This allows you to change the button's color, border, font, and more:

self.button.setStyleSheet("""
    QPushButton {
        background-color: #3498db;
        color: white;
        border-radius: 5px;
        padding: 10px;
    }
    QPushButton:hover {
        background-color: #2980b9;
    }
""")

Practical Example

Let's create a practical example where the QPushButton is used to change the text of a QLabel when clicked:

from PySide6.QtWidgets import QApplication, QPushButton, QLabel, QVBoxLayout, QWidget

class ButtonLabelExample(QWidget):
    def __init__(self):
        super().__init__()

        layout = QVBoxLayout()

        self.label = QLabel("Hello, World!")
        self.button = QPushButton("Change Text")

        self.button.clicked.connect(self.change_text)

        layout.addWidget(self.label)
        layout.addWidget(self.button)
        self.setLayout(layout)
        self.setWindowTitle('Button and Label Example')

    def change_text(self):
        self.label.setText("Text Changed!")

app = QApplication([])
window = ButtonLabelExample()
window.show()
app.exec()

In this example, a QPushButton is used to change the text of a QLabel from "Hello, World!" to "Text Changed!" when clicked.

Post a Comment

Previous Post Next Post