Я немного новичок в Qt и python.Я создал простой графический интерфейс, который состоит из простого калькулятора.Я использовал QT Designer для разработки GUI, и теперь я хочу связать свои кнопки с дисплеем QLCDNumber.
Я создал * .clicked.connect (self.my_func), чтобы связать его, но только инструкция print работает хорошо при нажатии кнопки.
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
self.gridLayoutWidget = QtWidgets.QWidget(Dialog)
self.gridLayoutWidget.setGeometry(QtCore.QRect(20, 80, 164, 134))
self.gridLayoutWidget.setObjectName("gridLayoutWidget")
self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
self.gridLayout.setContentsMargins(0, 0, 0, 0)
self.gridLayout.setObjectName("gridLayout")
self.pushButton_9 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_9.setObjectName("pushButton_9")
self.gridLayout.addWidget(self.pushButton_9, 0, 2, 1, 1)
self.pushButton_9.clicked.connect(self.my_func)
self.pushButton_5 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_5.setObjectName("pushButton_5")
self.gridLayout.addWidget(self.pushButton_5, 1, 1, 1, 1)
self.pushButton_7 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_7.setObjectName("pushButton_7")
self.gridLayout.addWidget(self.pushButton_7, 0, 0, 1, 1)
self.pushButton_6 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_6.setObjectName("pushButton_6")
self.gridLayout.addWidget(self.pushButton_6, 1, 2, 1, 1)
self.pushButton_8 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_8.setObjectName("pushButton_8")
self.gridLayout.addWidget(self.pushButton_8, 0, 1, 1, 1)
self.pushButton_4 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_4.setObjectName("pushButton_4")
self.gridLayout.addWidget(self.pushButton_4, 1, 0, 1, 1)
self.pushButton_3 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_3.setObjectName("pushButton_3")
self.gridLayout.addWidget(self.pushButton_3, 2, 2, 1, 1)
self.pushButton_0 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_0.setObjectName("pushButton_0")
self.gridLayout.addWidget(self.pushButton_0, 3, 0, 1, 3)
self.pushButton_2 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 2, 1, 1, 1)
self.pushButton_1 = QtWidgets.QPushButton(self.gridLayoutWidget)
self.pushButton_1.setObjectName("pushButton_1")
self.gridLayout.addWidget(self.pushButton_1, 2, 0, 1, 1)
self.pushButton_6.raise_()
self.pushButton_5.raise_()
self.pushButton_4.raise_()
self.pushButton_1.raise_()
self.pushButton_2.raise_()
self.pushButton_3.raise_()
self.pushButton_8.raise_()
self.pushButton_9.raise_()
self.pushButton_0.raise_()
self.pushButton_7.raise_()
self.lcdNumber = QtWidgets.QLCDNumber(Dialog)
self.lcdNumber.setGeometry(QtCore.QRect(20, 10, 221, 61))
self.lcdNumber.setObjectName("lcdNumber")
self.gridLayoutWidget_2 = QtWidgets.QWidget(Dialog)
self.gridLayoutWidget_2.setGeometry(QtCore.QRect(190, 80, 51, 134))
self.gridLayoutWidget_2.setObjectName("gridLayoutWidget_2")
self.gridLayout_2 = QtWidgets.QGridLayout(self.gridLayoutWidget_2)
self.gridLayout_2.setContentsMargins(0, 0, 0, 0)
self.gridLayout_2.setObjectName("gridLayout_2")
self.pushButton_10 = QtWidgets.QPushButton(self.gridLayoutWidget_2)
self.pushButton_10.setObjectName("pushButton_10")
self.gridLayout_2.addWidget(self.pushButton_10, 2, 0, 1, 1)
self.pushButton = QtWidgets.QPushButton(self.gridLayoutWidget_2)
self.pushButton.setObjectName("pushButton")
self.gridLayout_2.addWidget(self.pushButton, 1, 0, 1, 1)
self.pushButton_11 = QtWidgets.QPushButton(self.gridLayoutWidget_2)
self.pushButton_11.setObjectName("pushButton_11")
self.gridLayout_2.addWidget(self.pushButton_11, 3, 0, 1, 1)
self.pushButton_12 = QtWidgets.QPushButton(self.gridLayoutWidget_2)
self.pushButton_12.setObjectName("pushButton_12")
self.gridLayout_2.addWidget(self.pushButton_12, 0, 0, 1, 1)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(20, 230, 60, 16))
self.label.setObjectName("label")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def my_func(self):
print("Works")
self.lcdNumber.display(9)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton_9.setText(_translate("Dialog", "9"))
self.pushButton_5.setText(_translate("Dialog", "5"))
self.pushButton_7.setText(_translate("Dialog", "7"))
self.pushButton_6.setText(_translate("Dialog", "6"))
self.pushButton_8.setText(_translate("Dialog", "8"))
self.pushButton_4.setText(_translate("Dialog", "4"))
self.pushButton_3.setText(_translate("Dialog", "3"))
self.pushButton_0.setText(_translate("Dialog", "0"))
self.pushButton_2.setText(_translate("Dialog", "2"))
self.pushButton_1.setText(_translate("Dialog", "1"))
self.pushButton_10.setText(_translate("Dialog", "-"))
self.pushButton.setText(_translate("Dialog", "+"))
self.pushButton_11.setText(_translate("Dialog", "="))
self.pushButton_12.setText(_translate("Dialog", "DEL"))
self.label.setText(_translate("Dialog", "TextLabel"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())