PyQt6 Button Click Event Example

  • Share this:

Code introduction


This code creates a simple PyQt6 application with a button. When the button is clicked, it prints a message.


Technology Stack : PyQt6, Python

Code Type : PyQt6 GUI Application

Code Difficulty : Intermediate


                
                    
import random
from PyQt6.QtWidgets import QApplication, QPushButton, QVBoxLayout, QWidget

def create_random_button():
    app = QApplication([])
    window = QWidget()
    layout = QVBoxLayout()
    
    button = QPushButton("Click Me!")
    layout.addWidget(button)
    window.setLayout(layout)
    window.show()
    
    # Simulate button click
    button.clicked.connect(lambda: print("Button clicked!"))
    
    app.exec()                
              
Tags: