In this PyQt6 lesson we are going to learn about Creating Button in Qt Designer with PyQt6, we have already learned that how you can create buttons and labels in PyQt6 using coding, now we want to use Qt Designer to do the same functionality.
Open your Qt Designer using pyqt5designer command in your terminal, after that create a new Widget application window, you can drag and drop the QPushButton and QLabel from the Widget Box.

If you want to add some stylesheets for your widgets, than you need to right click on your window, after that choose Changestylesheet.

After completing the design, save the file and it will be .ui extension file, we need to convert this to py file, we want to use pyuic6 module for this, so run this command in your terminal.
1 |
pyuic6 -x Button.ui -o mybutton.py |
This is the converted file, but we have also added the signal and slot functionality.
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
from PyQt6 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(400, 316) Form.setStyleSheet("QPushButton {\n" "\n" "background-color:\"red\"\n" "\n" "}\n" "\n" "QLabel {\n" "\n" "color:\"green\"\n" "\n" "}") self.pushButton = QtWidgets.QPushButton(Form) self.pushButton.setGeometry(QtCore.QRect(140, 30, 100, 100)) self.pushButton.setObjectName("pushButton") #connected the button with method self.pushButton.clicked.connect(self.clicked_btn) self.label = QtWidgets.QLabel(Form) self.label.setGeometry(QtCore.QRect(10, 180, 341, 41)) font = QtGui.QFont() font.setPointSize(16) font.setBold(True) font.setWeight(75) self.label.setFont(font) self.label.setObjectName("label") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def clicked_btn(self): self.label.setText("Check geekscoders.com website") self.label.setStyleSheet('background-color:red') def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.pushButton.setText(_translate("Form", "Click Me")) self.label.setText(_translate("Form", "My Label")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Form = QtWidgets.QWidget() ui = Ui_Form() ui.setupUi(Form) Form.show() sys.exit(app.exec()) |
This is our method for changing the text and background color of the label.
1 2 3 |
def clicked_btn(self): self.label.setText("Check geekscoders.com website") self.label.setStyleSheet('background-color:red') |
In here we have connected our clicked signal of the button with the method that we have already created.
1 |
self.pushButton.clicked.connect(self.clicked_btn) |
Run the code and this is the result.
