Class issue in connect definition
-
Can someone please help me with how I can edit
the label when the push button is clicked via the pushButton connect? Can't figure it out. The class UI_dialog is created by PYUIC in Pycharm.from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(585, 321) self.label = QtWidgets.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(250, 50, 61, 21)) self.label.setMinimumSize(QtCore.QSize(41, 0)) self.label.setAlignment(QtCore.Qt.AlignCenter) self.label.setWordWrap(True) self.label.setObjectName("label") self.pushButton = QtWidgets.QPushButton(Dialog) self.pushButton.setGeometry(QtCore.QRect(266, 98, 31, 23)) self.pushButton.setObjectName("pushButton") font = QtGui.QFont() font.setPointSize(12) self.retranslateUi(Dialog) self.pushButton.clicked.connect(Dialog.SB_UP_click) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.label.setText(_translate("Dialog", "SOUTH")) self.pushButton.setText(_translate("Dialog", "UP")) from PyQt5 import QtCore, QtGui, QtWidgets from My_Window import Ui_Dialog import sys class MainWindow(QtWidgets.QMainWindow, Ui_Dialog): def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_Dialog() self.ui.setupUi(self) def SB_UP_click(self): print("SB UP") self.label.setText("HI") print("got here") def main(): app = QtWidgets.QApplication([]) window = MainWindow() window.show() sys.exit(app.exec_()) if __name__ == '__main__': main()
-
Late night strikes again ! :-D
I misread the code.Your slot should be something like:
def SB_UP_click(self): print("SB UP") self.ui.label.setText("HI") print("got here")
-
Can someone please help me with how I can edit
the label when the push button is clicked via the pushButton connect? Can't figure it out. The class UI_dialog is created by PYUIC in Pycharm.from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Dialog(object): def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(585, 321) self.label = QtWidgets.QLabel(Dialog) self.label.setGeometry(QtCore.QRect(250, 50, 61, 21)) self.label.setMinimumSize(QtCore.QSize(41, 0)) self.label.setAlignment(QtCore.Qt.AlignCenter) self.label.setWordWrap(True) self.label.setObjectName("label") self.pushButton = QtWidgets.QPushButton(Dialog) self.pushButton.setGeometry(QtCore.QRect(266, 98, 31, 23)) self.pushButton.setObjectName("pushButton") font = QtGui.QFont() font.setPointSize(12) self.retranslateUi(Dialog) self.pushButton.clicked.connect(Dialog.SB_UP_click) QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.label.setText(_translate("Dialog", "SOUTH")) self.pushButton.setText(_translate("Dialog", "UP")) from PyQt5 import QtCore, QtGui, QtWidgets from My_Window import Ui_Dialog import sys class MainWindow(QtWidgets.QMainWindow, Ui_Dialog): def __init__(self): super(MainWindow, self).__init__() self.ui = Ui_Dialog() self.ui.setupUi(self) def SB_UP_click(self): print("SB UP") self.label.setText("HI") print("got here") def main(): app = QtWidgets.QApplication([]) window = MainWindow() window.show() sys.exit(app.exec_()) if __name__ == '__main__': main()
@heedaf said in Class issue in connect definition:
self.pushButton.clicked.connect(Dialog.SB_UP_click)
You're already connecting, so what does not work?
And please indent your code properly: it is hard to read.
-
@heedaf said in Class issue in connect definition:
self.pushButton.clicked.connect(Dialog.SB_UP_click)
You're already connecting, so what does not work?
And please indent your code properly: it is hard to read.
-
Hi,
You do not have any widget named label_2 be it in your designer part or your MainWindow class.
You have one that is named label that you can access through "self.ui".
-
I see.
You did the connection in the wrong class.
Setup the connection in your MainWindow class not the one that is building the UI.Wrong code reading, it's really just the slot that has an error.
-
No this one:
@heedaf said in Class issue in connect definition:
self.pushButton.clicked.connect(Dialog.SB_UP_click)
-
No this one:
@heedaf said in Class issue in connect definition:
self.pushButton.clicked.connect(Dialog.SB_UP_click)
-
Late night strikes again ! :-D
I misread the code.Your slot should be something like:
def SB_UP_click(self): print("SB UP") self.ui.label.setText("HI") print("got here")
-
You likely forgot the .ui between the self and .label.
Working with designer based UI requires some getting used to since you have all the code in the same file.
You have the Ui_XXX classes for the aesthetics of your widget (and which is auto generated) and the corresponding XXX that is for the logic you want to apply.