How do you create a button in PyQt5?

In PyQt5, you can create a button using the QPushButton class from the QtWidgets module. Below is a simple example code:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton

def on_click():
    print('Button clicked')

app = QApplication(sys.argv)

window = QWidget()
window.setGeometry(100, 100, 200, 100)

button = QPushButton('Click me', window)
button.setGeometry(50, 50, 100, 30)
button.clicked.connect(on_click)

window.show()

sys.exit(app.exec_())

In this sample code, we start by importing the necessary modules and then create an application instance. Next, we create a window and a button, and connect the button’s clicked signal to a slot function called on_click. When click the button , the slot function is called and a message is output. Finally, the window is displayed and the application is launched.

 

More tutorials

How to set the title and size of a PyQt5 window?(Opens in a new browser tab)

How to add a button column in a DataGridView?(Opens in a new browser tab)

How to create a button control in WinForms?(Opens in a new browser tab)

How can a text box be created in PyQt5?(Opens in a new browser tab)

What is the method for drawing graphics in PyQt5?(Opens in a new browser tab)

 

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds